reshape scipy csr matrix

2019-03-05 06:29发布

问题:

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.

回答1:

If you can catch the problem early enough, just include a shape parameter:

In [48]: a = csr_matrix((data, (row, col)))
In [49]: a
Out[49]: 
<3x2 sparse matrix of type '<class 'numpy.int64'>'
    with 6 stored elements in Compressed Sparse Row format>
In [50]: a = csr_matrix((data, (row, col)),shape=(3,5))
In [51]: a
Out[51]: 
<3x5 sparse matrix of type '<class 'numpy.int64'>'
    with 6 stored elements in Compressed Sparse Row format>
In [52]: a.A
Out[52]: 
array([[1, 4, 0, 0, 0],
       [2, 5, 0, 0, 0],
       [3, 6, 0, 0, 0]], dtype=int64)

You could also hstack on a pad. Make sure it's the sparse version:

In [59]: z = sparse.coo_matrix(np.zeros((3,3)))
In [60]: z
Out[60]: 
<3x3 sparse matrix of type '<class 'numpy.float64'>'
    with 0 stored elements in COOrdinate format>
In [61]: sparse.hstack((a,z))
Out[61]: 
<3x5 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in COOrdinate format>
In [62]: _.A
Out[62]: 
array([[1., 4., 0., 0., 0.],
       [2., 5., 0., 0., 0.],
       [3., 6., 0., 0., 0.]])

hstack uses sparse.bmat. That combines the coo attributes of the 2 arrays, and makes a new coo matrix.



回答2:

The reshape() method will work with csr_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 the reshape() 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:

In [57]: b = csr_matrix((a.data, a.indices, a.indptr), shape=(3, 5))

In [58]: b.shape
Out[58]: (3, 5)

In [59]: b.A
Out[59]: 
array([[1, 4, 0, 0, 0],
       [2, 5, 0, 0, 0],
       [3, 6, 0, 0, 0]], dtype=int64)