1import numpy as np
2from scipy.sparse import csr_matrix
3
4# create a 2-D representation of the matrix
5A = np.array([[1, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 1],\
6 [0, 0, 0, 2, 0, 0]])
7print("Dense matrix representation: \n", A)
8
9# convert to sparse matrix representation
10S = csr_matrix(A)
11print("Sparse matrix: \n",S)
12
13# convert back to 2-D representation of the matrix
14B = S.todense()
15print("Dense matrix: \n", B)