NumPy
Intermediate 15 min read NumPy

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.

np.randomdefault_rngseedlstsqsvdeigsolve
Before we start

What you will learn

  • Use default_rng and seeds for reproducible randomness
  • Solve linear systems and least-squares robustly
  • Compute eigen and SVD decompositions
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A laboratory's calibrated pipettes and a set of interpretive microscopes

Think of

Random generation and linear algebra

Pipettes (random) produce clean, controlled samples; microscopes (linalg) make hidden structure visible. Science needs both.

Mechanics

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.

Solve Ax=b
Overdetermined
Symmetric
Any matrix
Boxed result
Hands-on

Examples

Seeded simulation and least squares

Seeded simulation and least squares
import numpy as np
rng = 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)) # 3

The 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.

Real world

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.
Guidelines

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.
Watch out

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!).
When things break

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.
Interviews

Interview checkpoint

Practice

Hands-on lab

Simulate, Factor, Recover
30 min · intermediate
In progress
Objectives
  • Produce reproducible synthetic datasets
  • Recover regression weights via lstsq
  • Reconstruct a matrix from its SVD

Simulate, Factor, Recover

30 min · intermediate

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

np.random.default_rng(SAME) called twice produces:

2

SVD can factor any matrix, not just symmetric or square ones.

Go deeper

Official documentation