Why Does Squeeze Not Work On Sparse Arrays?
I have the following code: import numpy as np from scipy import sparse x = np.eye(3) print(x.sum(axis=1).shape) x = sparse.eye(3) print(x.sum(axis=1).shape) print(x.sum(axis=1).s
Solution 1:
In [1]: from scipy import sparse
In [2]: x = np.eye(3)
In [3]: x
Out[3]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
In [4]: x.shape
Out[4]: (3, 3)
In [5]: xs = sparse.eye(3)
In [6]: xs
Out[6]:
<3x3 sparse matrix of type'<class 'numpy.float64'>'
with 3 stored elements (1 diagonals) in DIAgonal format>
In [7]: print(xs)
(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
In [8]: xs.shape
Out[8]: (3, 3)
The np
sum produces an array, with one less dimension (unless you use keepdims
parameter)
In [9]: x.sum(axis=1)
Out[9]: array([1., 1., 1.])
sparse sum produces a np.matrix
object.
In [10]: xs.sum(axis=1)
Out[10]:
matrix([[1.],
[1.],
[1.]])
In [11]: _.shape
Out[11]: (3, 1)
np.matrix
is, by definition, always 2d. But it does have an A1
property which converts to ndarray
and applies squeeze.
In [12]: xs.sum(axis=1).A1
Out[12]: array([1., 1., 1.])
Sparse actually performs the row or column sum by matrix multiplication:
In [21]: xs*np.matrix(np.ones((3,1)))
Out[21]:
matrix([[1.],
[1.],
[1.]])
sparse matrix * np.matrix produces np.matrix
If sum
used ndarray
, the result would be an ndarray
, and squeezeable
In [22]: xs*np.ones((3,1))
Out[22]:
array([[1.],
[1.],
[1.]])
Note that I used *
(I could have used @); the sparse definition of multiply (e.g. dot) has priority.
In [23]: np.matrix(np.ones((1,3)))*xs
Out[23]: matrix([[1., 1., 1.]])
Post a Comment for "Why Does Squeeze Not Work On Sparse Arrays?"