SciPy Utilities

Learn about sparse matrix operations, linear and spline interpolation, and curve fitting in SciPy.

Sparse matrices

A sparse matrix is a matrix in which most of the elements are zero. By contrast, if most of the elements are nonzero, then the matrix is considered dense. The number of zero-valued elements divided by the total number of elements—for example, mnm*n for an m×nm × n matrix—is called the sparsity of the matrix.

Sparse matrices are incredibly useful in scientific computing, where they can provide significant computational and storage efficiencies. Many large-scale systems give rise to mathematical models that operate with sparse matrices. For example, the adjacency matrix of a graph is sparse if the graph has only a small number of edges. Similarly, in matrix factorization-based recommendation engines, the user-item rating matrices are sparse because not all users have rated all movies in the corpus.

In SciPy, there are several types of sparse matrices available.

  • coo_matrix: This is a sparse matrix in a Coordinate (COO) format.

  • csr_matrix: This is a Compressed Sparse Row (CSR) format.

  • csc_matrix: This is a Compressed Sparse Column (CSC) format.

The COO format

Here are a few characteristics of the COO sparse matrix format:

  • In the COO format, a matrix is represented using three separate arrays of equal length. The first array represents the row indexes, the second array represents the column indexes, and the third array represents the values of the nonzero elements.

  • The COO format is particularly useful for fast conversion among different formats and is especially suited for incrementally constructing sparse matrices.

  • However, the COO format does not directly support arithmetic operations or slicing.

The code below shows how to use SciPy to define a sparse matrix in the COO format and perform basic arithmetic operations:

Get hands-on with 1200+ tech skills courses.