Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Practice and Review

SciPy Cheat Sheet

A one-page SciPy cheat sheet to see which function to use for linear algebra, optimization, integration, statistics, signals, sparse data and more, with live examples.

This page condenses the whole SciPy course into tables you can scan in a few minutes. It answers one question: I know what I want to compute, which function do I call? Each section is a reminder, not an explanation. The links under every table lead to the lesson that teaches the topic properly, with the reasoning behind the defaults.

Two live examples near the top and bottom run a tiny tour of every major submodule, so you can see the typical call shape (inputs in, result out) for each one. Everything else is read-only reference.

flowchart TD
    A["What is my problem?"] --> B["Solve equations<br/>or factor a matrix"]
    A --> C["Find a minimum,<br/>root or best fit"]
    A --> D["Integrate or<br/>simulate change"]
    A --> E["Describe or test<br/>random data"]
    A --> F["Process a signal,<br/>image or graph"]
    B --> B1["scipy.linalg<br/>scipy.sparse.linalg"]
    C --> C1["scipy.optimize"]
    D --> D1["scipy.integrate"]
    E --> E1["scipy.stats"]
    F --> F1["scipy.signal, fft<br/>ndimage, csgraph, spatial"]

Start from the problem, then jump to the submodule that owns it.

Imports and conventions

Python
import numpy as np
from scipy import linalg, optimize, integrate, interpolate, stats
from scipy import signal, fft, sparse, spatial, ndimage, special, constants
from scipy.sparse import linalg as splinalg, csgraph
Convention What to remember
Arrays in, arrays out Almost everything accepts NumPy arrays and returns NumPy arrays.
Result objects minimize, solve_ivp, linprog and friends return an object: read .success, .message, .x or .y.
Tolerances Most iterative routines have tol, rtol and atol. Tighten them only when you can explain why.
Randomness Pass rng= or use np.random.default_rng(42) for reproducible results.
Submodules Import them explicitly (from scipy import stats). import scipy alone does not load them all.

More: Constants, Units and Conventions, NumPy Refresher for SciPy.

Linear algebra (scipy.linalg)

Task Function Note
Solve A x = b linalg.solve(A, b) Prefer it over inv(A) @ b.
Many right-hand sides, same A lu_factor then lu_solve Factor once, solve many times.
Inverse, determinant inv, det Rarely needed; check np.linalg.cond(A) first.
Symmetric positive definite cholesky, cho_factor, cho_solve Fastest and most stable for that class.
Orthogonal factorization qr Basis for least squares.
Triangular system solve_triangular Back and forward substitution.
Eigenvalues eig (general), eigh (symmetric) eigh returns real, sorted values.
Singular values svd Low-rank approximation, PCA.
Least squares lstsq(A, b) Overdetermined systems.
Pseudo-inverse pinv Rank-deficient matrices.
Matrix exponential expm Not the same as np.exp(A).
Norms norm(x, ord=...) 2-norm by default.

More: Matrices, Solving Systems and Inverses, LU, QR, Cholesky and Schur Decompositions, Eigenvalues and Singular Values, Least Squares and Pseudo-inverses.

Optimization (scipy.optimize)

Task Function Note
Minimize one variable minimize_scalar Use bounds= with method="bounded".
Root of one equation brentq(f, a, b) Needs a sign change on [a, b]; very reliable.
Root with a good guess newton, root_scalar Fast, but can diverge.
System of equations root(f, x0) f returns a vector.
Minimize many variables minimize(f, x0) BFGS with gradients, Nelder-Mead without.
Bounds only minimize(..., method="L-BFGS-B", bounds=...) Good for large problems.
Constraints method="SLSQP" or "trust-constr" Use constraints= and bounds=.
Many local minima differential_evolution, basinhopping, dual_annealing, shgo Global, slower, no guarantee.
Fit a model to data curve_fit(model, x, y, p0=...) Returns parameters and covariance.
Fit with robust loss least_squares(..., loss="soft_l1") Downweights outliers.
Linear program linprog HiGHS solver; read .status.
Integer or mixed program milp Use integrality=.
Best one-to-one matching linear_sum_assignment Cost matrix in, row and column indices out.

