load_csr_matrix

pyhelpers.store.load_csr_matrix(path_to_file, verbose=False, prt_kwargs=None, raise_error=False, **kwargs)[source]

Loads in a compressed sparse row (CSR) or compressed row storage (CRS).

Parameters:
  • path_to_file (str | os.PathLike) – Path to the CSR file (e.g. with extension “.npz”).

  • verbose (bool | int) – Whether to print relevant information in console as the function runs; defaults to False.

  • prt_kwargs (dict | None) – [Optional] Additional parameters for pyhelpers.store._check_loading_path(); defaults to None.

  • raise_error (bool) – Whether to raise the provided exception; if raise_error=False (default), the error will be suppressed.

  • kwargs – [Optional] Additional parameters for the function numpy.load().

Returns:

A compressed sparse row.

Return type:

scipy.sparse.csr.csr_matrix

Examples:

>>> from pyhelpers.store import load_csr_matrix
>>> from pyhelpers.dirs import cd
>>> from scipy.sparse import csr_matrix
>>> data = [1, 2, 3, 4, 5, 6]
>>> indices = [0, 2, 2, 0, 1, 2]
>>> indptr = [0, 2, 3, 6]
>>> csr_mat = csr_matrix((data, indices, indptr), shape=(3, 3))
>>> csr_mat
<3x3 sparse matrix of type '<class 'numpy.int32'>'
    with 6 stored elements in Compressed Sparse Row format>
>>> path_to_csr_npz = cd("tests", "data", "csr_mat.npz")
>>> csr_mat_ = load_csr_matrix(path_to_csr_npz, verbose=True)
Loading ".\tests\data\csr_mat.npz" ... Done.
>>> # .nnz gets the count of explicitly-stored values (non-zeros)
>>> (csr_mat != csr_mat_).count_nonzero() == 0
True
>>> (csr_mat != csr_mat_).nnz == 0
True