7

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 ?

2 Answers 2

10

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) 
Sign up to request clarification or add additional context in comments.

Comments

4

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]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.