📜  如何在python代码示例中将密集矩阵转换为稀疏矩阵

📅  最后修改于: 2022-03-11 14:46:03.473000             🧑  作者: Mango

代码示例1
# dense to sparse
from numpy import array
from scipy.sparse import csr_matrix
# create dense matrix
A = array([[1, 0, 0, 1, 0, 0], [0, 0, 2, 0, 0, 1], [0, 0, 0, 2, 0, 0]])
print(A)
# convert to sparse matrix (CSR method)
S = csr_matrix(A)
print(S)
# reconstruct dense matrix
B = S.todense()
print(B)