Interactive statistics

56 free interactive lessons: from mean and median to A/B tests and Bayes. Drag the charts and build intuition. No sign-up.

Module 8: Classification

Overfitting and honest validation (train/test)

A model can be made arbitrarily complex and forced to pass perfectly through every training point. But that is not the goal: the model must work on NEW data it has never seen. Here hides the main trap of machine learning β€” and the reason all the metrics (AUC, RMSE) must be measured the right way.

βšͺ training🟑 new data (test)
Training error: 51Error on new data: 9973

The white points are the data the model learns on. The teal curve is the model itself. The yellow points are new data the model has not seen. The slider sets the model's complexity (the curve's flexibility).

What it means

Splitting into train and test (and more reliably β€” cross-validation) is basic machine learning hygiene. All the metrics from the previous lessons (AUC, precision/recall, RMSE) are meaningful only on held-out data. A model praised for accuracy on its training data can be useless in production.

This continues the thread about sample size and honesty: a pretty result on the same data the model was tuned on easily turns out to be self-deception β€” memorized noise.

Where it shows up

Overfitting explains why a "genius" trading strategy that perfectly describes the past loses money on the future β€” it memorized history's accidents.

And why a model trained on one city or one camera starts failing in new conditions: it adapted to particulars, not to the essence.

Definitions
Overfitting
the model memorized the random noise of the training data and performs poorly on new data.
Underfitting
the model is too simple and misses even the real pattern.
Train / test
the data split: the model learns on the training part; quality is honestly checked on the test part.
Cross-validation
a k-fold split: each part takes a turn as the test set, the estimates are averaged β€” a stable quality estimate.
When the method lies (assumptions)

The honesty of train/test rests on the test taking NO part in training or tuning. Data leakage (test bleeding into training, or a feature "from the future") paints a pretty but fake estimate β€” there is a separate lesson on this in the traps module.

The split must respect the structure: for time series the test comes LATER than the training (you cannot learn on the future); for groups (one user β€” many rows) split by group, otherwise the estimate is inflated.

Deep dive: the math and the mechanism (optional)

Under the hood is the bias-variance trade-off. A simple model has high bias (systematically off) but low variance (stable across samples). A complex one β€” low bias but high variance (wiggles from sample to sample). The error on new data β‰ˆ biasΒ² + variance + irreducible noise, and the minimum is in the middle of the complexity range.

Overfitting is fought not only with train/test: regularization (a penalty for complexity), more data, a simpler model, early stopping. All of them shift the bias-variance balance toward the optimum.

Next β†’
Enjoying the materials?

This site is built by the DataSlice Telegram channel: data analytics in plain words β€” real cases, metrics, careers. The channel is in Russian.

Open the Telegram channel