在scipy.sparse.lil峎矩阵对象似乎未显式存储设置为0的值。其他稀疏矩阵,如csr_矩阵,则为。在
考虑以下示例:
In [1]: from scipy.sparse import lil_matrix
In [2]: import numpy as np
In [3]: x = lil_matrix((5, 5), dtype=np.float32)
In [4]: x[3, 3] = 0
In [5]: x
Out[5]:
<5x5 sparse matrix of type '<class 'numpy.float32'>'
with 0 stored elements in LInked List format>
这是不好的,因为有时在一个图的元素之间会有一个0的距离(例如,一个数据点的副本)。如果我把一个liléu矩阵传递给,例如。,scipy.sparse.csgraph.connected_components,它将检测不正确数量的连接组件,因为显式0被转换回“稀疏性”,因此被视为无限距离。在
我不能使用csr_矩阵,因为为其分配元素的效率非常低。但是,它将显式地存储0值,这与lil廑矩阵不同。将上述代码中的lil_matrix替换为csr_matrix,输出变为:
^{pr2}$有人知道如何将显式的0值存储在lil帴matrix对象中吗?在
谢谢。在
lil
__setitem__
使用编译的lil_fancy_set
函数。医生说:
^{pr2}$csr
矩阵有eliminate_zeros
方法:它还有
sum_duplicates
方法。在将coo
格式转换为csr
时使用,有助于从重叠的子矩阵创建矩阵。在相关问题 更多 >
编程相关推荐