+1 vote
in Programming Languages by (77.8k points)
I want to find all rows and columns of CSR matrix that contain non-zero values. Also, the values of those non-zero values. Is there any function for it?

1 Answer

+2 votes
by (60.2k points)
edited by
 
Best answer

You can use the find() function of scipy. This function returns the indices and values of the non-zero elements of a CSR matrix. The function returns three arrays: values for rows, values for columns, and non-zero data values.

Here is an example:

>>> from scipy.sparse import csr_array, find

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

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

>>> data = np.array([11, 15, 32, 44, 28])

>>> x=csr_array((data, (r, c)), shape=(3, 3))

>>> x.toarray()

array([[11, 15, 28],

       [ 0, 32,  0],

       [ 0, 44,  0]])

>>> find(x)

(array([0, 0, 1, 2, 0], dtype=int32), array([0, 1, 1, 1, 2], dtype=int32), array([11, 15, 32, 44, 28]))


...