Machine learningLoading progress…

Baselines and overfitting

Define a learning problem, establish a baseline and evaluate it on data not used for fitting.

Start with a baseline

Before trusting a fancy model, build the dumbest sensible one. For numbers, predict the training mean for every example. For categories, predict the most common class. This baseline gives the score any real model must beat. On our 40 validation houses the mean baseline has MSE 7809.

Predict first. A classifier reports 92% accuracy. 90% of the examples belong to one class. How impressive is it?

Training error always falls. Validation error does not.

A flexible model can bend through every training point, memorising even the noise. That is overfitting: very low training error with poor error on new data. Raise the degree and track both curves.

40801201600100200300400area (m2)price (thousands)

Blue: the 12 training houses the curve is fitted to. Orange: 40 validation houses the curve never saw. A green curve that leaves the picture has predictions beyond 0 to 440 thousand.

degree 1: 2 fitted numbers training MSE = 369.1 (root 19.2) validation MSE = 248.7 (root 15.8) baseline val = 7808.7
0369050100150degreetypical miss (root MSE)

blue training, orange validation (capped at 150). Only degrees you have visited are drawn.

baseline 88.4
this model 15.8

Beats the baseline on validation by 97 percent (in MSE).

Orange dots are 40 validation houses the fit never saw. Raise the degree: training error keeps falling, validation error falls, then turns upward.

Root-MSE (typical miss in thousands): degree 0 gives 88 on validation, the best degree 3 gives 15, and degree 9 gives 640 while its training error is only 8. Choosing by training error alone would pick degree 9, the worst model here.

Reading the gap

  • Both errors high: underfitting. The model is too simple (degree 0 here).
  • Training low, validation high: overfitting. Simplify, add data, or regularise.
  • Suspiciously perfect on validation: suspect leakage, not genius.
Chapter context

A learning algorithm chooses a model from data rather than requiring a person to specify every decision rule. The resulting model is useful only if its predictions transfer to the intended future inputs under a clearly defined objective.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For labeled data D={(xi,yi)}i=1nD=\{(x_i,y_i)\}_{i=1}^n, empirical risk is

R^(θ)=1n∑iℓ(fθ(xi),yi).\hat R(\theta)=\frac1n\sum_i\ell(f_\theta(x_i),y_i).

It estimates performance on the sampled population, not automatically on every deployment population. Accuracy is the fraction of correct predictions when classification is the task.

Worked example

A classifier predicts five held-out labels: [1,0,1,1,0] for actual labels [1,0,0,1,0]. Four are correct, so accuracy is 4/5=0.8. This number says nothing about its training accuracy or behavior on a differently collected test set.

Reference code

The following example uses the Python standard library and does not call a remote service.

actual = [1,0,0,1,0]
predicted = [1,0,1,1,0]
correct = sum(a == b for a,b in zip(actual,predicted))
assert correct == 4
assert correct/len(actual) == .8

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Learning a pattern does not establish causation. Validation data used repeatedly for model selection is no longer an untouched test. A random split is not always an independent split.