1# dense to sparse
2from numpy import array
3from scipy.sparse import csr_matrix
4# create dense matrix
5A = array([[1, 0, 0, 1, 0, 0], [0, 0, 2, 0, 0, 1], [0, 0, 0, 2, 0, 0]])
6print(A)
7# convert to sparse matrix (CSR method)
8S = csr_matrix(A)
9print(S)
10# reconstruct dense matrix
11B = S.todense()
12print(B)