Overfitting, validation and data leakage
ML Foundations · 10 questions
Short notes
Key idea
Overfitting means the model learned the training data's noise, not the pattern: great on training data, poor on new data. Leakage means information the model won't have at prediction time slipped into training, so test scores look too good to be true.
Syntax
- Compare training and validation scores: a big gap means overfitting.
- Cross-validation:
cross_val_score(model, X, y, cv=5)gives 5 scores on 5 different splits. - Reduce overfitting: simpler model, more data, fewer features, regularisation, limits like
max_depth. - Pipelines fit preprocessing on training folds only:
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
pipe = make_pipeline(StandardScaler(), LogisticRegression())
Example
A churn model scores 99%. The feature "account_closed_date is filled" is only ever filled after a customer churns: leakage. Remove it and the honest score is 81%.
Traps
- Features recorded after the outcome (refund issued, cancellation reason) leak the answer.
- Scaling or filling missing values on all data before splitting leaks test information.
- Random splits on time data let the model see the future; split by date.
- Tuning against the test set until it looks good overfits to the test set.
Want the full lesson with step-by-step examples, a project and a certificate? ML Foundations course, ₹180 →
- Question 1easy
Training accuracy 100%, test accuracy 72%. What does this suggest?
- Question 2medium
You try 10 settings and pick the one with the best test-set score, then report that score. What's wrong?
- Question 3medium
How many scores does 5-fold cross-validation return?
from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.linear_model import LogisticRegression X, y = make_classification(n_samples=100, random_state=0) print(len(cross_val_score(LogisticRegression(max_iter=500), X, y, cv=5)))
- Question 4medium
You want to predict, early in a visit, whether it will end in a purchase. Which feature leaks?
- Question 5hard
You refine a triage prompt until it scores 100% on 20 tickets, and report 100%. What's the problem?
Overfitting, validation and data leakage
Overfitting, validation and data leakage
Overfitting, validation and data leakage
Overfitting, validation and data leakage
Overfitting, validation and data leakage
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.