gpt4 book ai didi

python - 将 LIL 稀疏矩阵的所有元素设置为零,同时保留其稀疏性

转载 作者:行者123 更新时间:2023-12-01 09:29:13 24 4
gpt4 key购买 nike

在我的代码中,我执行了一系列迭代。在每次迭代中,我在 sparse lil format 中使用相同的矩阵 A计算某事。 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)
....

最佳答案

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)

很容易设置datacsr矩阵为零,并保持稀疏性(直到 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>

但是对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)

关于python - 将 LIL 稀疏矩阵的所有元素设置为零,同时保留其稀疏性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50107592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com