Topic 3 of 6 · 5 min read
Spotting target leakage in features
Goal: Find the leaking feature or preprocessing step in a pipeline whose validation score looks too good to be true.
Leakage means the model saw information during training that it will not have at prediction time, and the warning sign is a validation score that is better than the problem should allow. The model looks excellent offline and fails in production.
Check yourself · 1 of 3
In a training pipeline, what does target leakage mean?
Three common forms
- Target leakage. A feature is a consequence of the label, or is recorded after the outcome. Predicting loan default with
collections_calls_madeleaks: the calls happen only after a default. Predicting hospital readmission usingdischarge_summary_writtenhas the same problem. - Preprocessing leakage. You fit a scaler, imputer, encoder or feature selector on the whole dataset, then split. The test rows influenced the training transform. The scikit-learn pitfalls guide shows selecting features on all data and then getting a high cross-validation score on random labels.
- Duplicate or near-duplicate leakage. The same user, image, document or row appears in both train and test. The model memorises it.
How to look for it
- Distrust a very high score. A 99 percent AUC on a hard task is a reason to look, not to celebrate.
- Check feature importance. One feature carrying most of the signal deserves a question: when is this value known?
- Ask the timing question for every feature. "At the moment we must predict, do we have this value?" If not, drop it.
- Split first, fit later. Fit every transform on training data only, ideally inside a
Pipelineso cross-validation refits it per fold. - Check for duplicates across splits by ID, hash or text similarity.
Before and after
# Leaky: the scaler sees the test rows
X_scaled = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y)
# Fixed: split first, fit the scaler on training data only
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = make_pipeline(StandardScaler(), LogisticRegression())
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
Scaling leaks little on its own. Feature selection, target encoding and imputation by group can leak a lot, which is why the habit matters more than any single case.
What goes wrong
- Fixing the score, not the cause. You drop the obvious leaking column and ignore a second one that is a proxy for it, such as a status code set after the outcome.
- Leakage through the split. Rows of one customer land on both sides.
- A leak in the labels' own pipeline. A feature built from the same table the label came from, joined after the event.
Examples
A column recorded after the outcome
A model predicts which support tickets will escalate and reaches 98 percent accuracy. The top feature is assigned_to_tier2. That value is set when a ticket escalates, so the model reads the answer. Removing it drops accuracy to a realistic level, and the real signal is in ticket text and customer plan.
Feature selection before splitting
selector = SelectKBest(k=20).fit(X, y)
X_sel = selector.transform(X)
scores = cross_val_score(model, X_sel, y)
The selector used the labels of every row, including those later used for validation. Put SelectKBest and the model in one Pipeline and pass that to cross_val_score.
Key points
- A validation score that is too good for the problem is the main warning sign of leakage.
- For every feature ask whether its value is known at the moment of prediction.
- Fit scalers, imputers, encoders and feature selectors on training data only, preferably inside a Pipeline.
- Check for duplicate or near-duplicate rows and entities across train and test.
- A single feature that dominates importance deserves a timing check before any celebration.
Common mistakes
- Celebrating a score of 99 percent without asking why it is so high.
- Fitting preprocessing on the full dataset before the train and test split.
- Dropping one leaking column but not checking for proxies that carry the same information.
- Splitting randomly when the same user or document appears many times.
Check yourself · 2 of 3
Which pipeline avoids preprocessing leakage?
Check yourself · 3 of 3
A model predicting payment default scores 0.99 AUC. Its top feature is days_since_last_collection_call. What should you do?
Practice items, not counted towards your mastery.
Start here
- scikit-learn: common pitfalls and recommended practices (opens in a new tab)ArticleFree
Data leakage, preprocessing inside pipelines and inconsistent preprocessing, with working code.
- Cross-validation: evaluating estimator performance (scikit-learn) (opens in a new tab)DocsFree
Group-aware and time-series splitters, and why each exists.
- Datasheets for Datasets (opens in a new tab)PaperFree
The question list for documenting where a dataset came from, how it was labelled and what it should not be used for.