“Your model is excellent offline and disappointing in production. How would you check for leakage?”
Data leakage occurs when model development uses information that would not legitimately be available for the prediction being evaluated. It can make an otherwise ordinary model appear remarkably good.
Begin by stating the prediction timestamp, the target, and the population on which the model will operate. Without that contract, you cannot decide whether a feature or validation split is valid.
01. Reconstruct one prediction
Consider a fraud model scoring a payment at 10:00. The request contains the amount and account ID. A previous event occurred at 09:59 but did not arrive in the feature pipeline until 10:02. A dispute is reported two weeks later.
| Information | Usable for the 10:00 prediction? |
|---|---|
| Amount in the current request | Yes, assuming the value is available to the scoring service. |
| Account history known by 10:00 | Yes, under the online feature's freshness contract. |
| The 09:59 event that arrived at 10:02 | No, if the serving system had not received it. |
| The later dispute outcome | A possible label after maturity, never an input for that earlier prediction. |
Event time says when something happened. Availability time says when your system could use it. A historical join that checks only event time can still leak data that arrived late.
For each training example, reproduce the information available at its prediction time. Document the timestamp and update rules for every derived feature.
02. Recognize the common forms
Target leakage
A feature encodes the answer or a consequence of it. Examples include a refund reason assigned after a dispute, a moderation action taken after investigation, or a “resolved fraud” flag. Renaming the column does not change its causal timing.
Preprocessing leakage
A scaler, imputer, feature selector, or dimensionality-reduction step learns from data reserved for evaluation. Split first and fit learned transformations inside the training portion of each validation fold. A pipeline helps enforce this boundary, but it cannot make an invalid source feature safe. scikit-learn documents these pitfalls.
Entity and duplicate leakage
Near-identical examples or records from the same entity can make evaluation easier than the intended deployment task. If the goal is performance on unseen customers, split by customer. If serving predicts future events for existing customers, a time split may be more relevant. Entity overlap is not automatically leakage; the split must match the question.
Repeated test-set use
Choosing features, models, and thresholds after repeatedly inspecting the test set turns it into a development set. Preserve a final holdout for the frozen process and use validation data for iterative decisions.
03. Audit the process, not just the columns
- Write the serving contract. Define what is known when the decision is made and what outcome is predicted.
- Trace suspicious features. Inspect their upstream computation, update schedule, and source timestamps.
- Rebuild features historically. Confirm a later database snapshot is not overwriting what was known earlier.
- Inspect the split. Check duplicate records, temporal overlap, and entity overlap against the intended generalization task.
- Fit within training folds. Include learned preprocessing, resampling, and feature selection in this boundary.
- Repeat evaluation after repair. Compare a simple baseline and the candidate model under the corrected protocol.
An ablation can help: remove a suspicious feature and measure the effect. A large drop is a clue, not proof. The feature could also be legitimately predictive; provenance establishes whether it was available.
04. Separate label delay from feature leakage
In fraud detection, many recent payments have no mature outcome. Labeling all of them legitimate creates noisy or censored targets. That is distinct from putting future dispute information into an input, although both can distort evaluation.
Choose training examples old enough for the selected outcome window, retain the label's observation time, and report performance on mature cohorts. The appropriate window depends on the task; there is no universal “wait seven days” rule.
In recommendation and search, the previous policy controls what is exposed. That selection bias also deserves attention, but it is not fixed merely by correcting feature timestamps.
See it in a systemTime-correct features in fraud detection ↗
Connect the audit to a real-time scoring pipeline and delayed feedback.
05. Answer the tricky follow-ups
“Can future information ever be used?”
Yes, for constructing a later-observed training label when that matches the task. A teacher model might also use privileged information under a carefully specified training method. The serving model and evaluation must still honor the information available at prediction time.
“We used a time split. Are we safe?”
No. Future aggregate features, preprocessing fit on the full dataset, and late-arriving data can cross that boundary. A time split is one safeguard, not a complete audit.
“The model still fails after fixing leakage.”
Investigate distribution shift, feature skew between training and serving, label changes, and overfitting. A production failure can have several causes, and a suspiciously high offline score is not proof of any single one.
“I would reproduce one decision as of its actual serving time, audit feature provenance, and check split and preprocessing boundaries. Then I would re-evaluate a frozen pipeline on a holdout that matches deployment.”
Sources & further reading
- scikit-learn: common pitfalls and recommended practices — data separation and learned preprocessing.
- Kapoor and Narayanan: Leakage and the Reproducibility Crisis in ML-based Science — a broader taxonomy of leakage failures.
Timestamp examples are invented for illustration.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.