scipy coo_matrix 将一些小数字视为 0.0 但仍放入稀疏矩阵

问题描述 投票:0回答:1

我使用一些 python 和 numpy/scipy 代码并生成一个稀疏数组:

<COOrdinate sparse array of dtype 'float64'
    with 21 stored elements and shape (403, 196)>
  Coords    Values
  (206, 138)    0.0
  (206, 139)    0.23614037219163428
  (206, 140)    0.2366397154157327
  (206, 141)    0.236805402497099
  (206, 142)    0.0
  (207, 138)    0.3928269859331709
  (207, 139)    0.3939331263543298
  (207, 140)    0.3995270442982785
  (207, 141)    0.3821409243437723
  (207, 142)    0.3638856690042628
  (208, 138)    0.3935915394673921
  (208, 139)    0.3957499496341213
  (208, 140)    0.39550968408901105
  (208, 141)    0.38493115738953887
  (208, 142)    0.0
  (209, 138)    0.0
  (209, 139)    0.3917329284904307
  (209, 140)    0.39178317681678937
  (209, 141)    0.3796339720019004
  (210, 139)    0.0
  (210, 140)    0.40025290199002156

我期待的是:

<COOrdinate sparse array of dtype 'float64'
    with 21 stored elements and shape (403, 196)>
  Coords    Values
  (206, 139)    0.23614037219163428
  (206, 140)    0.2366397154157327
  (206, 141)    0.236805402497099
  (207, 138)    0.3928269859331709
  (207, 139)    0.3939331263543298
  (207, 140)    0.3995270442982785
  (207, 141)    0.3821409243437723
  (207, 142)    0.3638856690042628
  (208, 138)    0.3935915394673921
  (208, 139)    0.3957499496341213
  (208, 140)    0.39550968408901105
  (208, 141)    0.38493115738953887
  (209, 139)    0.3917329284904307
  (209, 140)    0.39178317681678937
  (209, 141)    0.3796339720019004
  (210, 140)    0.40025290199002156

我尝试打印:

np.set_printoptions(precision=100, suppress=True)
print(coo[0].toarray()[206,138])

如果我不在 jupyterlab 中写

0.0
,它会给我
np.float64(0.0)
print

但不是类似

1.14514e-20

之类的东西

我不禁要问:如果numpy认为这是0.0,为什么它显示在稀疏数组中,而不是空值?

python numpy scipy sparse-matrix
1个回答
0
投票

我询问矩阵是如何创建的原因是,根据方法不同,0 的处理方式也不同。

举例说明:

In [129]: from scipy import sparse

用一个小的新 0 和一个实际 0 制作一个稀疏矩阵:

In [130]: x = np.array([[1,2,1e-20],[3,0,4.1]]);x
Out[130]: 
array([[1.0e+00, 2.0e+00, 1.0e-20],
       [3.0e+00, 0.0e+00, 4.1e+00]])

使用

coo_matrix
构造函数:

In [131]: M = sparse.coo_matrix(x); M
Out[131]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in COOrdinate format>

In [132]: print(M)
  (0, 0)    1.0
  (0, 1)    2.0
  (0, 2)    1e-20
  (1, 0)    3.0
  (1, 2)    4.1

但是使用备用输入,指定所有元素及其坐标:

In [133]: M1 = sparse.coo_matrix((x.ravel(),([0,0,0,1,1,1],[0,1,2,0,1,2]))); M1
Out[133]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in COOrdinate format>
    
In [135]: print(M1)
  (0, 0)    1.0
  (0, 1)    2.0
  (0, 2)    1e-20
  (1, 0)    3.0
  (1, 1)    0.0
  (1, 2)    4.1

(1,1)点仍然存在。 它完全按照我提供的方式获取输入。

eliminate_zeros
可以通过
self.data!=0
掩蔽测试来摆脱这个 0。

In [137]: M1.eliminate_zeros(); M1
Out[137]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in COOrdinate format>

它并没有摆脱接近于零的1e-20。

© www.soinside.com 2019 - 2024. All rights reserved.