I need a sparse matrix (I'm using Compressed Sparse Row Format (CSR) from scipy.sparse) to do some computation. I have it in a form of (data, (row, col)) tuple. Unfortunately some of the rows and columns will be all equal zero and I would like to get rid of those zeros. Right now I have:
[In]:
from scipy.sparse import csr_matrix
aa = csr_matrix((1,2,3), ((0,2,2), (0,1,2))
aa.todense()
[Out]:
matrix([[1, 0, 0],
[0, 0, 0],
[0, 2, 3]], dtype=int64)
And I would like to have:
[Out]:
matrix([[1, 0, 0],
[0, 2, 3]], dtype=int64)
After using the method eliminate_zeros() on the object I get None:
[In]:
aa2 = csr_matrix.eliminate_zeros(aa)
type(aa2)
[Out]:
<class 'NoneType'>
Why does that method turn it into None?
Is there any other way to get a sparse matrix (doesn't have to be CSR) and get rid of empty rows/columns easily?
I'm using Python 3.4.0.
aavariable? Then it wouldn't return anything (i.e.None).0sfrom the.data, but does not change dimensions.elminate_zerosdoes not changeaa=sparse.csr_matrix(((1,2,3),((0,2,2),(0,1,2)))), since it doesn't have extra 0s.