SciPy Sparse Matrix

The sparse matrix allows the data structure to store large sparse matrices, and provide the functionality to perform complex matrix computations. In simple words, suppose you have a 2-D matrix with hundreds of elements, where only a few of them contain a non-zero value. When sorting this matrix using the sorting approach, we would waste a lot of space for zeros.

The sparse data structure allows us to store only non-zero values assuming the rest of them are zeros.

Sparse matrix types in SciPy

There are various ways to represent a sparse matrix; SciPy provides seven of them.

  • Block Sparse Row matrix (BSR)
  • Coordinate list matrix (COO)
  • Compressed Sparse Column matrix (CSC)
  • Compressed Sparse Row matrix (CSR)
  • Sparse matrix with Diagonal storage (DIA)
  • Dictionary Of Keys based sparse matrix (DOK)
  • Row-based linked list sparse matrix (LIL)

Consider the following example:

Output:

[[0. 1. 0. 0. 0.]
 [2. 0. 0. 0. 0.]
 [3. 0. 4. 0. 0.]
 [0. 0. 5. 0. 0.]
 [0. 0. 0. 6. 0.]]
The data is [1. 2. 3. 4. 5. 6.]
Cell which consits the non-zero values [0 1 2 4 5 6]

Next TopicSciPy Spatial




Latest Courses