b.A1
will do the job.
In [83]: A
Out[83]:
<2x2 sparse matrix of type '<class 'numpy.float64'>'
with 0 stored elements in Compressed Sparse Row format>
In [84]: A.A
Out[84]:
array([[ 0., 0.],
[ 0., 0.]])
In [85]: b=A.sum(axis=0)
In [86]: b
Out[86]: matrix([[ 0., 0.]])
In [87]: b.A1
Out[87]: array([ 0., 0.])
In [88]: A.A.sum(axis=0) # another way
Out[88]: array([ 0., 0.])
You can up vote this, or add to my top grossing answer here: Numpy matrix to array :)
A
is a sparse matrix. Sparse sum is performed with a matrix product (an appropriate matrix of 1s). The result is a dense matrix.
Sparse matrix has a toarray()
method, with a .A
shortcut.
Dense matrix also has those, but it also has a .A1
(poorly documented - hence all my hits), which flattens as well.
The doc for A1
:
Return `self` as a flattened `ndarray`.
Equivalent to ``np.asarray(x).ravel()``
In fact the code is
return self.__array__().ravel()
====================
Is MATLAB b(:)
really the equivalent?
A(:)
is all the elements of A, regarded as a single column.
If I read that correctly, the numpy
equivalent is a transpose, or b.ravel().T
. The shape would be (2,1). But in MATLAB a column matrix is the simplest form of matrix.
In [94]: b.T
Out[94]:
matrix([[ 0.],
[ 0.]])
(I'm an old MATLAB programmer, with Octave on my standby computer. And a copy of 3.5 on some old Windows disk. :) ).