title | author | date |
---|---|---|
Create a Sparse Matrix |
Damian Mingle |
06/07/2018 |
# Bring in libraries
import numpy as np
from scipy import sparse
# Dense matrix
matrix_dense = np.array([[0, 0],
[0, 1],
[3, 0]])
# Make a compressed sparse row matrix
matrix_sparse = sparse.csr_matrix(matrix_dense)
# Show the sparse matrix
print(matrix_sparse)
matrix_sparse
(1, 1) 1
(2, 0) 3
<3x2 sparse matrix of type '<class 'numpy.int32'>'
with 2 stored elements in Compressed Sparse Row format>