SciPy Sparse Data
Learn all about SciPy Sparse Data in this comprehensive tutorial.
- •Sparse data is data that has mostly unused elements (elements that don't carry any information ).
- •SciPy has a module, scipy.
- •We can create CSR matrix by passing an arrray into function scipy.
- •Viewing stored data (not the zero items) with the data property:
What is Sparse Data
Sparse data is data that has mostly unused elements (elements that don't carry any information ).
It can be an array like this one:
[1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0]
In scientific computing, when we are dealing with partial derivatives in linear algebra we will come across sparse data.
How to Work With Sparse Data
SciPy has a module, scipy.sparse that provides functions to deal with sparse data.
There are primarily 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
We will use the CSR matrix in this tutorial.
CSR Matrix
We can create CSR matrix by passing an arrray into function scipy.sparse.csr_matrix().
The example above returns:
Sparse Matrix Methods
Viewing stored data (not the zero items) with the data property:
Counting nonzeros with the count_nonzero() method:
Removing zero-entries from the matrix with the eliminate_zeros() method:
Eliminating duplicate entries with the sum_duplicates() method:
Converting from csr to csc with the tocsc() method:
Module quiz
2 questionsWhich of the following is true about SciPy Sparse Data?
What is the most common pitfall when working with SciPy Sparse Data?
Answer all questions to submit.