Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a column of zeroes to a csr_matrix

I have an MxN sparse csr_matrix, and I'd like to add a few columns with only zeroes to the right of the matrix. In principle, the arrays indptr, indices and data keep the same, so I only want to change the dimensions of the matrix. However, this seems to be not implemented.

>>> A = csr_matrix(np.identity(5), dtype = int)
>>> A.toarray()
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1]])
>>> A.shape
(5, 5)
>>> A.shape = ((5,7))
NotImplementedError: Reshaping not implemented for csr_matrix.

Also horizontally stacking a zero matrix does not seem to work.

>>> B = csr_matrix(np.zeros([5,2]), dtype = int)
>>> B.toarray()
array([[0, 0],
       [0, 0],
       [0, 0],
       [0, 0],
       [0, 0]])
>>> np.hstack((A,B))
array([ <5x5 sparse matrix of type '<type 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>,
       <5x2 sparse matrix of type '<type 'numpy.int32'>'
    with 0 stored elements in Compressed Sparse Row format>], dtype=object)

This is what I want to achieve eventually. Is there a quick way to reshape my csr_matrix without copying everything in it?

>>> C = csr_matrix(np.hstack((A.toarray(), B.toarray())))
>>> C.toarray()
array([[1, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0]])
like image 281
physicalattraction Avatar asked Oct 20 '14 09:10

physicalattraction


2 Answers

You can use scipy.sparse.vstack or scipy.sparse.hstack to do it faster:

from scipy.sparse import csr_matrix, vstack, hstack

B = csr_matrix((5, 2), dtype=int)
C = csr_matrix((5, 2), dtype=int)
D = csr_matrix((10, 10), dtype=int)

B2 = vstack((B, C))
#<10x2 sparse matrix of type '<type 'numpy.int32'>'
#        with 0 stored elements in COOrdinate format>

hstack((B2, D))
#<10x12 sparse matrix of type '<type 'numpy.int32'>'
#        with 0 stored elements in COOrdinate format>

Note that the output is a coo_matrix, which can be efficiently converted to the CSR or CSC formats.

like image 116
Saullo G. P. Castro Avatar answered Oct 20 '22 08:10

Saullo G. P. Castro


What you want to do isn't really what numpy or scipy understand as a reshape. But for your particular case, you can create a new CSR matrix reusing the data, indices and indptr from your original one, without copying them:

import scipy.sparse as sps

a = sps.rand(10000, 10000, density=0.01, format='csr')

In [19]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020), copy=True)
100 loops, best of 3: 6.26 ms per loop

In [20]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020), copy=False)
10000 loops, best of 3: 47.3 us per loop

In [21]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020))
10000 loops, best of 3: 48.2 us per loop

So if you no longer need your original matrix a, since the default is copy=False, simply do:

a = sps.csr_matrix((a.data, a.indices, a.indptr), shape=(10000, 10020))
like image 26
Jaime Avatar answered Oct 20 '22 08:10

Jaime