parse_csr_matrix

pyhelpers.ops.parse_csr_matrix(path_to_csr, verbose=False, **kwargs)

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

Parameters
  • path_to_csr (str) – path where a CSR file (e.g. with a file extension “.npz”) is saved

  • verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to False

  • kwargs – [optional] parameters of numpy.load

Returns

a compressed sparse row

Return type

scipy.sparse.csr.csr_matrix

Examples:

>>> from pyhelpers.ops import parse_csr_matrix
>>> from pyhelpers.dirs import cd
>>> from scipy.sparse import csr_matrix, save_npz

>>> data_ = [1, 2, 3, 4, 5, 6]
>>> indices_ = [0, 2, 2, 0, 1, 2]
>>> indptr_ = [0, 2, 3, 6]

>>> csr_m = csr_matrix((data_, indices_, indptr_), shape=(3, 3))
>>> csr_m
<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")
>>> save_npz(path_to_csr_npz, csr_m)

>>> parsed_csr_mat = parse_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)
>>> (parsed_csr_mat != csr_m).count_nonzero() == 0
True

>>> (parsed_csr_mat != csr_m).nnz == 0
True