From writing rules to learning them
Experience improves performance: measured, not claimed
Two classes drawn from Gaussians with means (−1, −0.5) and (1, 0.5) and σ = 1.1. A nearest-centroid learner sees n labelled examples (E), classifies (T) and is scored on 2,000 fresh points (P). A hand-written rule never improves; the Bayes error is the floor no learner can pass.
The formal problem
Data (xᵢ, yᵢ) ~ P(X, Y) i.i.d. Choose f from a hypothesis class 𝓗 to minimise the risk R(f) = 𝔼[ℓ(f(X), Y)]. P is unknown, so we minimise the empirical risk R̂ₙ(f) = (1/n) Σ ℓ(f(xᵢ), yᵢ) and hope R̂ₙ ≈ R. That hope is generalisation.
Walk one dataset through the pipeline
Derivation: the gradient the walker follows
The training data here are linearly separable, so the minimum of L is approached only as ‖w‖ → ∞: the loss keeps falling after the accuracy reaches 100 %. Watch the boundary sharpen between epoch 100 and 1000.
Three ideas that make learning work
Idea 1 · Features: a non-linear problem becomes linear in φ(x)
A linear classifier in φ-space is a quadric in x-space. Kernel methods use the same trick with φ implicit: k(x, x′) = φ(x)·φ(x′).
Idea 2 · Capacity: training error falls, test error turns back up
Idea 3 · Reward: a three-armed bandit learns with no labels at all
No pulls yet. Each arm pays 1 with an unknown probability.
- pulls T
- 0
- total reward
- 0
- regret
- 0
- best arm share
- –
With ε = 0 the agent can lock onto a poor arm forever; with large ε it keeps paying for exploration. Expected regret per step ≥ ε·(μ* − mean μ).
The defining choice: learn from labels, or find structure
Same geometry, different question. Nearest-centroid and k-means use the identical rule argmin ‖x − μ‖². Supervised learning gets μ from labels in one pass; k-means must guess μ and c together, so it alternates, converges to a local minimum of J, and cannot name its clusters.
Empirical risk, by hand and by eye
Worked example by hand: three points, one line
Data (1, 2), (2, 3), (3, 5); first the guess ŷ = 1.5x, then the least-squares line. Redo every step on paper: there are only three points.
Derivation: why w* = Sxy ÷ Sxx
MSE is a convex quadratic bowl in (w, b): the gradient step button above slides down it, and the “set w*, b*” button jumps to its bottom.
Where the test error comes from
The arithmetic for that degree appears here.
Best degree
The data specification
Derivation: the decomposition at a fixed x
The chart averages each term over 60 test inputs x on [0, 1]. Monte Carlo error makes the parts sum to the empirical test MSE only approximately; the chart shows the three parts and their exact sum.
The family of learning paradigms
Pick a row above.
Transfer learning, formally. Pan & Yang (2010): a domain is 𝒟 = {𝒳, P(X)}, a task is 𝒯 = {𝒴, f}. Given a source (𝒟ₛ, 𝒯ₛ) and a target (𝒟ₜ, 𝒯ₜ) with 𝒟ₛ ≠ 𝒟ₜ or 𝒯ₛ ≠ 𝒯ₜ, transfer learning improves the target predictor fₜ using knowledge from the source. It helps when source and target share low-level structure; when they don't, it can hurt (negative transfer).
The limit: the curse of dimensionality
Apply it to a new problem
Which paradigm?
Reference implementation: the learning step from scratch
# logistic regression by batch gradient descent: the loop of section 02 import numpy as np def fit_logistic(X, y, lr=0.5, epochs=1000): mu, sd = X.mean(0), X.std(0) # fit scaling on TRAIN only Z = (X - mu) / sd w, b = np.zeros(Z.shape[1]), 0.0 for _ in range(epochs): p = 1 / (1 + np.exp(-(Z @ w + b))) # sigma(w.z + b) g = p - y # dl/ds = p - y w -= lr * Z.T @ g / len(y) b -= lr * g.mean() return w, b, mu, sd def predict_proba(X, w, b, mu, sd): return 1 / (1 + np.exp(-(((X - mu) / sd) @ w + b)))
Twelve lines contain the whole of section 02: scaling with training statistics, the sigmoid, the gradient p − y from the derivation, and the update. Return μ and σ with the weights: a model is the weights and the preprocessing it was trained with.