8 Overfitting and Generalization

Learn how overfitting and underfitting affect performance on unseen data, how to diagnose them, and how to improve generalization through suitable model choices and careful evaluation.

and model fit

A useful model performs well not only on the examples used to train it, but also on genuinely unseen examples. describes this ability to make accurate predictions on previously unseen data.

occurs when a model learns the training data too closely, including noise or accidental details, and performs worse on new data. occurs when a model cannot capture important patterns, so it performs poorly even on its training data.

Diagnosing fit with validation

Compare performance on training data with performance on separate . The appropriate measure depends on the task: a regression model might be assessed with mean squared error, while a classifier might be assessed with accuracy or another classification metric. For error measures, lower is better; for scores such as accuracy, higher is better.

The comparison helps identify common patterns:

  • Poor training performance and poor validation performance: likely .

  • Good training performance and much worse validation performance: likely .

  • Good training performance and similar validation performance: reasonable .

For example, a straight line might underfit a curved relationship between house size and price. A very high-degree polynomial might pass through every training point, including unusual or noisy sales, but make poor predictions for other houses. A model with suitable complexity should capture the broad relationship without chasing individual quirks. Learning curves and validation curves can help reveal these patterns.

Why models overfit or underfit

is more likely when a model has too much flexibility for the amount or quality of the available data. Possible contributors include deep decision trees, overly complex equations, too many training iterations, and noisy or unrepresentative datasets.

Evaluation can also give a misleading impression of if the evaluation data are not truly independent of the training data. Performance may also be affected when training data and future data come from different distributions.

can result from a model that is too simple, missing useful features, or training that has not progressed enough. Excessively strong can restrict a model so much that it fails to learn real patterns.

Adjusting model complexity and data

Choose adjustments based on the performance pattern. If a model overfits, reduce its effective complexity: use a simpler model, limit decision-tree depth, require more examples per leaf, or select fewer relevant features. discourages overly complex parameter values, while early stopping ends training when validation performance stops improving.

If a model underfits, increase its ability to learn useful patterns. Consider a more suitable model or features, reduce excessive , or train longer when training has not converged.

Use enough representative training data. More varied examples can help a model learn stable relationships rather than memorize a small sample. Check whether the data resemble the situations where predictions will be used.

Evaluating models without leakage

Use the data splits for distinct purposes. Train the model and learn preprocessing steps using training data. Use to compare models or tune settings, and keep the test set for a final evaluation. Repeatedly choosing settings based on the test score can make that score overly optimistic.

Prevent by splitting data before preprocessing or feature selection. Fit transformations such as scaling or imputation on training data, then apply the learned transformation to validation and test data. Pipelines help keep these steps within the correct data split.

When appropriate, use to make model selection more stable, especially when data are limited. In k-fold , divide the training data into folds. Train on all but one fold and validate on the remaining fold, repeating until each fold has served as . Keep a separate test set for the final assessment.

Choosing a model that generalizes

The goal is not to maximize training performance alone. Select a model that learns meaningful structure and continues to perform well on genuinely unseen examples. Diagnose fit by comparing training and validation performance, match model complexity to the problem, use representative data, tune with or , prevent , and reserve the test set for final evaluation.