Python Tutorial
SciPy Sparse Data
A sparse matrix stores only non-zero values. Use CSR when most entries are zero.
CSR Matrix
import numpy as np
from scipy.sparse import csr_matrix
arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2])
mat = csr_matrix(arr)
print(mat)
print(mat.data) # [1 1 2]
print(mat.count_nonzero())From a 2D Array
dense = np.array([[0, 0, 1], [1, 0, 2], [0, 0, 0]])
print(csr_matrix(dense))
print(csr_matrix(dense).tocsc()) # column-oriented📘 Real-World Deep Dive
Knowing <strong>SciPy Sparse (SciPy)</strong> well is what turns SciPy from a curiosity into a daily tool — you'll reach for it in nearly every real project.
Real-Life Scenario
An end-to-end usage of SciPy Sparse that you'd actually see in a data pipeline or analytics notebook.
Real-Life Example
import numpy as np
from scipy.sparse import csr_matrix
M = csr_matrix(np.eye(5))
print(M.toarray())
print("density:", M.nnz / (5*5))Expected Output
(see source)Common mistakes
- Many SciPy functions take
methodstrings with subtle spelling differences ("trust-constr"vs."trust-constr") — readscipy.optimize.least_squaresdocs. - Sparse matrices need explicit conversion to dense (
toarray()) before being fed to functions that don't acceptscipy.sparse. scipy.signalfunctions often return arrays whose length differs from input — always inspectlen(out)defensively.- Treating SciPy Sparse as a black box without reading the docs — the API has subtle defaults that bite when you scale.
🚀 Performance & Best Practices
- Use vectorised
scipy.statsdistributions instead of looping per-sample for large parametric studies. scipy.sparse.csr_matrixis the right format for arithmetic;csc_matrixis right for slicing columns.- Prefer Cython/Numba (or NumPy ufuncs) over Python loops inside SciPy callbacks (e.g.
odeint). - When working with SciPy, prefer vectorised / batched operations over Python loops.
🧪 Try It Yourself
- Reproduce the snippet on a representative slice of your own data.
- Profile the snippet with
cProfileortimeitand find the single biggest improvement. - Generalise the snippet into a small, reusable function you can drop into future projects.