Scipy稀疏矩阵CSR返回NaN在0.0/1.0上。

4
我在scipy.sparse.csr_matrix中发现了一种意外的行为,这对我来说似乎是一个错误。有人能确认这不是正常的吗?我不是稀疏结构专家,因此可能会误解正确的用法。
>>> import scipy.sparse
>>> a=scipy.sparse.csr_matrix((1,1))
>>> b=scipy.sparse.csr_matrix((1,1))
>>> b[0,0]=1
/home/marco/anaconda3/envs/py35/lib/python3.5/site-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
>>> a/b
matrix([[ nan]])

另一方面,numpy可以正确处理这个问题:
>>> import numpy as np
>>> a=np.zeros((1,1))
>>> b=np.ones((1,1))
>>> a/b
array([[ 0.]])

谢谢


1
你尝试过使用(a/b).toarray()吗? - Thiago Baldim
我觉得这是一个bug。 - Warren Weckesser
(a/b).tolist() 返回 [[nan]]a/b 的类型为矩阵,因此没有 toarraytodense 方法。 - marcotama
3
已提交错误报告:https://github.com/scipy/scipy/issues/6401。 - marcotama
1个回答

2
对于稀疏矩阵/稀疏矩阵,
在scipy/sparse/compressed.py中,
    if np.issubdtype(r.dtype, np.inexact):
        # Eldiv leaves entries outside the combined sparsity
        # pattern empty, so they must be filled manually. They are
        # always nan, so that the matrix is completely full.
        out = np.empty(self.shape, dtype=self.dtype)
        out.fill(np.nan)
        r = r.tocoo()
        out[r.row, r.col] = r.data
        out = np.matrix(out)

这个部分解释了这个操作。请尝试使用稍大一点的矩阵进行测试。
In [69]: a=sparse.csr_matrix([[1.,0],[0,1]])
In [70]: b=sparse.csr_matrix([[1.,1],[0,1]])
In [72]: (a/b)
Out[72]: 
matrix([[  1.,  nan],
        [ nan,   1.]])

无论 a 在哪些位置上有 0(即无稀疏值),除法的结果都是 nan。它返回一个密集矩阵,并填充 nan
如果没有这段代码,稀疏的逐元素除法会产生一个带有那些“空”的非对角线插槽的稀疏矩阵。
In [73]: a._binopt(b,'_eldiv_')
Out[73]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [74]: a._binopt(b,'_eldiv_').A
Out[74]: 
array([[ 1.,  0.],
       [ 0.,  1.]])

反之可能更有启发性。
In [76]: b/a
Out[76]: 
matrix([[  1.,  inf],
        [ nan,   1.]])
In [77]: b._binopt(a,'_eldiv_').A
Out[77]: 
array([[  1.,  inf],
       [  0.,   1.]])

看起来综合稀疏模式由分子决定。在进一步的测试中,经过eliminate_zeros处理后,它看起来像这样。

In [138]: a1=sparse.csr_matrix(np.ones((2,2)))
In [139]: a1
Out[139]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [140]: a1[0,1]=0
In [141]: a1
Out[141]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [142]: a1/b
Out[142]: 
matrix([[  1.,  nan],
        [ inf,   1.]])

是的,那就是问题的原因。我已经在这里提交了一个修复:https://github.com/scipy/scipy/pull/6405 - perimosocordiae

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接