+1 vote
in Programming Languages by (59.8k points)
I want to find the indices (row, column) of all non-zero elements of a CSR matrix. Is there any Numpy or Scipy function for it?

1 Answer

+2 votes
by (71.8k points)
selected by
 
Best answer

The nonzero() function of the csr_matrix module of scipy returns a tuple of arrays (row, col) containing the indices of the non-zero elements of a CSR matrix.

You can combine the row and column arrays returned by the nonzero() function using zip() to get the indices of non-zero elements.

Here is an example to show how to use the nonzero() function:

>>> import numpy as np

>>> from scipy.sparse import csr_matrix, save_npz, load_npz

>>> r = np.array([0, 0, 1, 2, 2, 3, 3, 3])

>>> c = np.array([0, 1, 2, 1, 2, 0, 2, 3])

>>> v = np.array([1, 2, 3, 4, 5, 6, 7, 8])

>>> x=csr_matrix((v, (r,c)), shape=(4,4))

>>> x

<4x4 sparse matrix of type '<class 'numpy.int64'>'

with 8 stored elements in Compressed Sparse Row format>

>>> x.toarray()

array([[1, 2, 0, 0],

       [0, 0, 3, 0],

       [0, 4, 5, 0],

       [6, 0, 7, 8]])

>>> i,j=x.nonzero()

>>> i

array([0, 0, 1, 2, 2, 3, 3, 3], dtype=int32)

>>> j

array([0, 1, 2, 1, 2, 0, 2, 3], dtype=int32)

>>> list(zip(i,j))

[(0, 0), (0, 1), (1, 2), (2, 1), (2, 2), (3, 0), (3, 2), (3, 3)]


...