CSR Scipy Matrix更新其值后不会更新



我在python中具有以下代码:

import numpy as np
from scipy.sparse import csr_matrix
M = csr_matrix(np.ones([2, 2],dtype=np.int32))
print(M)
print(M.data.shape)
for i in range(np.shape(mat)[0]):
    for j in range(np.shape(mat)[1]):
        if i==j:
            M[i,j] = 0
print(M)
print(M.data.shape)

前2印刷的输出是:

  (0, 0)    1
  (0, 1)    1
  (1, 0)    1
  (1, 1)    1
(4,)

代码正在更改相同索引的值(i == j),并将值设置为零。执行循环后,最后两个打印的输出为:

  (0, 0)    0
  (0, 1)    1
  (1, 0)    1
  (1, 1)    0
(4,)

如果我正确理解稀疏矩阵的概念,则不应该这样。它不应该向我显示零值,最后2印刷品的输出应如下:

  (0, 1)    1
  (1, 0)    1
(2,)

有人对此有解释吗?我做错了吗?

是的,您正在尝试一一更改矩阵的元素。:)

好吧,它确实有效,尽管如果您以另一种方式更改事物(将0设置为零为零),您将获得效率警告。

要保持快速的更改,它只会更改M.data数组中的值,并且不会重新计算索引。您必须调用单独的csr_matrix.eliminate_zeros方法清理矩阵。要获得最佳速度,请在循环结束时致电一次。

有一个CSR_MATRIX.SETDIAG方法,可以让您用一个呼叫设置整个对角线。它仍然需要清理。

In [1633]: M=sparse.csr_matrix(np.arange(9).reshape(3,3))
In [1634]: M
Out[1634]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'
    with 8 stored elements in Compressed Sparse Row format>
In [1635]: M.A
Out[1635]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)
In [1636]: M.setdiag(0)
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
In [1637]: M
Out[1637]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'
    with 9 stored elements in Compressed Sparse Row format>
In [1638]: M.A
Out[1638]: 
array([[0, 1, 2],
       [3, 0, 5],
       [6, 7, 0]])
In [1639]: M.data
Out[1639]: array([0, 1, 2, 3, 0, 5, 6, 7, 0])
In [1640]: M.eliminate_zeros()
In [1641]: M
Out[1641]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'
    with 6 stored elements in Compressed Sparse Row format>
In [1642]: M.data
Out[1642]: array([1, 2, 3, 5, 6, 7])