Learn the MachineDansk

Explained with an interactive chart

What is overfitting?

Overfitting is when a model learns its training data so closely that it also learns the random noise in it, and so gets worse on new data. Try it yourself: turn up the model’s flexibility and watch the training error fall while the error on new points rises.

The chart needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

What does the chart show?

The points come from a smooth curve plus random noise. The model is a polynomial fitted to the ten training points with least squares. The degree sets how flexible the model is: degree 0 is a horizontal line, degree 1 a straight line, and degree 9 has ten parameters, enough to pass through all ten points.

The error is the mean squared distance between the model’s curve and the points. The training error is measured on the points the model was fitted to. The validation error is measured on the 40 new points and tells you how well the model does on data it has not seen.

Underfitting and overfitting

At a low degree the model is too rigid. It cannot follow the shape of the curve, so both the training and validation errors are high. This is called underfitting.

At a high degree the model becomes so flexible that it bends to the noise in these particular ten points. The training error approaches zero, but the curve swings wildly between the points and the validation error rises. That is overfitting: the model has learned what was random instead of the pattern.

In between is a range where the validation error is lowest. That is the model you should choose, and you find it by looking at the validation error, not the training error.

Why does the training error always fall?

A degree-5 polynomial can do everything a degree-4 polynomial can, and a little more. With least squares, the training error can therefore never rise as the degree rises. That is why a low training error on its own says almost nothing about how good a model is.

The noise sets a limit

The dashed line shows the variance of the noise. Even the true curve does not hit new points exactly, because the noise cannot be predicted. A validation error far below the line is luck with that particular sample, not a better model.

Press “New sample”

Each new sample draws new points from the same process. Notice that a high-degree model changes dramatically from sample to sample, while a straight line barely changes but is wrong in the same way every time. These are the two sources of error known as variance and bias. The best degree can also change between samples.

How to avoid overfitting

Try the whole workflow

In the Overfitting lab you first predict what will happen, compare your experiments in a table, choose one model based on validation error, and finally test it honestly on a test set you have not looked at. The lab is part of full access.

See the Overfitting lab Or watch a model learn with gradient descent, free

More explainers

The data on this page is synthetic: a sine curve plus normally distributed noise with a standard deviation of 0.25. It is computed in your browser.