Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 794
Uri Goren Avatar asked Jun 02 '16 17:06

Uri Goren


People also ask

How do you find non zero entries in a sparse matrix?

N = nnz( X ) returns the number of nonzero elements in matrix X .

What does Csr_matrix do in Python?

The function csr_matrix() is used to create a sparse matrix of compressed sparse row format whereas csc_matrix() is used to create a sparse matrix of compressed sparse column format.

What is Getnnz?

getnnz , which is the number of nonzero terms of a sparse matrix.

What is nnz in sparse matrix?

nnz returns the number of nonzero elements in a sparse matrix. nonzeros returns a column vector containing all the nonzero elements of a sparse matrix. nzmax returns the amount of storage space allocated for the nonzero entries of a sparse matrix.


2 Answers

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)
like image 149
Andreas K. Avatar answered Sep 20 '22 16:09

Andreas K.


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])
like image 36
Gareth McCaughan Avatar answered Sep 20 '22 16:09

Gareth McCaughan