简体   繁体   English

scipy:如何获取每一行的所有非零值索引?

[英]scipy: how to get all non-zero value index for each row?

I can't find more info about scipy.sparse indexing except SciPy v0.11 Reference Guide, which says that 除了《 SciPy v0.11参考指南》,我找不到有关scipy.sparse索引的更多信息。

The lil_matrix class supports basic slicing and fancy indexing with a similar syntax to NumPy arrays.
. I have read numpy document about index, but I didn't understand it clearly, for example, 我已经阅读了有关索引的numpy文档,但是我不清楚,例如,

 Asp = sparse.lil_matrix((3,3)) Asp.setdiag(zeros(3)) Asp[0, 1:3] = 10 print Asp.todense() 

1. why the output is 1.为什么输出

 [[ 0. 10. 10.] [[0. 10. 10.]\n [ 0. 0. 0.] [0. 0. 0.]\n [ 0. 0. 0.]] [0. 0. 0.]] 

what does [0,1:3] meaning? [0,1:3]是什么意思? if I use 如果我用

 Asp[0, 1:2,3] = 10 

there's a error: 有一个错误:

 IndexError: invalid index IndexError:无效的索引 
, I don't know the reason. ,我不知道原因。

2.what's the fastest way to get all non-zero values for each row? 2.什么是最快获取每一行所有非零值的方法?

For your second question, use the nonzero() method. 对于第二个问题,请使用nonzero()方法。 I had to dig through the source to find it, since I couldn't find it in any of the reference documentation. 我必须仔细研究源代码才能找到它,因为在任何参考文档中都找不到。

def nonzero(self):
    """nonzero indices

    Returns a tuple of arrays (row,col) containing the indices
    of the non-zero elements of the matrix.

    Examples
    --------
    >>> from scipy.sparse import csr_matrix
    >>> A = csr_matrix([[1,2,0],[0,0,3],[4,0,5]])
    >>> A.nonzero()
    (array([0, 0, 1, 2, 2]), array([0, 1, 2, 0, 2]))

    """

what does [0,1:3] mean? [0,1:3]是什么意思?

That means: row 0, elements 1 to 3 (exclusive). 这意味着:第0行,元素13 (不包括)。 Since Numpy and Scipy use zero-based indices, row 0 is the first row and 1:3 denotes the first and second column. 由于Numpy和Scipy使用从零开始的索引,因此第0行是第一行,而1:3表示第一列和第二列。

Asp[0, 1:2,3] is invalid because you've got three indices, 0 , 1:2 and 3 . Asp[0, 1:2,3]无效,因为您有三个索引0 1:23 Matrices only have two axes. 矩阵只有两个轴。

This is all standard Numpy stuff; 这是所有标准的Numpy内容; read any good tutorial on that package. 阅读有关该软件包的任何优秀教程。

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

相关问题 如何快速获取numpy数组中非零值的索引? - How to get the index of non-zero value in a numpy array quickly? 在coo_matrix(稀疏稀疏矩阵)中,如何选择并返回每一行中的“随机列索引”(不包括该行中的非零索引)? - In coo_matrix (scipy sparse matrix), how can I select and return “random column index” in each row excluding non-zero indexes in that row? 为数据帧中的每一行获取最右边的非零值位置(非lambda方法) - Get rightmost non-zero value position for each row in dataframe (non lambda method) 如何从稀疏SciPy矩阵中获取非零值? - How to get non-zero values from sparse SciPy matrix? 将 Pandas DataFrame 中每一行的最后一个非零值转换为 0 - Convert the last non-zero value to 0 for each row in a pandas DataFrame 在Pandas DataFrame的每一行中查找第一个非零值 - Find First Non-zero Value in Each Row of Pandas DataFrame 如何从python中的列表中获取零和非零值的索引范围? - how to get index range for zero and non-zero value from list in python? 如何在矩阵的每一行中保持最小的非零值? - How to keep smallest non-zero value(s) in each row of a matrix? 从scipy稀疏矩阵的每一行中有效地选择随机非零列 - Efficiently select random non-zero column from each row of sparse matrix in scipy 获取 python 中列表中下一个非零值的索引 - Get the index of the next non-zero value in a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM