Storage Schemes#
Sparse Array Classes#
There are seven sparse array types in scipy.sparse:
each suitable for some tasks
many employ sparsetools C++ module by Nathan Bell
assume the following is imported:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
warning for Numpy users:
passing a sparse array object to NumPy functions that expect ndarray/matrix does not work. Use sparse functions.
the older csr_matrix classes use ‘*’ for matrix multiplication (dot product) and ‘A.multiply(B)’ for elementwise multiplication.
the newer csr_array uses ‘@’ for dot product and ‘*’ for elementwise multiplication
sparse arrays can be 1D or 2D, but not nD for n > 2 (unlike Numpy arrays).
Common Methods#
all scipy.sparse array classes are subclasses of
sparray
default implementation of arithmetic operations
always converts to CSR
subclasses override for efficiency
shape, data type, set/get
indices of nonzero values in the array
format conversion, interaction with NumPy (
toarray()
)…
attributes:
mtx.T
- transpose (same as mtx.transpose())mtx.real
- real part of complex matrixmtx.imag
- imaginary part of complex matrixmtx.size
- the number of nonzeros (same as self.getnnz())mtx.shape
- the number of rows and columns (tuple)
data and indices usually stored in 1D NumPy arrays
Summary#
format |
matrix * vector |
get item |
fancy get |
set item |
fancy set |
solvers |
note |
---|---|---|---|---|---|---|---|
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 |
COO |
sparsetools |
. |
. |
. |
. |
iterative |
has data array, facilitates fast conversion |
DIA |
sparsetools |
. |
. |
. |
. |
iterative |
has data array, specialized |
LIL |
via CSR |
yes |
yes |
yes |
yes |
iterative |
arithmetic via CSR, incremental construction |
DOK |
Python |
yes |
one axis only |
yes |
yes |
iterative |
O(1) item access, incremental construction, slow arithmetic |