Google News
logo
SciPy - Interview Questions
How to Work With Sparse Data
SciPy has a module, scipy.sparse that provides functions to deal with sparse data. There are two types of sparse matrices that we use :
 
CSC : Compressed Sparse Column. For efficient arithmetic, fast column slicing.
CSR : Compressed Sparse Row. For fast row slicing, faster matrix vector products
 
CSR Matrix : We can create CSR matrix by passing an arrray into function scipy.sparse.csr_matrix().
 
Create a CSR matrix from an array :
 
import numpy as np
from scipy.sparse import csr_matrix
arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2])
print(csr_matrix(arr))


Output :
 

(0, 5) 1
(0, 6) 1
(0, 8) 2

Advertisement