简体   繁体   English

稀疏矩阵:如何为每一行获得非零索引

[英]Sparse matrix: how to get nonzero indices for each row

I have an scipy CSR matrix and i want to get element column indices for each row. 我有一个scipy CSR矩阵,我想获得每一行的元素列索引。 My approach is: 我的方法是:

import scipy.sparse as sp
N = 100
d = 0.1
M = sp.rand(N, N, d, format='csr')

indM = [row.nonzero()[1] for row in M]

indM is what i need, it has the same number of row as M and looks like this: indM是我需要的,它与M的行数相同,如下所示:

[array([ 6,  7, 11, ..., 79, 85, 86]),
 array([12, 20, 25, ..., 84, 93, 95]),
...
 array([ 7, 24, 32, 40, 50, 51, 57, 71, 74, 96]),
 array([ 1,  4,  9, ..., 71, 95, 96])]

The problem is that with big matrices this approach looks slow. 问题是,对于大矩阵,这种方法看起来很慢。 Is there any way to avoid list comprehension or somehow speed this up? 有没有办法避免列表理解或以某种方式加快这一点?

Thank you. 谢谢。

You can simply use the indices and indptr attributes directly: 您可以直接使用indicesindptr属性:

import numpy
import scipy.sparse

N = 5
d = 0.3
M = scipy.sparse.rand(N, N, d, format='csr')
M.toarray()
# array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
#        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.30404632],
#        [ 0.63503713,  0.        ,  0.        ,  0.        ,  0.        ],
#        [ 0.68865311,  0.81492098,  0.        ,  0.        ,  0.        ],
#        [ 0.08984168,  0.87730292,  0.        ,  0.        ,  0.18609702]])

M.indices
# array([1, 2, 4, 3, 0, 1, 4], dtype=int32)
M.indptr
# array([0, 3, 4, 6, 6, 7], dtype=int32)

numpy.split(M.indices, M.indptr)[1:-1]
# [array([], dtype=int32),
#  array([4], dtype=int32),
#  array([0], dtype=int32),
#  array([0, 1], dtype=int32),
#  array([0, 1, 4], dtype=int32)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy为每一行找到稀疏矩阵的最小非零元素 - Scipy find minimum nonzero element of a sparse matrix for each row 如何有效地计算 scipy.csr 稀疏矩阵中非零索引的成对交集? - How to efficiently calculate pairwise intersection of nonzero indices in a scipy.csr sparse matrix? 给定行索引和列索引,如何设置稀疏矩阵的项? - How to set items of a sparse matrix given the row and column indices? 如何从稀疏矩阵中每行随机选择一个非零元素而在python中没有for循环 - How to randomly select one nonzero element per row from a sparse matrix with out for loop in python 如何从非零元素的索引字典创建稀疏 numpy 数组? - How to create a sparse numpy array from a dictionary of indices for nonzero elements? 在Python中的稀疏矩阵中按行查找非零条目的快速方法 - A fast way to find nonzero entries by row in a sparse matrix in Python scipy.sparse 矩阵:将行均值减去非零元素 - scipy.sparse matrix: subtract row mean to nonzero elements 如何获得稀疏矩阵数据数组的对角元素的索引 - How to get indices of diagonal elements of a sparse matrix data array 如何在numpy矩阵的每一行中获取N个最大值的索引矩阵? - How to get matrix of indices of N maximum values in each row of an numpy matrix? Pytorch:如何在二维张量的每一行中找到第一个非零元素的索引? - Pytorch: How can I find indices of first nonzero element in each row of a 2D tensor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM