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

时间:2016-12-28 18:56:40

标签: python matrix scipy

我在python中有以下代码:

        public Chemicalsdetails GetChemicalDataGeneratedForMonth(string branchcode, string departmentnumber, string previousMonth, string currentMonth)
            {
                string result = string.Empty;
                result = _chemmeterprocessor.CopyPreviousMonthData(branchcode, departmentnumber, previousMonth, currentMonth);
                Chemicalsdetails objChem = null;

                List<Chemicaltransactiondto> objAllData = new List<Chemicaltransactiondto>();

                //****Check for "Success"
                if (result == "Success")
                {
                    //****Retrieve chemical data
                    objAllData = _chemmeterprocessor.GetAllChemicalEntries(branchcode, departmentnumber, currentMonth);
                    //****End Retrieve chemical data
                }

                //****Check for non-null data.
                if ((result == "Success") && (objAllData!=null))
                {
                    objChem = new Chemicalsdetails();

                    objChem.GetAllChemicalsInformation = objAllData;                
                }
                else
                {
                    Chemicalsdetails objNoData = new Chemicalsdetails();                 
                }

                return objChem; 
            }

前两张照片的输出是:

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)

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

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

如果我正确理解稀疏矩阵的概念,那就不应该这样了。它不应该显示零值,最后2个打印的输出应该是这样的:

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

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

1 个答案:

答案 0 :(得分: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])