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 ?
N = nnz( X ) returns the number of nonzero elements in matrix X .
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.
getnnz , which is the number of nonzero terms of a 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.
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)
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With