← All animations

📉 Overfitting & Regularization

A model that's too simple misses the trend; a model that's too flexible memorises the noise instead of learning the signal — it nails every training point yet flails wildly between them. The art of machine learning is landing in the sweet spot between the two. Scroll through to watch a curve go from underfit to overfit, see why test error forms a U, and meet the cures.

1. Curve fitting — underfit, just right, overfit

The grey dots are noisy measurements sampled from a hidden true curve (dashed). We fit a polynomial of adjustable degree through them. Degree 1 is a stiff straight line that misses the shape (underfit). A middling degree captures the real trend. Crank the degree up and the curve contorts to pass through every dot — but lurches wildly in the gaps, chasing noise it should ignore (overfit).

degree 1 underfit
noisy data hidden true curve fitted model

2. Train vs test error — the U-shape and the sweet spot

Now we split the data: most points train the model, the rest are held out as a test set the model never sees while fitting. Training error falls forever as complexity rises — a flexible enough model can memorise the training set perfectly. But test error first drops, then climbs back up: that turn-up is overfitting, and the widening gap between the two curves is the generalisation gap. The lowest point of the test curve is the sweet spot. Add regularization to penalise wiggly fits, or click more data — both flatten the over-fitting tail.

λ = 0 (none) sweet spot: degree —
training error (RMSE) test error (RMSE) N data points

3. Why it happens — and how to cure it

Every model's error splits into bias (error from being too simple to capture the real shape) and variance (error from being so flexible that it swings around with each particular sample of noise). This is the bias–variance trade-off: simple models are high-bias / low-variance (underfit), complex models are low-bias / high-variance (overfit). The signal is the part of the data that repeats; the noise is the part that doesn't. Overfitting is memorising the noise instead of learning the signal — which is why it looks great on data you've already seen and falls apart on data you haven't.