首页 > 解决方案 > 将 LIL 稀疏矩阵的所有元素设置为零,同时保持其稀疏性

问题描述

在我的代码中,我执行了一系列迭代。A在每次迭代中,我使用相同的稀疏lil格式矩阵来计算一些东西。的稀疏模式A是先验已知的并且不会改变。特别是,只有数字对角线-7:7包含非零元素,所有其他条目始终为零。

在每次迭代的请求中,我想将所有条目设置A为零。是否可以在不改变稀疏模式的情况下做到这一点?(例如列表中非零元素的数量)。这是表演所需要的。

这是我的代码的相关部分。每个都setdiag(0,i)减少了非零元素的数量,从而改变了稀疏模式。

#loop through iterations
for j in range(0,100):

     #Set all the non-zero entries to zero
     for i in range(-7,8):
            A.setdiag(0,i)

     #perform some computations:
     A.setdiag(23,0)
     ....

标签: pythonscipysparse-matrix

解决方案


In [178]: M = sparse.random(10,10,.2,'lil')
In [179]: M.data
Out[179]: 
array([list([0.10547901096204515, 0.7773041836996356, 0.906367486659326]),
       list([0.5758389931282252]),
       list([0.5346741809135753, 0.42524140314511094, 0.9750432813270062]),
       list([0.3165061782270727, 0.07300201792370353]),
       list([0.14325780997849935, 0.7047922399412353]),
       list([0.4598433233007516]),
       list([0.49436959373252576, 0.4704056215324637, 0.3527714631535005, 0.8823332112975898, 0.3518348016978091]),
       list([]), list([0.9792362001477255]),
       list([0.3205231173252291, 0.0465534963642843])], dtype=object)
In [180]: M.rows
Out[180]: 
array([list([2, 7, 8]), list([6]), list([1, 3, 6]), list([6, 7]),
       list([3, 6]), list([7]), list([2, 4, 5, 7, 9]), list([]),
       list([2]), list([5, 8])], dtype=object)

很容易将data矩阵csr的 设置为零,并保持稀疏性(直到eliminate_zeros运行)。

In [182]: Mc.data
Out[182]: 
array([0.10547901, 0.77730418, 0.90636749, 0.57583899, 0.53467418,
       0.4252414 , 0.97504328, 0.31650618, 0.07300202, 0.14325781,
       0.70479224, 0.45984332, 0.49436959, 0.47040562, 0.35277146,
       0.88233321, 0.3518348 , 0.9792362 , 0.32052312, 0.0465535 ])
In [183]: Mc.data[:]=0
In [184]: Mc.A
Out[184]: 
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
In [185]: Mc
Out[185]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 20 stored elements in Compressed Sparse Row format>
In [186]: Mc.eliminate_zeros()
In [187]: Mc
Out[187]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 0 stored elements in Compressed Sparse Row format>

但是对 an 做同样的事情lil需要对data数组进行迭代,并将每个列表替换为对应的 0 列表。

In [193]: M = sparse.random(10,10,.2,'lil')
In [194]: M
Out[194]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 20 stored elements in LInked List format>
In [195]: for i in range(M.shape[0]):
     ...:     M.data[i] = np.zeros(len(M.data[i])).tolist()
     ...:     
In [196]: M
Out[196]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 20 stored elements in LInked List format>
In [197]: M.data
Out[197]: 
array([list([0.0]), list([0.0]), list([0.0]), list([0.0]),
       list([0.0, 0.0, 0.0]), list([0.0, 0.0, 0.0, 0.0]),
       list([0.0, 0.0]), list([0.0, 0.0, 0.0, 0.0, 0.0]), list([0.0]),
       list([0.0])], dtype=object)

推荐阅读