Overfitting & How to Fix It
Read a little, play a little. No scary maths, and no rush.
The student who memorized the textbook
A student memorizes past papers word-for-word: 100% on those exact questions, blank stare at anything new. That's overfitting — the model learned training quirks instead of patterns. The opposite failure, underfitting (bad on train and validation), means the model is too simple to capture even the training signal.
Reading the curves
Plot train and validation score as complexity grows (deeper trees, more epochs). Healthy: both rise together, small gap. Overfitting: train keeps climbing while validation stalls then falls — the divergence point is where learning stopped and memorizing began. This picture diagnoses more ML problems than any other single image.
The fix ladder (climb in order)
- More data — the honest cure; quirks drown in volume.
- Simpler model — shallower trees, fewer features. Capacity must match data.
- Regularization — penalize complexity (
Cin logistic regression,max_depthin trees). Gentle pressure toward simpler explanations. - Cross-validation — rotate which slice validates (
cross_val_score) so your number isn't a lucky split. - Early stopping — halt training the moment validation worsens.
A worked example: choosing tree depth
A team tries decision trees of different depths on the same split:
- Depth 1: train 0.72, validation 0.71. Both low and close together: underfitting.
- Depth 4: train 0.86, validation 0.84. Both higher, small gap: a good fit.
- Depth 20: train 1.00, validation 0.76. Perfect on training data, worse on new data: overfitting.
They choose depth 4. The deepest tree looked best on the data it memorized and was the worst choice for real use.
Will more data help?
A learning curve plots training and validation scores as you train on more and more data. If the validation score is still rising at your current data size, collecting more examples is likely worth it. If both curves have flattened out close together at a disappointing level, more of the same data will not help; you need better features or a more flexible model.
You can overfit the validation set too
Trying hundreds of settings and keeping whichever scores best on validation slowly tunes the model to the quirks of that particular slice. That is why the test set is kept aside and used once at the very end. For small datasets, cross-validation reduces the risk by spreading the evaluation across many slices.
The same ideas in neural networks
Networks overfit too, and the fixes have their own names: dropout randomly switches off parts of the network during training, weight decay is regularization, early stopping halts when validation worsens, and data augmentation creates varied copies of training examples, such as flipped or cropped images, to get the effect of more data. Whatever the model, the diagnosis stays the same: compare training and validation scores, and treat a growing gap as the warning sign.
Remember this
- Gap between train and validation is the diagnosis; the ladder is the prescription.
- Complexity must be earned by data — constrain first, enlarge only with evidence.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue