Sparse Matrix Legacy Interface (scipy.sparse)#

SciPy 2-D sparse matrix interface for numeric data.

Warning

The SciPy sparse package is switching to an array interface, compatible with NumPy arrays, from the older numpy.matrix interface. We recommend that you use the array objects (csr_array, coo_array, etc.).

When using the array interface, please note that:

  • x * y performs element-wise multiplication (just like NumPy arrays). To make code work with both arrays and matrices, use x @ y for matrix multiplication.

  • Operations, like sum, produce np.matrix for spmatrix while they produce np.array for sparray. Multiplication of np.matrix and np.array differ in the same way as between spmatrix and sparray.

  • Sparse arrays use array style slicing operations, returning scalars, or 1D or 2D sparse arrays according to the numpy standards for indexing. If you need always 2D results, use an appropriate index with sparse arrays. For example, A[:, i, None] or A[:, [i]].

The construction utilities (eye, kron, random, diags, etc.) have appropriate replacements (see Building sparse arrays).

For more information see Migration from spmatrix to sparray.

Sparse matrix classes#

bsr_matrix(arg1[, shape, dtype, copy, ...])

Block Sparse Row format sparse matrix.

coo_matrix(arg1[, shape, dtype, copy, maxprint])

A sparse matrix in COOrdinate format.

csc_matrix(arg1[, shape, dtype, copy, maxprint])

Compressed Sparse Column matrix.

csr_matrix(arg1[, shape, dtype, copy, maxprint])

Compressed Sparse Row matrix.

dia_matrix(arg1[, shape, dtype, copy, maxprint])

Sparse matrix with DIAgonal storage.

dok_matrix(arg1[, shape, dtype, copy, maxprint])

Dictionary Of Keys based sparse matrix.

lil_matrix(arg1[, shape, dtype, copy, maxprint])

Row-based LIst of Lists sparse matrix.

spmatrix()

This class provides a base class for all sparse matrix classes.

Building sparse matrices#

eye(m[, n, k, dtype, format])

Sparse matrix of chosen shape with ones on the kth diagonal and zeros elsewhere.

identity(n[, dtype, format])

Identity matrix in sparse format.

diags(diagonals[, offsets, shape, format, dtype])

Construct a sparse matrix from diagonals.

spdiags(data, diags[, m, n, format])

Return a sparse matrix from diagonals.

bmat(blocks[, format, dtype])

Build a sparse array or matrix from sparse sub-blocks.

random(m, n[, density, format, dtype, rng, ...])

Generate a sparse matrix of the given shape and density with randomly distributed values.

rand(m, n[, density, format, dtype, rng, ...])

Generate a sparse matrix of the given shape and density with uniformly distributed values.

To combine matrices use the same functions as for Combining arrays.

Identifying sparse matrices#

issparse(x)

Is x a sparse array or sparse matrix type?

isspmatrix(x)

Is x of a sparse matrix type?

isspmatrix_csc(x)

Is x of csc_matrix type?

isspmatrix_csr(x)

Is x of csr_matrix type?

isspmatrix_bsr(x)

Is x of a bsr_matrix type?

isspmatrix_lil(x)

Is x of lil_matrix type?

isspmatrix_dok(x)

Is x of dok_array type?

isspmatrix_coo(x)

Is x of coo_matrix type?

isspmatrix_dia(x)

Is x of dia_matrix type?