Random Numbers and Linear Algebra
Simulate and factor your way to insight
np.random generates reproducible random data and np.linalg powers solve, eig, svd and matrix products — the primitives behind simulations, feature synthesis, PCA and least squares.
What you will learn
- Use default_rng and seeds for reproducible randomness
- Solve linear systems and least-squares robustly
- Compute eigen and SVD decompositions
What is this?
numpy's random module generates synthetic data (normal, uniform, binomial, etc.) and shuffles arrays; `random.default_rng()` with a seed makes draws reproducible. The linalg module exposes the compact linear algebra backend (LAPACK): matmul, solve, lstsq, eig, svd, det, matrix_rank. Together they cover simulations and the matrix factorizations behind most ML.
Why do you need it?
- Seeded randomness is the difference between a reproducible experiment and a lottery.
- np.linalg.lstsq is the robust replacement for the normal equations.
- SVD is the engine of PCA, embeddings, compression and recommendation.
- Synthetic data (np.random) is how you test algorithms before touching real data.
Mental model
A laboratory's calibrated pipettes and a set of interpretive microscopes
Random generation and linear algebra
Pipettes (random) produce clean, controlled samples; microscopes (linalg) make hidden structure visible. Science needs both.
How it works
- rng = np.random.default_rng(seed); rng.normal(size), rng.integers, rng.choice, rng.permutation.
- The same seed → same sequence → reproducible experiments.
- np.linalg.solve(A, b) solves Ax=b for square A; lstsq(A, b) handles overdetermined systems by least squares.
- eig/eigh factor square matrices; svd decomposes any matrix into UΣVᵀ.
- @ and np.matmul are the exposed product; dot is backward-compatible sugar.
Decomposition choices.
Examples
Seeded simulation and least squares
import numpy as nprng = np.random.default_rng(42)X = rng.normal(size=(100, 3))y = X @ np.array([2.0, -1.0, 0.5]) + rng.normal(0, 0.1, 100)w, res, rank, sv = np.linalg.lstsq(X, y, rcond=None)print(w.round(3)) # ~[2., -1., 0.5]print(np.linalg.matrix_rank(X)) # 3The design matrix was simulated, then recovered via lstsq to ~3 decimals. Small noise prevents exact recovery — the residual r is why real fits report error at all.
In production
- A Monte-Carlo risk model uses a seeded default_rng so every compliance run reproduces identical scenarios.
- A recommender computes embeddings via truncated SVD of the interaction matrix.
- An ML platform's stability checks run lstsq on near-collinear feature sets to expose rank deficiency.
Best practices
- Always seed generators at the top of experiments for reproducibility.
- Use default_rng over the legacy np.random global API (better stateless design).
- For least squares, prefer lstsq over explicit (XᵀX)⁻¹Xᵀy.
Common mistakes
- Reseeding inside a loop, producing the same 'random' values every iteration.
- Using solve on an ill-conditioned or singular matrix.
- Treating SVD's U and Vᵀ interchangeably (they're different spaces!).
Debugging tips
- Non-reproducible results → some code path reseeds or mutates the generator.
- A 'Singular matrix' from solve → use lstsq or add regularization.
- Check lstsq's rank output: rank < X.shape[1] flags redundant columns.
Interview checkpoint
Hands-on lab
- Produce reproducible synthetic datasets
- Recover regression weights via lstsq
- Reconstruct a matrix from its SVD
Simulate, Factor, Recover
30 min · intermediate
Quick quiz
np.random.default_rng(SAME) called twice produces:
SVD can factor any matrix, not just symmetric or square ones.