More: Scalar Minimization and Root Finding, Minimizing Functions of Many Variables, Constraints, Bounds and Global Optimization, Curve Fitting and Least Squares, Linear and Integer Programming.

Integration and ODEs (scipy.integrate)

Task Function Note
Integral of a function quad(f, a, b) Returns (value, error); accepts np.inf.
Double / n-fold integral dblquad, nquad Slow in high dimensions.
Integral of samples trapezoid(y, x), simpson(y, x=x) Simpson needs smooth data.
Running integral of samples cumulative_trapezoid(y, x, initial=0) Same length as y with initial.
Initial value problem solve_ivp(f, (t0, t1), y0) f(t, y); default RK45.
Stiff problem method="Radau", "BDF" or "LSODA" Pass a jac= when you can.
Stop at an event events= Function crossing zero ends or marks the run.
Output at chosen times t_eval= or dense_output=True Avoid relying on internal steps.
Boundary value problem solve_bvp Needs an initial mesh and guess.

More: Numerical Integration, Solving ODEs with solve_ivp, Stiff Systems and Higher-Order ODEs.

Interpolation (scipy.interpolate)

Task Function Note
Smooth curve through points CubicSpline(x, y) Choose bc_type= for the ends.
No overshoot PchipInterpolator, Akima1DInterpolator Keep monotone data monotone.
Regular grid, n-D RegularGridInterpolator Fast lookup on gridded data.
Scattered points griddata, RBFInterpolator RBF scales to many dimensions.
Noisy data make_smoothing_spline, UnivariateSpline Smooths rather than passes through.
Evaluate or build B-splines BSpline, make_interp_spline Lower-level control.

Never trust values outside the data range: extrapolation is a guess.

More: One-Dimensional Interpolation, Multidimensional and Scattered-Data Interpolation.

Statistics (scipy.stats)

Task Function Note
Distribution object stats.norm(loc, scale) Methods: pdf/pmf, cdf, sf, ppf, rvs.
Fit a distribution stats.norm.fit(data) Returns parameters; check the fit visually.
Summary statistics stats.describe(x) Mean, variance, skew, kurtosis.
Compare two means ttest_ind, ttest_rel, ttest_1samp Use equal_var=False for Welch.
Without normality mannwhitneyu, wilcoxon Rank-based tests.
Three or more groups f_oneway, kruskal Follow up with post-hoc tests.
Categorical counts chisquare, chi2_contingency, fisher_exact Table in, p-value out.
Normality shapiro, normaltest Low power with few samples.
Correlation pearsonr, spearmanr, kendalltau Correlation is not causation.
Simple regression linregress Slope, intercept, r, p, stderr.
Resampling bootstrap, permutation_test Assumption-light; fix the seed.
Density estimate gaussian_kde Bandwidth changes the picture.

A small p-value says the data would be surprising under the null hypothesis. It does not say the effect is large or important: report an effect size and an interval too.

More: Distributions and Descriptive Statistics, Hypothesis Testing, Correlation, Regression and Bootstrap.

Signals, FFT and images

Task Function Note
Spectrum of real data fft.rfft, fft.rfftfreq(n, d) Frequencies need the sample spacing d.
Inverse transform fft.irfft Pass n= for odd lengths.
Compression-friendly transform fft.dct Used in image and audio coding.
Design a filter signal.butter(N, Wn, fs=fs, output="sos") Always use sos, not ba, for stability.
Apply a filter sosfilt, sosfiltfilt sosfiltfilt has zero phase shift.
FIR filter firwin, lfilter Linear phase.
Frequency response freqz, sosfreqz Check before you filter.
Power spectrum signal.welch Averaged, less noisy than one FFT.
Time-frequency map signal.spectrogram Window length trades time against frequency.
Find peaks signal.find_peaks(x, prominence=...) Prominence beats a bare height.
Smooth keeping peaks savgol_filter Polynomial window.
Remove a trend signal.detrend Linear by default.
Resample signal.resample Fourier method; assumes periodic data.
Blur, edges ndimage.gaussian_filter, sobel Work on any n-D array.
Label regions ndimage.label Returns labels and a count.
Morphology binary_erosion, binary_dilation, binary_opening Clean up masks.
Rotate, zoom ndimage.rotate, zoom order= controls interpolation.

More: The Fast Fourier Transform, Filter Design and Filtering Signals, Spectrograms, Peaks and Resampling, Image Processing with scipy.ndimage.

Sparse, graphs and space

Task Function Note
Build a sparse matrix sparse.coo_array, lil_array Good for assembling.
Compute with it convert to csr_array Fast row slicing and products.
Banded matrices sparse.diags Use float values to keep a float dtype.
Solve a sparse system splinalg.spsolve Direct. Use cg or gmres for huge ones.
A few eigenvalues splinalg.eigsh Symmetric; svds for singular values.
Shortest paths csgraph.shortest_path, dijkstra Weights from the matrix entries.
Connected parts csgraph.connected_components Returns count and labels.
Spanning tree csgraph.minimum_spanning_tree Edge weights must be nonzero.
Nearest neighbours spatial.KDTree(points).query(x) Returns distance and index.
Pairwise distances spatial.distance.cdist Dense result: mind the memory.
Hull, triangulation ConvexHull, Delaunay, Voronoi Built on Qhull.
3-D rotations spatial.transform.Rotation Euler, quaternion, matrix.
Hierarchical clusters cluster.hierarchy.linkage, fcluster Dendrogram-based.
K-means cluster.vq.kmeans2 Whiten features first.

More: Sparse Matrices, Sparse Linear Algebra and Graph Algorithms, Spatial Algorithms, Special Functions and Clustering.

Special functions and constants

Need Function Note
Factorial-like values special.gamma, special.gammaln gammaln avoids overflow.
Counting special.comb(n, k), special.perm Floats unless exact=True.
Error function special.erf, special.erfc erfc keeps precision in the tail.
Logistic curve special.expit, special.logit Stable sigmoid.
Sum of exponentials special.logsumexp Never overflows.
Bessel family special.jv, special.kv Waves and heat in cylinders.
Physical constants constants.c, constants.h, constants.g SI units.
Unit factors constants.minute, constants.mile, constants.hour Multiply to get SI.

Live tour: one call per submodule, part 1

Python Linear algebra, optimization, integration, interpolation, statistics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Live tour: one call per submodule, part 2

Python Signals, sparse, graphs, spatial, images, special

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Five rules that prevent most bugs

  1. Check the result object. res.success and res.message tell you whether an optimizer or ODE solver actually converged.
  2. Prefer solve over inv. A factorization is faster and more accurate than forming an inverse.
  3. Scale your problem. Parameters or data that differ by many orders of magnitude upset optimizers and conditioning.
  4. Use sparse formats for sparse data, and convert to CSR before arithmetic.
  5. Fix seeds, then report intervals. A single random run or a bare p-value is not a result.

More: Performance, Accuracy and Common Pitfalls, Testing Numerical Code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You need to solve the same system A x = b for many different vectors b. What is the best approach?

    Factoring A is the expensive step. lu_factor does it once and lu_solve reuses it for each right-hand side, with better accuracy than multiplying by an explicit inverse.
  2. 2. Which filter-design output format should you normally choose for stability?

    Second-order sections (sos) split the filter into small, well-behaved stages. The ba (numerator and denominator) form can lose precision at higher orders.
  3. 3. Which function finds a root on an interval [a, b] very reliably, provided f(a) and f(b) have opposite signs?

    brentq brackets the root and combines bisection with faster steps, so it converges whenever the sign change holds. The others solve different problems.
  4. 4. A sparse matrix will be used for many matrix-vector products. Which format is a good target?

    CSR is built for fast row access and arithmetic. COO and LIL are convenient for building a matrix, but you should convert to CSR before heavy computation.

Summary

  • Start from the task: solving and factoring is linalg, finding minima, roots and fits is optimize, areas and dynamics are integrate.
  • stats covers distributions, tests, correlation and resampling; signal, fft and ndimage cover waveforms and images.
  • sparse, csgraph and spatial handle large, structured and geometric data.
  • Always inspect result objects, scale your problem and fix random seeds.
  • Use this page for lookup, and the linked lessons when you need the reasoning.

You have reached the end of the SciPy course. To practice, revisit Exercises with Solutions or Interview Questions.