简体   繁体   English

Python Scipy如何从csr_matrix遍历上/下三角部分非零

[英]Python Scipy How to traverse upper/lower trianglar portion non-zeros from csr_matrix

I have a very sparse matrix(similarity matrix) with dimensions 300k * 300k. 我有一个非常稀疏的矩阵(相似性矩阵),尺寸为300k * 300k。 In order to find out the relatively greater similarities between users, I only need upper/lower triangular portion of the matrix. 为了找出用户之间相对较大的相似性,我只需要矩阵的上/下三角部分。 So, how to get the coordinates of users with value larger than a threshold in an efficient way? 那么,如何有效地获取值大于阈值的用户坐标呢? Thanks. 谢谢。

How about 怎么样

sparse.triu(M)

If M is 如果M

In [819]: M.A
Out[819]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)

In [820]: sparse.triu(M).A
Out[820]: 
array([[0, 1, 2],
       [0, 4, 5],
       [0, 0, 8]], dtype=int32)

You may need to construct a new sparse matrix, with just nonzeros above the threshold. 您可能需要构造一个新的稀疏矩阵,其中非零值仅高于阈值。

In [826]: sparse.triu(M>2).A
Out[826]: 
array([[False, False, False],
       [False,  True,  True],
       [False, False,  True]], dtype=bool)

In [827]: sparse.triu(M>2).nonzero()
Out[827]: (array([1, 1, 2], dtype=int32), array([1, 2, 2], dtype=int32))

Here's the code for triu : 这是triu的代码:

def triu(A, k=0, format=None):
    A = coo_matrix(A, copy=False)
    mask = A.row + k <= A.col
    row = A.row[mask]
    col = A.col[mask]
    data = A.data[mask]
    return coo_matrix((data,(row,col)), shape=A.shape).asformat(format)

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

相关问题 (Python Scipy)如何展平一个csr_matrix并将其附加到另一个csr_matrix? - (Python Scipy) How to flatten a csr_matrix and append it to another csr_matrix? 如何在scipy中创建评级csr_matrix? - How to create a ratings csr_matrix in scipy? 如何保留 SciPy 稀疏矩阵 CSR_Matrix 中的插入顺序? - How to preserve order of insertion in SciPy Sparse Matrix CSR_Matrix? Matlab稀疏到Python scipy csr_matrix的转换 - Conversion of Matlab sparse to Python scipy csr_matrix 在Python中有效地找到scipy / numpy中非零的间隔? - efficiently finding the interval with non-zeros in scipy/numpy in Python? 来自表示为集合列表的多个向量的scipy csr_matrix - scipy csr_matrix from several vectors represented as list of sets 将 maxtrix 从 scipy.sparse.identity 分配给 csr_matrix - Assigning maxtrix from scipy.sparse.identity to csr_matrix python:将(字符串)集列表转换为 scipy csr_matrix - python: convert a list of (string) sets to a scipy csr_matrix 如何在Scipy Python稀疏矩阵中实现CSR_Matrix的循环置换(左移和右移)? - How to implement Circular Permutation (left and right shift) of CSR_Matrix in Scipy Python Sparse Matrices? 如何使用 Python (可能还有 Scipy)估计一个巨大的、稀疏的 csr_matrix 的等级? - How to estimate the rank of a huge, sparse csr_matrix using Python (and probably Scipy)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM