Sparse data refers to data with mostly unused elements (elements that do not carry meaningful information).
For example, it can be an array like this:
[1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0]
Sparse Data: is a data set where most of the item values are zero. Dense Array: is the opposite of a sparse array: most of the values are not zero. |
In scientific computing, sparse data often arises when working with partial derivatives in linear algebra.
SciPy provides the scipy.sparse
module, which includes functions for working with sparse data.
The two primary types of sparse matrices are:
In this tutorial, we will use the CSR matrix.
A CSR matrix can be created by passing an array to the scipy.sparse.csr_matrix() function.
Construct a CSR matrix from an array:
import numpy as np arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2]) print(csr_matrix(arr)) |
The above example outputs:
(0, 5) 1 (0, 6) 1 (0, 8) 2 |
From the result, we observe three non-zero elements: