为什么挤压不适用于稀疏数组? [英] Why does squeeze not work on sparse arrays?

查看:56
本文介绍了为什么挤压不适用于稀疏数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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 没有按预期工作.我做错了什么?

It looks like squeeze is not working as intended. What am I doing wrong?

推荐答案

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 参数)

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.])

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

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 根据定义,总是二维的.但它确实有一个 A1 属性,它可以转换为 ndarray 并应用挤压.

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 实际上是通过矩阵乘法进行行或列求和:


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.]])

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

sparse matrix * np.matrix produces np.matrix

如果sum使用ndarray,结果将是一个ndarray,并且是可压缩的

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.]])

这篇关于为什么挤压不适用于稀疏数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