scipy矩阵在更新其值后不会更新

scipy矩阵在更新其值后不会更新

本文介绍了CSR scipy矩阵在更新其值后不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有以下代码:

I have the following code in 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)

前两张照片的输出为:

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

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

The code is changing the value of the same index (i==j) and setting the value to zero.After executing the loops then the output of the last 2 prints is:

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

如果我正确地理解了稀疏矩阵的概念,则情况并非如此.它不应向我显示零值,并且最后两张打印的输出应如下所示:

If I understand the concept of sparse matrices correctly, it should not be the case. It should not show me the zero values and the output of last 2 prints should be like this:

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

有人对此有解释吗?我在做错什么吗?

Does anyone have explanation for this? Am I doing something wrong?

推荐答案

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

Yes, you are trying to change elements of the matrix one by one. :)

好的,它确实可以那样工作,但是如果您以其他方式更改设置(将0设置为非零),则会收到效率警告".

Ok, it does work that way, though if you changed things the other way (setting a 0 to nonzero) you will get an Efficiency warning.

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

To keep your kind of change fast, it only changes the value in the M.data array, and does not recalculate the indices. You have to invoke a separate csr_matrix.eliminate_zeros method the clean up the matrix. To get best speed call this once at the end of the loop.

有一个 csr_matrix.setdiag 方法,可让您一次调用即可设置整个对角线.它仍然需要清理.

There is a csr_matrix.setdiag method that lets you set the whole diagonal with one call. It still needs the cleanup.

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

这篇关于CSR scipy矩阵在更新其值后不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:13