Machine Learning · hard

How would you detect data leakage in a pipeline?

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Check features using future/target info; fit transforms only on train; audit unrealistically high scores.

How to answer it

Leakage is information in the training features that will not exist at prediction time. The symptom is a model that looks too good; the cause is almost always time or the target.

Where to look, in order:

How to find it when you suspect it:

# 1. per-feature suspicion: a single feature with near-perfect separation is a leak until proven otherwise
from sklearn.metrics import roc_auc_score
{c: roc_auc_score(y, X[c]) for c in X.columns if X[c].dtype != object}
# 2. time-respecting evaluation: train on months 1-9, test on 10-12; a big drop vs random CV is a leak

Then the structural fix: build features as of a timestamp (point-in-time joins), fit every transform inside the pipeline on training folds only, and split by entity and by time.

Related questions

Practice this for real