相关疑难解决方法(0)

scipy.sparse:将行设置为零

假设我有一个CSR格式的矩阵,将行(或行)设置为零的最有效方法是什么?

以下代码运行缓慢:

A = A.tolil()
A[indices, :] = 0
A = A.tocsr()
Run Code Online (Sandbox Code Playgroud)

我不得不转换为scipy.sparse.lil_matrix因为CSR格式似乎既不支持花哨的索引,也不支持将值设置为切片.

row scipy sparse-matrix

8
推荐指数
1
解决办法
3640
查看次数

在scipy中删除/设置稀疏矩阵的非零对角元素

说我想从a中删除对角线scipy.sparse.csr_matrix.这样做有效吗?我在sparsetools模块中看到有C返回对角线的功能.

基于其他SO答案在这里这里我目前的方法如下:

def csr_setdiag_val(csr, value=0):
    """Set all diagonal nonzero elements
    (elements currently in the sparsity pattern)
    to the given value. Useful to set to 0 mostly.
    """
    if csr.format != "csr":
        raise ValueError('Matrix given must be of CSR format.')
    csr.sort_indices()
    pointer = csr.indptr
    indices = csr.indices
    data = csr.data
    for i in range(min(csr.shape)):
        ind = indices[pointer[i]: pointer[i + 1]]
        j =  ind.searchsorted(i)
        # matrix has only elements up until diagonal …
Run Code Online (Sandbox Code Playgroud)

python scipy sparse-matrix diagonal

6
推荐指数
1
解决办法
1761
查看次数

标签 统计

scipy ×2

sparse-matrix ×2

diagonal ×1

python ×1

row ×1