Data Leakage
A colleague's new churn model scores a precision of 0.97 and a recall of 0.98 on August, far beyond anything before it. Its most important feature is exit_survey_sent. Pantry sends that survey the moment a member presses "cancel". The model has not learned to predict cancelling; it has learned to notice that a cancellation already started. Live on the 1st of the month, when nobody has pressed anything yet, it will be close to useless.
The rule
Every feature must be knowable at the moment you predict.
Leakage is information in the training data that will not be available when the model is actually used. It makes offline scores look wonderful and live results disappoint. It comes in three common forms:
- Target leakage: a feature created by the outcome itself.
exit_survey_sent,cancellation_reasonandmembership_fee_refundedall exist only because the member cancelled. - Time leakage: a feature computed with data from after the prediction moment. An
avg_orders_per_monthworked out over the whole year includes the very months you are trying to predict. - Train–test contamination: information from validation or test rows seeping into training. Filling blanks with the mean of all rows, scaling with statistics from the full dataset, or letting the same member appear on both sides of a split all do it.
The warning signs are consistent: a score that seems too good to be true, one feature that dominates everything, and a model that shines offline but falls flat in production.
The shortcut
Run the timestamp test on every feature: "At midnight on the 1st, when the model runs, would this value already exist, exactly as stored?" If the answer is no, drop the feature or rebuild it from earlier data.
orders_last_30_days
Counted from orders before the 1st. It exists at prediction time: keep it.
The time-based split from the first lesson in this section already keeps each member's future rows out of training. The timestamp test catches what a split cannot: a leak baked into a single column.
Drills
Answer fast — trust the rule.
Which feature leaks the outcome in Pantry's churn model?
A model is excellent on validation data but poor once it runs live. What should you suspect first?
Before splitting, you fill missing values with the mean of the whole dataset. What is the problem?
avg_orders_per_month is calculated over the full year, then used to predict June cancellations. What kind of leak is it?
Remember
- Every feature must be knowable at the moment you predict.
- Timestamp test: at prediction time, would this value already exist, exactly as stored?
- Trap: scores that look too good to be true. Hunt for the leak before you celebrate.
