Overfitting & Underfitting
Overfitting & Underfitting — Underfitting is when a model is too simple to capture the pattern (high error everywhere); overfitting is when it is so flexible it memorizes noise (low train error, high test error). The sweet spot minimizes test error.
Slide a model from a flat line to a deranged wiggle and watch two curves: training error keeps falling, but test error dips then rises — the single most important picture in machine learning, drawn by your own hand.
- Train points
- Test points
- Model
- Truth
Train vs test error — the U (click a degree to jump there)
Complexity controls
The idea in plain words
Slide the model from a flat line to a wild wiggle and watch two numbers. Training error only ever falls — a flexible model can always hug the points it has seen. Test error, measured on points it hasn’t, dips and then rises. Too simple underfits; too complex overfits.
The bottom of that test-error U is the sweet spot. Overshoot it and the model is memorizing noise, not the pattern — the same trap you can trigger with polynomial degree, and the reason for the bias–variance tradeoff.
Now, the math
Generalization is measured by the gap between two errors:
- error on the data the model was fit to — falls with complexity.
- error on held-out data — dips then rises, forming the U.
▸ Show the derivation
A model with enough parameters can drive training error to zero by interpolating every point, but those extra degrees of freedom fit the random noise in the sample. On fresh data that noise is different, so the wiggles that helped on the training set now hurt — test error climbs even as training error keeps falling.
Trace it by hand
Using the repo's own polynomial demo data: 10 noisy training points sampled from the curve 2 sin(0.6 x) + 0.3 x, plus 40 independently-noised test points. We fit polynomials of degree 1, 4, and 9 by least squares and report each MSE, rounded to 2 decimals.
Degree 1: too simple
A straight line cannot follow the sine wave, so it misses train and test data alike. Test even lands slightly below train here, pure luck of the noise.
Degree 4: the sweet spot
Enough bend to capture the true curve; test error is at its lowest of the three fits.
Degree 9: memorize the sample
Ten coefficients through ten points interpolates them exactly — the computed training MSE is about 5e-21, pure floating-point dust.
The two curves diverge
Training error is monotone in complexity; test error traces the U whose bottom is the model you actually want.
What just happened: The degree-9 fit scores a perfect 0 on the points it saw and 9.90 on points it did not — 20 times worse than degree 4's 0.48. Training error alone rewarded the memorizer; only the held-out gap exposed it.
Now Break It
Try this: Max out complexity — train error hits zero while test error explodes; resample and the overfit curve flails.
Control: Complexity slider (set to maximum), then resample
What happens: Overfitting! Train error is near zero but test error has exploded — the model memorized the noise.
Where overfitting & underfitting is used
Overfitting and underfitting describe the two ways a model can miss the mark, and both appear constantly in practice. An underfit model is too simple to capture the pattern: a straight line trying to fit clearly curved sales data, or a tiny network that cannot learn to read handwritten digits, shows high error on both training and test data. An overfit model instead memorizes noise: a deep decision tree that perfectly classifies every training email but flags legitimate messages as spam, or a stock-prediction model that fits historical wiggles that never repeat. Teams fighting overfitting reach for regularization, dropout, early stopping, cross-validation, and simply gathering more data. The classic U-shaped curve, where test error falls then rises as complexity grows, guides the search for the sweet spot in between.
A common misconception is that low training error means a good model. Training error near zero often signals overfitting, not success, because the model has learned quirks of the training set that will not generalize; the honest measure is error on held-out data. A second pitfall is treating more complexity or more training as always better. Past a point, extra capacity or extra epochs push a model from the healthy region into overfitting, which is exactly why early stopping and validation curves matter. It also helps to remember that overfitting and underfitting sit on the bias-variance tradeoff: underfitting is high bias, overfitting is high variance, and good generalization balances the two rather than eliminating either alone.
Frequently asked questions
What is the difference between overfitting and underfitting?
How do I know if my model is overfitting?
How can I prevent overfitting?
How do I fix underfitting?
What is the bias-variance tradeoff?
Does low training error mean my model is good?
Written & reviewed by the ML Visualization team · Last updated .