Uri Goren
Uri Goren

Reputation: 13672

accessing all non zero entries of a csr_matrix

I have a sparse matrix:

from scipy.sparse import csr_matrix
M=csr_matrix((5,5))
M[2,3]=4

I would like to iterate all non-zero entries, something like:

for x,y,v in M.non_zero_entries:
   do_something()

I try to comprehend the values of M.data,M.indices and M.indptr

Now, in the example above:

print (M.data) #outputs [4]
print (M.indices) #outputs [3]
print (M.indptr) #outputs [0,0,0,1,1,1]

How can I extract the non-zero records from that ?

Upvotes: 7

Views: 1757

Answers (2)

kuzand
kuzand

Reputation: 9796

What you are (were) looking for is the nonzero method:

csr_matrix.nonzero()

Returns a tuple of arrays (row,col) containing the indices of the non-zero elements of the matrix.

So your loop would be like this:

for row, col in zip(*M.nonzero()):
    val = M[row, col]
    # do something
    print((row, col), val)

Upvotes: 10

Gareth McCaughan
Gareth McCaughan

Reputation: 19971

You could use M.tocoo() which returns a "coordinate-format" version of the matrix, which has vectors data, row and col which you use in the "obvious" way.

Or you could do it by hand. Something like this (WARNING: tested on exactly one example, and I've given no thought to efficiency):

def csr_entries(M):
    """Generator of tuples (i,j,x) of sparse matrix entries
    meaning that M[i,j]=x."""
    for row in range(len(M.indptr)-1):
        i,j = M.indptr[row],M.indptr[row+1]
        for k in range(i,j):
            yield (row, M.indices[k], M.data[k])

Upvotes: 4

Related Questions