a
    ˆù»j  ã                   @   s8   d dl Zd dlZddgZedd�Zd	dd„Zdd„ ZdS )
é    NÚsave_npzÚload_npzF)Zallow_pickleTc                 C   sÔ   i }|j dv r"|j|j|jd� nN|j dkr<|j|jd� n4|j dkrZ|j|j|jd� nd|j › d�}t|ƒ‚|j|j  d	¡|j	|j
d
� t|tjjƒr¦|jdd� |r¾tj| fi |¤Ž ntj| fi |¤Ž dS )a€   Save a sparse matrix or array to a file using ``.npz`` format.

    Parameters
    ----------
    file : str or file-like object
        Either the file name (string) or an open file (file-like object)
        where the data will be saved. If file is a string, the ``.npz``
        extension will be appended to the file name if it is not already
        there.
    matrix: spmatrix or sparray
        The sparse matrix or array to save.
        Supported formats: ``csc``, ``csr``, ``bsr``, ``dia`` or ``coo``.
    compressed : bool, optional
        Allow compressing the file. Default: True

    See Also
    --------
    scipy.sparse.load_npz: Load a sparse matrix from a file using ``.npz`` format.
    numpy.savez: Save several arrays into a ``.npz`` archive.
    numpy.savez_compressed : Save several arrays into a compressed ``.npz`` archive.

    Examples
    --------
    Store sparse matrix to disk, and load it again:

    >>> import numpy as np
    >>> import scipy as sp
    >>> sparse_matrix = sp.sparse.csc_matrix([[0, 0, 3], [4, 0, 0]])
    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)

    >>> sp.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)
    >>> sparse_matrix = sp.sparse.load_npz('/tmp/sparse_matrix.npz')

    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)
    ©ZcscZcsrZbsr)ÚindicesÚindptrÚdia)ÚoffsetsÚcoo)ÚrowÚcolz4Save is not implemented for sparse matrix of format Ú.Úascii)ÚformatÚshapeÚdataT)Ú	_is_arrayN)r   Úupdater   r   r   r
   r   ÚNotImplementedErrorÚencoder   r   Ú
isinstanceÚspÚsparseZsparrayÚnpZsavez_compressedZsavez)ÚfileÚmatrixÚ
compressedZarrays_dictÚmsg© r   ú=E:\soccer\ml_env\Lib\site-packages\scipy/sparse/_matrix_io.pyr      s&    .



ýc                 C   s–  t j| fi t¤Ž��l}| d¡}|du r8td| › d�ƒ‚| ¡ }t|tƒsT| d¡}| d¡rh|d }n|d }zt	t
j|› ƒ}W n6 ty¸ } ztd	|› d
�ƒ|‚W Y d}~n
d}~0 0 |dv rò||d |d |d f|d d�W  d  ƒ S |dk�r&||d |d f|d d�W  d  ƒ S |dk�rb||d |d |d ff|d d�W  d  ƒ S td|› d�ƒ‚W d  ƒ n1 �sˆ0    Y  dS )aû   Load a sparse array/matrix from a file using ``.npz`` format.

    Parameters
    ----------
    file : str or file-like object
        Either the file name (string) or an open file (file-like object)
        where the data will be loaded.

    Returns
    -------
    result : csc_array, csr_array, bsr_array, dia_array or coo_array
        A sparse array/matrix containing the loaded data.

    Raises
    ------
    OSError
        If the input file does not exist or cannot be read.

    See Also
    --------
    scipy.sparse.save_npz: Save a sparse array/matrix to a file using ``.npz`` format.
    numpy.load: Load several arrays from a ``.npz`` archive.

    Examples
    --------
    Store sparse array/matrix to disk, and load it again:

    >>> import numpy as np
    >>> import scipy as sp
    >>> sparse_array = sp.sparse.csc_array([[0, 0, 3], [4, 0, 0]])
    >>> sparse_array
    <2x3 sparse array of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_array.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)

    >>> sp.sparse.save_npz('/tmp/sparse_array.npz', sparse_array)
    >>> sparse_array = sp.sparse.load_npz('/tmp/sparse_array.npz')

    >>> sparse_array
    <2x3 sparse array of type '<class 'numpy.int64'>'
        with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_array.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)

    In this example we force the result to be csr_array from csr_matrix
    >>> sparse_matrix = sp.sparse.csc_matrix([[0, 0, 3], [4, 0, 0]])
    >>> sp.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)
    >>> tmp = sp.sparse.load_npz('/tmp/sparse_matrix.npz')
    >>> sparse_array = sp.sparse.csr_array(tmp)
    r   Nz	The file z+ does not contain a sparse array or matrix.r   r   Z_arrayZ_matrixzUnknown format "ú"r   r   r   r   r   )r   r   r   r	   r
   r   z4Load is not implemented for sparse matrix of format r   )r   ÚloadÚPICKLE_KWARGSÚgetÚ
ValueErrorÚitemr   ÚstrÚdecodeÚgetattrr   r   ÚAttributeErrorr   )r   ZloadedZsparse_formatZsparse_typeÚclsÚer   r   r   r   P   s:    6




(ÿ
ÿ
ÿÿ)T)	Únumpyr   Zscipyr   Ú__all__Údictr!   r   r   r   r   r   r   Ú<module>   s
   

E