Reputation: 386
How can I extract the main diagonal of a sparse matrix? The matrix is created in scipy.sparse
. I want equivalent of np.diagonal()
, but for sparse matrix.
Upvotes: 11
Views: 6589
Reputation: 231335
A sparse matrix has a diagonal
method:
M.diagonal()
http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.diagonal.html
The numpy diagonal is a little more powerful, allowing you to specify an off diagonal
M.A.diagonal(2)
Upvotes: 12