如何将显式的0值存储在scipy稀疏lil峎矩阵中?

2024-04-26 04:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

在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对象中吗?在

谢谢。在


Tags: 对象inimportnumpy元素距离示例np
1条回答
网友
1楼 · 发布于 2024-04-26 04:45:08

lil__setitem__使用编译的lil_fancy_set函数。医生说:

In [320]: sparse._csparsetools.lil_fancy_set?
Docstring:
Set multiple items to a LIL matrix.

Checks for zero elements and deletes them.

Parameters
     
M, N, rows, data
    LIL matrix data
i_idx, j_idx
    Indices of elements to insert to the new LIL matrix.
values
    Values of items to set.
Type:      builtin_function_or_method

csr矩阵有eliminate_zeros方法:

^{pr2}$

它还有sum_duplicates方法。在将coo格式转换为csr时使用,有助于从重叠的子矩阵创建矩阵。在

相关问题 更多 >