【问题标题】:Why does squeeze not work on sparse arrays?为什么挤压不适用于稀疏数组?
【发布时间】:2020-01-28 08:15:13
【问题描述】:

我有以下代码:

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).squeeze().shape)

我得到以下输出:

(3,)
(3, 1)
(1, 3)

看起来squeeze 没有按预期工作。我做错了什么?

【问题讨论】:

  • 我没有使用过 scipy.sparse 但我很确定它处理的是 矩阵 而不是数组,它们是固定的二维对象。尝试使用 numpy 矩阵而不是数组进行比较。
  • @AndrasDeak 我需要使用稀疏矩阵来解决我的问题,因为它们太大了。这里的代码只是一个小例子。

标签: python numpy scipy


【解决方案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)

np sum 生成一个数组,维度少一(除非您使用keepdims 参数)

In [9]: x.sum(axis=1)                                                                            
Out[9]: array([1., 1., 1.])

稀疏求和产生一个np.matrix 对象。

In [10]: xs.sum(axis=1)                                                                          
Out[10]: 
matrix([[1.],
        [1.],
        [1.]])
In [11]: _.shape                                                                                 
Out[11]: (3, 1)

np.matrix,根据定义,总是 2d。但它确实有一个 A1 属性,可以转换为 ndarray 并应用挤压。

In [12]: xs.sum(axis=1).A1                                                                       
Out[12]: array([1., 1., 1.])

Sparse 实际上通过矩阵乘法来执行行或列求和:

In [21]: xs*np.matrix(np.ones((3,1)))                                                            
Out[21]: 
matrix([[1.],
        [1.],
        [1.]])

稀疏矩阵 * np.matrix 产生 np.matrix

如果sum 使用ndarray,则结果将是ndarray,并且是可挤压的

In [22]: xs*np.ones((3,1))                                                                       
Out[22]: 
array([[1.],
       [1.],
       [1.]])

请注意,我使用了*(我本来可以使用@);乘法的稀疏定义(例如点)具有优先权。

In [23]: np.matrix(np.ones((1,3)))*xs                                                            
Out[23]: matrix([[1., 1., 1.]])

【讨论】:

  • 我不知道A1,谢谢!所以,因为 np.matrix 总是 2D,所以挤压不起作用?
  • 这里squeeze 应用于np.matrix,而不是稀疏矩阵。但根据定义,稀疏矩阵也是 2d。 np.matrixndarray 的子类;稀疏矩阵是一个单独的类,但其行为类似于np.matrixnp.matrix 即将退出,但将稀疏矩阵切换为 ndarray 类似行为还有很长的路要走。
  • 稀疏求和实际上是通过矩阵乘法执行的 - 请参阅我的编辑。
猜你喜欢
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
相关资源
最近更新 更多