I have the sum of a csr_matrix over one dimension, which returns a 1 dimensional vector. This is by default of the type numpy.matrix with shape (1, N). However, I want to represent this by a numpy.array with shape (N,). The following works:
>>> import numpy as np; import scipy.sparse as sparse
>>> a = sparse.csr_matrix([[0,1,0,0],[1,0,0,0],[0,1,2,0]])
>>> a
Out[15]:
<3x4 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements in Compressed Sparse Row format>
>>> a.todense()
Out[16]:
matrix([[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 1, 2, 0]], dtype=int64)
>>> a.sum(axis=0)
Out[17]: matrix([[1, 2, 2, 0]], dtype=int64)
>>> np.array(a.sum(axis=0)).ravel()
Out[18]: array([1, 2, 2, 0], dtype=int64)
However, this last step seems a bit overkill for a transformation from a numpy matrix to numpy array. Is there a function that I am missing that can do this for me? It shall pass the following unit test.
def test_conversion(self):
a = sparse.csr_matrix([[0,1,0,0],[1,0,0,0],[0,1,2,0]])
r = a.sum(axis=0)
e = np.array([1, 2, 2, 0])
np.testing.assert_array_equal(r, e)
aitself is a sparse matrix, the sum is anp.matrix. Shortcuts like.A1that work with the later don't work on the sparse one.