diagonal matrix numpy python scipy
Return diagonal elements of scipy sparse matrix
https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.lil_matrix.html
给定scipy.sparse.csr.csr_matrix,有没有一种快速的方法可以返回对角线上的元素?
我想这样做的原因是计算inv(D) A,其中D是对角线矩阵,其对角线条目与A一致(A是我的稀疏矩阵,保证对角线具有非零值)。
使用csr_matrix.diagonal():
Returns the main diagonal of the matrix
例:1
2
3
4
5
>>> 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)
在示例中,mymat是一个密集数组(即toarray() 😃)。 稀疏的对角线不像密集的对角线那样普遍。