2.5.2. Storage Schemes¶
- seven sparse matrix types in scipy.sparse:
- csc_matrix: Compressed Sparse Column format
- csr_matrix: Compressed Sparse Row format
- bsr_matrix: Block Sparse Row format
- lil_matrix: List of Lists format
- dok_matrix: Dictionary of Keys format
- coo_matrix: COOrdinate format (aka IJV, triplet format)
- dia_matrix: DIAgonal format
each suitable for some tasks
many employ sparsetools C++ module by Nathan Bell
assume the following is imported:
>>> import numpy as np >>> import scipy.sparse as sps >>> import matplotlib.pyplot as plt
- warning for NumPy users:
the multiplication with ‘*’ is the matrix multiplication (dot product)
- not part of NumPy!
- passing a sparse matrix object to NumPy functions expecting ndarray/matrix does not work
2.5.2.1. Common Methods¶
- all scipy.sparse classes are subclasses of
spmatrix
- default implementation of arithmetic operations
- always converts to CSR
- subclasses override for efficiency
shape, data type set/get
nonzero indices
format conversion, interaction with NumPy (toarray(), todense())
...
- all scipy.sparse classes are subclasses of
- attributes:
- mtx.A - same as mtx.toarray()
- mtx.T - transpose (same as mtx.transpose())
- mtx.H - Hermitian (conjugate) transpose
- mtx.real - real part of complex matrix
- mtx.imag - imaginary part of complex matrix
- mtx.size - the number of nonzeros (same as self.getnnz())
- mtx.shape - the number of rows and columns (tuple)
data usually stored in NumPy arrays
2.5.2.2. Sparse Matrix Classes¶
2.5.2.3. Summary¶
format | matrix * vector | get item | fancy get | set item | fancy set | solvers | note |
---|---|---|---|---|---|---|---|
DIA | sparsetools | . | . | . | . | iterative | has data array, specialized |
LIL | via CSR | yes | yes | yes | yes | iterative | arithmetics via CSR, incremental construction |
DOK | python | yes | one axis only | yes | yes | iterative | O(1) item access, incremental construction |
COO | sparsetools | . | . | . | . | iterative | has data array, facilitates fast conversion |
CSR | sparsetools | yes | yes | slow | . | any | has data array, fast row-wise ops |
CSC | sparsetools | yes | yes | slow | . | any | has data array, fast column-wise ops |
BSR | sparsetools | . | . | . | . | specialized | has data array, specialized |