# Python : How can I find number of 1's in CSR (Compressed Sparse Row matrix) matrix ?

I have created a CSR matrix using scipy.sparse.csr_matrix and the elements in the matrix are either 0 or 1. The matrix is very sparse and big. Is there any way to count how many matrix elements have value 1?

by (13.1k points)

There are several ways to count number of 1's in your CSR matrix, but the quickest way is to use __getattr__

Example:

>>> X.toarray()
array([[0, 1, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 1, 1],
[1, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[0, 1, 1, 1, 0],
[1, 0, 0, 0, 0]], dtype=int8)
>>> print(X.__getattr__)
<bound method spmatrix.__getattr__ of <7x5 sparse matrix of type '<class 'numpy.int8'>'
with 13 stored elements in Compressed Sparse Row format>>

Another approach: You can convert the matrix to COOrdinate format using tocoo() function and then check the length.

>>> X_1 = (X == 1)
>>> X_1
<7x5 sparse matrix of type '<class 'numpy.bool_'>'
with 13 stored elements in Compressed Sparse Row format>
>>> co=X_1.tocoo()
>>> co
<7x5 sparse matrix of type '<class 'numpy.bool_'>'
with 13 stored elements in COOrdinate format>
>>> m=set(zip(co.row,co.col))
>>> len(m)
13