Overfitting and generalization

core 9 minfundamentalsgeneralization

Builds on:Bias-variance tradeoff

The idea

Training error is a rigged benchmark: the model was optimized on exactly those points, so it will always look better there than on the population it came from. Generalization is the only thing that pays — expected performance on new data drawn from the same distribution. Overfitting is what happens when optimizing the rigged benchmark starts to hurt the real one: the model uses its capacity to encode the noise and coincidences of one particular sample.

The operational quantity is the generalization gap:

gap=E[Ltest]−Ltrain\text{gap} = \mathbb{E}[L_{\text{test}}] - L_{\text{train}}

A small gap with high error means underfitting (bias); a large gap means overfitting (variance). Since expected test loss is unobservable, you estimate it with held-out data, and the protocol matters:

The most useful diagnostic plot is the learning curve: train and validation error as a function of training-set size. Converged-and-high means bias (more data will not help); a persistent gap that shrinks with data means variance (more data will help). This one plot answers the most expensive question in applied ML — should we go collect more data? — before you spend the money.

The failure mode that mimics great generalization is leakage: the model sees information at training time that will not exist at prediction time. Leakage produces beautiful offline metrics and a production disaster, which is why "too good to be true" validation scores should trigger a pipeline audit, not a celebration.

Worked example

A gradient-boosted classifier is trained on subsets of increasing size; error rates (1 - accuracy) on the training subset and a fixed validation set:

Train sizeTrain errorVal errorGap
1000.000.310.31
1,0000.040.190.15
5,0000.080.130.05
20,0000.100.110.01

Reading it like an interviewer wants:

Contrast: if the table had shown train 0.02 / val 0.19 at 20,000 rows with the gap still shrinking, the correct move would be the opposite — collect more data or regularize harder.

Cheat sheet

Gap: gap=Lval−Ltrain\text{gap} = L_{\text{val}} - L_{\text{train}}; overfitting = low train error + large gap; underfitting = high train error + small gap.

Learning-curve shapeDiagnosisMove
Curves converge, both highbiasmore capacity, better features; more data useless
Train low, val high, gap shrinking with nnvariancemore data, regularization, simpler model
Val error rises with more epochs/roundsoverfitting during trainingearly stopping at the val minimum
Val score too good to be trueleakageaudit pipeline, especially fit-before-split transforms