How can I reshape efficiently and scipy.sparse csr_matrix?
I need to add zero rows at the end. Using:
from scipy.sparse import csr_matrix
data = [1,2,3,4,5,6]
col = [0,0,0,1,1,1]
row = [0,1,2,0,1,2]
a = csr_matrix((data, (row, col)))
a.reshape(3,5)
I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/scipy/sparse/base.py", line 129, in reshape
self.__class__.__name__)
NotImplementedError: Reshaping not implemented for csr_matrix.
The
reshape()
method will work withcsr_matrix
objects in scipy 1.1, which is close to being released. In the meantime, you can try the code at Reshape sparse matrix efficiently, Python, SciPy 0.12 for reshaping a sparse matrix.Your example won't work, however, because you are trying to reshape an array with shape (3, 2) into an array with shape (3, 5). The code linked to above and the sparse
reshape()
method follow the same rules as thereshape()
method of numpy arrays: you can't change the total size of the array.If you want to change the total size, you will eventually be able to use the
resize()
method (which operates in-place), but that is also a new feature of scipy 1.1, so it is not yet released.Instead, you can construct a new sparse matrix as follows:
If you can catch the problem early enough, just include a shape parameter:
You could also
hstack
on a pad. Make sure it's the sparse version:hstack
usessparse.bmat
. That combines thecoo
attributes of the 2 arrays, and makes a newcoo
matrix.