Visual explainer
Overfitting vs Underfitting
Underfitting misses the pattern; overfitting follows sample noise. Compare both on the same held-out inputs before choosing a model.
Underfitting leaves important structure unexplained. Overfitting adapts to details of the training sample that do not transfer to new observations. Low training loss alone cannot distinguish useful learning from memorization.
These are synthetic, unitless regression data, not benchmark results. The dashed signal is . At , the five filled training dots have labels . Their deviations from the signal are . The center bracket shows the deviation. In real work the true signal is usually unknown; here it makes the mechanism checkable. Every regression plot uses the same x and y scales, with the horizontal axis at .
A model that cannot bend
The least-squares constant is the training mean, . Its dashed vertical residuals expose the missed shape: low endpoints and a high center. Mean squared error, or MSE, averages squared prediction errors:
Here the constant's training MSE is . A small train–validation gap would not repair its inability to bend. High loss can also come from optimization, features or noisy labels, so inspect those before declaring capacity the cause.
Follow the broad shape
Fitting degree two gives for this particular sample. You can verify it: the residual vector sums to zero and its dot products with and are zero, the least-squares normal equations for the three polynomial coefficients.
The fitted curve leaves training MSE . The remaining error is visible. A useful fit need not pass through every training observation; knowing which part is noise is an advantage of this constructed example, not information normally available to the learner.
Hit every training dot
The degree-four interpolant is
Substituting each training input reproduces its label, giving zero training MSE. Five polynomial coefficients suffice for five distinct inputs; symmetry makes the two odd coefficients zero. The drawing joins exact evaluations every input units with straight segments, approximating the continuous quartic. Its small dips below the horizontal axis are negative predictions, not clipping. Interpolation by itself does not establish overfitting; the next scene checks unseen inputs.
Look between training inputs
The hollow square is a validation observation at , with target . The interpolant predicts . The vertical bracket is the resulting miss of units. The dashed reference is the signal; the solid grey path is the quartic.
For a transparent calculation, validation labels at are noiseless signal values . This deliberately favorable check gives the quadratic zero validation loss. It does not imply zero expected error on future noisy observations.
Compare errors on one scale
The horizontal axis is polynomial degree, not training epochs. The dashed training trace joins MSE values for degrees . The solid validation trace joins . Straight connections guide the eye between three measured candidates; they assert nothing about intermediate degrees.
For the quartic, the four validation errors have magnitudes . Squaring and averaging gives approximately . Its perfect training fit therefore transfers worse than the quadratic on these inputs. The constant is also poor, at approximately validation MSE. There is no universal numerical gap threshold, and a U-shaped curve is not guaranteed for every model family or dataset.
Dampen the extra bends
The dashed curve is ; the solid curve is . At , damping moves the prediction halfway from toward , landing at . The square remains the validation target.
More generally, has training MSE and validation MSE on this fixture. At , these are and approximately . This controlled blend illustrates restraint; it is not a ridge solution or a practical algorithm that knows the true signal. Real penalties, model simplification and early stopping must be evaluated on validation data. Early stopping changes training time; the earlier degree sweep changes model capacity.
Select with validation
Bar length encodes validation MSE on one scale. The outlined zero mark means no bar length; the check marks the selected degree-two model. Choose it among these three candidates, then freeze that choice before final testing. Because validation helped select the model, its winning score is not an independent estimate of deployment error. Use cross-validation when a single split is too unstable.
Predict a fresh input
The diamond marks a fresh input . Follow its vertical guide to the selected quadratic, then the horizontal guide to the prediction . Neither this input nor its label changes the fitted coefficients. A final test set measures the frozen model on unused observations; one correct prediction cannot establish population-wide reliability or resilience to distribution shift.
Where it breaks
The amber return path shows the failure: test scores feed back into model choices. Once those scores influence tuning, the test is part of selection and can look too optimistic. Keep a fresh independent check, or use a suitable nested evaluation design. No fixed production loss or guaranteed collapse follows from this schematic.
The scikit-learn polynomial example demonstrates insufficient and excessive flexibility. Its validation-curve guide explains selection and final testing; the cross-validation guide describes test leakage. Our exact numbers come from the synthetic fixture above.
The Quick Version
- Observations can differ from their underlying signal.
- A constant leaves systematic residuals on curved data.
- A quadratic can preserve structure without hitting every dot.
- Zero training error does not establish generalization.
- Unseen inputs expose sample-specific interpolation errors.
- Compare training and validation on matching scales.
- Restraint can trade training fit for better validation fit.
- Select candidates with validation, then freeze the choice.
- Predict fresh inputs without refitting on their labels.
- Test-score feedback compromises the independent evaluation.