[英]Return diagonal elements of scipy sparse matrix
Given a scipy.sparse.csr.csr_matrix
, is there a quick way to return the elements on the diagonal? 给定
scipy.sparse.csr.csr_matrix
,有没有一种快速的方法来返回对角线上的元素?
The reason I would like to do this is to compute inv(D) A
, where D
is a diagonal matrix whose diagonal entries agree with A
( A
is my sparse matrix, guaranteed to have nonzeros on the diagonal). 我想这样做的原因是计算
inv(D) A
,其中D
是对角线矩阵,其对角线项与A
一致( A
是我的稀疏矩阵,保证对角线具有非零值)。
Use csr_matrix.diagonal() : 使用csr_matrix.diagonal() :
Returns the main diagonal of the matrix
返回矩阵的主对角线
Example: 例:
>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> mymat = csr_matrix((4, 4), dtype=np.int8)
>>> mymat.diagonal()
array([0, 0, 0, 0], dtype=int8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.