QUICK ANSWEROverfitting means a model has memorised the training data, including its noise, instead of learning patterns that generalise. The sign is a big gap: high training accuracy but much lower validation or test accuracy. Fix it by getting more or cleaner data, using cross-validation, simplifying the model, adding regularisation (L1/L2, dropout), early stopping, and removing features that leak the answer.

How to detect it
| Situation | Training score | Validation score | Diagnosis |
|---|---|---|---|
| Good fit | High | High, close to training | OK |
| Overfitting | Very high | Much lower | Model memorises |
| Underfitting | Low | Low | Model too simple or weak features |
| Suspiciously perfect | ~100% | ~100% | Check for data leakage |
Fix it step by step
- Split properly: keep a test set you never touch until the end; use k-fold cross-validation for model choice.
- Plot learning curves (score vs training size) and validation curves (score vs model complexity).
- Check for data leakage: features created from the target, future information in time-series, or the same customer in train and test.
- Simplify: fewer features, shallower trees (
max_depth,min_samples_leaf), fewer layers. - Regularise: L2/L1 penalty (
Coralphain scikit-learn), dropout and weight decay in neural networks. - Use early stopping on the validation set for gradient boosting and neural networks.
- Get more data or use data augmentation (images, text).
- Use ensembles that reduce variance, such as random forests or bagging.
Quick example with scikit-learn
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
for depth in [3, 6, 12, None]:
m = RandomForestClassifier(max_depth=depth, random_state=42)
s = cross_val_score(m, X, y, cv=5, scoring="f1")
print(depth, round(s.mean(), 3)) Pick the depth with the best cross-validated score, not the best training score.
Data leakage: the hidden overfitting
Leakage makes a model look excellent in testing and fail in production. A classic example: predicting loan default with a column "collections_agency_assigned", which only exists after a default. Ask for every feature: "would I know this at the moment I make the prediction?"
Questions
Is a small gap between train and validation scores normal?
Yes. Some gap is expected. Worry when the gap is large or growing as you train longer.
Does more data always fix overfitting?
It usually helps most, but only if the new data is relevant and correctly labelled.