Skip to content

load_npz

Load a sparse matrix in numpy's .npz format from disk. Note: This is not binary compatible with scipy's save_npz() output. This binary format is not currently stable. Will only load files saved by this package.

Parameters:

Name Type Description Default
filename file-like object, string, or pathlib.Path

The file to read. File-like objects must support the seek() and read() methods.

required

Returns:

Type Description
SparseArray

The sparse matrix at path filename.

Examples:

See sparse.save_npz for usage examples.

See Also
Source code in sparse/numba_backend/_io.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def load_npz(filename):
    """Load a sparse matrix in numpy's `.npz` format from disk.
    Note: This is not binary compatible with scipy's `save_npz()`
    output. This binary format is not currently stable.
    Will only load files saved by this package.

    Parameters
    ----------
    filename : file-like object, string, or pathlib.Path
        The file to read. File-like objects must support the
        `seek()` and `read()` methods.

    Returns
    -------
    SparseArray
        The sparse matrix at path `filename`.

    Examples
    --------
    See [`sparse.save_npz`][] for usage examples.

    See Also
    --------
    - [`sparse.save_npz`][]
    - [`scipy.sparse.save_npz`][]
    - [`scipy.sparse.load_npz`][]
    - [`numpy.savez`][]
    - [`numpy.load`][]

    """

    with np.load(filename) as fp:
        cls = getattr(sparse, fp["format"].item().upper())
        return cls.from_nodes(fp)