Skip to content
AI360XpertBeta
Core ML

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.

Observations differ from the underlying signal.
Observations differ from the underlying signal.

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 q(x)=2−x2/2q(x)=2-x^2/2. At x=[−2,−1,0,1,2]x=[-2,-1,0,1,2], the five filled training dots have labels [0.1,1.1,2.6,1.1,0.1][0.1,1.1,2.6,1.1,0.1]. Their deviations from the signal are [0.1,−0.4,0.6,−0.4,0.1][0.1,-0.4,0.6,-0.4,0.1]. The center bracket shows the 0.60.6 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 y=0y=0.

A model that cannot bend

A constant fit leaves systematic training residuals.
A constant fit leaves systematic training residuals.

The least-squares constant is the training mean, 5/5=15/5=1. Its dashed vertical residuals expose the missed shape: low endpoints and a high center. Mean squared error, or MSE, averages squared prediction errors:

MSE⁡=1n∑i(y^i−yi)2.\operatorname{MSE}=\frac{1}{n}\sum_i(\hat y_i-y_i)^2.

Here the constant's training MSE is (0.81+0.01+2.56+0.01+0.81)/5=0.84(0.81+0.01+2.56+0.01+0.81)/5=0.84. 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

A quadratic captures the broad shape without hitting every dot.
A quadratic captures the broad shape without hitting every dot.

Fitting degree two gives q(x)=2−x2/2q(x)=2-x^2/2 for this particular sample. You can verify it: the residual vector sums to zero and its dot products with xx and x2x^2 are zero, the least-squares normal equations for the three polynomial coefficients.

The fitted curve leaves training MSE (0.01+0.16+0.36+0.16+0.01)/5=0.14(0.01+0.16+0.36+0.16+0.01)/5=0.14. 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

A quartic can interpolate all five training observations.
A quartic can interpolate all five training observations.

The degree-four interpolant is

h(x)=2.6−4324x2+724x4.h(x)=2.6-\frac{43}{24}x^2+\frac{7}{24}x^4.

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 0.250.25 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 interpolant misses a held-out input between training points.
The interpolant misses a held-out input between training points.

The hollow square is a validation observation at x=1.5x=1.5, with target q(1.5)=0.875q(1.5)=0.875. The interpolant predicts h(1.5)=0.0453125h(1.5)=0.0453125. The vertical bracket is the resulting miss of 0.82968750.8296875 units. The dashed reference is the signal; the solid grey path is the quartic.

For a transparent calculation, validation labels at x=[−1.5,−0.5,0.5,1.5]x=[-1.5,-0.5,0.5,1.5] are noiseless signal values [0.875,1.875,1.875,0.875][0.875,1.875,1.875,0.875]. 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

Lower training error need not lower validation error.
Lower training error need not lower validation error.

The horizontal axis is polynomial degree, not training epochs. The dashed training trace joins MSE values [0.84,0.14,0][0.84,0.14,0] for degrees [0,2,4][0,2,4]. The solid validation trace joins [0.390625,0,0.38779541015625][0.390625,0,0.38779541015625]. Straight connections guide the eye between three measured candidates; they assert nothing about intermediate degrees.

For the quartic, the four validation errors have magnitudes [0.8296875,0.2953125,0.2953125,0.8296875][0.8296875,0.2953125,0.2953125,0.8296875]. Squaring and averaging gives approximately 0.3880.388. Its perfect training fit therefore transfers worse than the quadratic on these inputs. The constant is also poor, at approximately 0.3910.391 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

Damping sample-specific bends trades training fit for validation fit.
Damping sample-specific bends trades training fit for validation fit.

The dashed curve is hh; the solid curve is g=(q+h)/2g=(q+h)/2. At x=1.5x=1.5, damping moves the prediction halfway from 0.04531250.0453125 toward 0.8750.875, landing at 0.460156250.46015625. The square remains the validation target.

More generally, gα=q+α(h−q)g_\alpha=q+\alpha(h-q) has training MSE 0.14(1−α)20.14(1-\alpha)^2 and validation MSE 0.38779541015625α20.38779541015625\alpha^2 on this fixture. At α=0.5\alpha=0.5, these are 0.0350.035 and approximately 0.096950.09695. 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

Validation selects the quadratic among these three candidates.
Validation selects the quadratic among these three candidates.

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 selected model predicts a new input without refitting.
The selected model predicts a new input without refitting.

The diamond marks a fresh input x=0.75x=0.75. Follow its vertical guide to the selected quadratic, then the horizontal guide to the prediction 2−0.752/2=1.718752-0.75^2/2=1.71875. 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

Tuning from test scores compromises the held-out check.
Tuning from test scores compromises the held-out check.

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.