ML Atlas

04 · Evaluation · 5 min read · updated

What is overfitting and how do you spot it?

In short

An overfit model memorizes noise in the training set and does worse on new data; an underfit model is too simple to capture even the main pattern.

What it is

Overfitting is when a model has fitted the training set so closely that it has also learned its accidental features — noise, exceptions, label errors — and as a result predicts worse on new data. Underfitting is the opposite: the model is too simple, or trained too briefly, to capture even the main relationship, so it is wrong both on the training data and on new data.

Intuition: a student who memorized the answers to last year's exam will score 100% on it but fail a new set of questions — that is overfitting. A student who only remembered "the answer is usually B" will do poorly on both exams — that is underfitting. We want someone in between: a student who understood the principles and can therefore handle questions they have never seen.

The key point is that overfitting cannot be seen on the training data. There, the overfit model looks best. It reveals itself only when you compare the training score with the score on data the model has not seen.

Mechanism — why it works this way

Every training set is only a sample. It contains the real signal (e.g. "larger tumours are more often malignant") and the randomness of that particular sample (that one patient with unusual values). A learning algorithm minimizes training error and has no way to tell signal from noise — to it, both are simply patterns to fit.

A model with high capacity (a deep tree, a network with millions of weights, a high-degree polynomial) can fit almost any arrangement of points, so it fits the noise too. But the noise in new data is different, so those "learned" details stop fitting and add error. A low-capacity model (a straight line through clearly curved data, a tree with a single split) cannot accommodate even the signal — that is underfitting.

In statistical terms this is the bias–variance trade-off. An underfit model has high bias: it is systematically wrong in the same way. An overfit model has high variance: trained on a different sample from the same population, it would give noticeably different predictions. The error on new data is roughly the sum of these two components plus irreducible noise, which is why the minimum lies somewhere in the middle of the complexity scale.

The risk of overfitting grows when there is little data relative to the number of parameters, when there are many features (making it easier to find a spurious correlation), when labels are noisy and when training runs too long. It shrinks with more data, regularization, a simpler model, early stopping and averaging many models.

A caveat: the picture of a "U-shaped" error curve is not always complete. In very large networks one observes double descent — past the threshold at which the model exactly interpolates the data, test error can start falling again. This does not invalidate the notion of overfitting, but it shows that "number of parameters" is an imperfect measure of complexity.

By example

We trained decision trees to tell malignant tumours from benign ones on part of the cases and tested them on the rest, letting the tree grow deeper each time. A tree with a single question makes quite a few mistakes on both training and test — that is underfitting. A tree with no depth limit grows until it has memorised every training case: 100% on training, but only 90.2% on test — less than a tree with three levels of questions (91.6%). The extra leaves describe individual unusual cases from the training sample, not a rule.

The basic signal of overfitting is the gap between the training and test scores: here 9.8 percentage points, versus 4.2 with a single question. Beware of noise: with a small test set, small differences between depths mean nothing. Averaged over 100 random splits, the three-level tree and the full one both score 93.0% on test — which is why depth is chosen by cross-validation rather than a single split.

How it was computed
  • Data: Breast Cancer Wisconsin (569 tumours, 30 features, positive class = malignant).
  • Split: stratified, 426 training and 143 test (train_test_split, test_size=0.25, random_state=0).
  • Model: DecisionTreeClassifier(max_depth=d, random_state=0) for increasing depth d.
  • Accuracy (training / test): depth 1 (one split, 2 leaves) 93.0% / 88.8%; depth 3 (8 leaves) 97.7% / 91.6%; unlimited (18 pure leaves) 100% / 90.2%.
  • Noise: on 143 test examples one error is worth 0.7 points; depths 3, 4 and 5 give 91.6%, 90.9% and 90.2%.
  • 100 splits (random_state 0 to 99), mean training / test: depth 1 — 92.5% / 90.1%, depth 3 — 97.2% / 93.0%, unlimited — 100% / 93.0%. The gap grows steadily with depth (from 2.4 to 7 points), but the three-level tree beats the full one on test in fewer than half of the splits.

In practice

  • Always compare the training score with the validation score: cross_validate(model, X, y, return_train_score=True). High training and low validation = overfitting; both low = underfitting.
  • Plot a validation curve (ValidationCurveDisplay) for a complexity parameter such as max_depth, C or alpha, and a learning curve (LearningCurveDisplay) to check whether more data would help.
  • Remedies for overfitting: regularization (Ridge, C in LogisticRegression), constraining the tree (max_depth, min_samples_leaf), dropout and weight decay in PyTorch, early stopping, more data, augmentation.
  • Remedies for underfitting: a richer model, extra features (e.g. PolynomialFeatures), weaker regularization, longer training.
  • Common mistake: picking the model "to suit the test set" through repeated attempts — that is overfitting too, just at the level of the researcher's decisions.

Frequently asked questions

How big a gap between training and validation means overfitting?
There is no universal threshold. What matters is whether reducing complexity improves the validation score. Some gap is normal — models almost always do better on data they have seen. The problem is a gap that widens as you add complexity while the validation score falls.
Does 100% training accuracy always mean overfitting?
Not always. Random forests and large networks often fit the training data perfectly and still generalize well. Overfitting is established only by the score on unseen data and by comparison with simpler variants.
Does more data always cure overfitting?
It usually helps, because noise in a larger sample cancels out more often and is harder to memorize. It will not help with underfitting — a model that is too simple stays too simple however many examples it gets. A learning curve shows which case you are dealing with.

Sources

  • James G., Witten D., Hastie T., Tibshirani R. "An Introduction to Statistical Learning", 2nd ed., Springer 2021, ch. 2.2 (Assessing Model Accuracy).
  • Hastie T., Tibshirani R., Friedman J. "The Elements of Statistical Learning", 2nd ed., Springer 2009, ch. 7 (Model Assessment and Selection).
  • Goodfellow I., Bengio Y., Courville A. "Deep Learning", MIT Press 2016, ch. 5.2 (Capacity, Overfitting and Underfitting).
  • Geman S., Bienenstock E., Doursat R. (1992). Neural Networks and the Bias/Variance Dilemma. Neural Computation, 4(1).
  • scikit-learn documentation: Underfitting vs. Overfitting — https://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html

See also