Sub topic 1
What data leakage is and why it inflates scores
Prerequisites
This chapter assumes you already know:
- Basic supervised learning: features, labels, and training and test sets.
- Python and NumPy fundamentals.
- scikit-learn estimators and their
fit,transform, andpredictmethods. - Overfitting, and why we evaluate on held-out data.
The definition
Data leakage happens when information from outside the training data influences the model during training or model selection. That information might be test labels, future values, or the target itself. The model then gets credit in evaluation for knowledge it will not have when it is deployed.
The evaluation score measures how well the model generalizes to unseen data. Leakage breaks that measurement. The model is no longer tested on data it has never seen, so the score no longer estimates real-world performance.
Why scores go up
Held-out evaluation works because the test set plays the role of the future. For that to hold, nothing computed from the test rows may reach the model before scoring. A leak breaks this in one of two ways:
- The model sees the answer. A feature encodes the label directly or indirectly. The model learns a shortcut that exists only in your dataset.
- The model sees the test set. Statistics, selections, or choices made using test rows shape the model. It is partly fitted to the data that is supposed to judge it.
In both cases the reported metric is optimistic. The model then performs worse in production, where the shortcut is missing or the test rows are truly unseen.
The main kinds of leakage
The rest of this tutorial covers each of these in turn. Here is a short map.
| Kind | What leaks | Typical cause |
|---|---|---|
| Target leakage | The label, through a feature | Using a column recorded after the outcome |
| Preprocessing leakage | Test-set statistics | Scaling or imputing before splitting |
| Temporal leakage | Future information | Random splits on time-ordered data |
| Group and duplicate leakage | Near-copies of test rows | The same patient or record on both sides |
| Tuning leakage | Test performance | Choosing hyperparameters by repeatedly checking the test set |
A small example
The most common form is fitting a preprocessing step on the full dataset before splitting. The scaler learns the mean and variance of every row, including the rows you plan to hold out.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X, y = make_classification(n_samples=500, n_features=10, random_state=0)
# Leaky: the scaler learns the mean and variance of the test rows too
X_scaled = StandardScaler().fit_transform(X)
X_train_bad, X_test_bad, y_train_bad, y_test_bad = train_test_split(
X_scaled, y, random_state=0
)
# Correct: split first, fit the scaler on training rows only
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
scaler = StandardScaler().fit(X_train)
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test)In the leaky version, the test rows contributed to the scaler's parameters. In the correct version, the scaler only ever saw X_train. The test set is transformed with training statistics, which is what happens in production when a new row arrives.
The effect of this particular leak is often small, because a mean and a variance carry little information about any single row. Do not read that as a reason to ignore it. The same pattern with a more powerful step, such as feature selection that uses labels, can turn a model with no skill into one that looks accurate. Later chapters show that case.
A rule of thumb
For every value the model uses, ask two questions:
- Would I have this value at prediction time?
- Was it computed using any data the model is not allowed to see?
If the answer to the first is no, or to the second is yes, you have a leak. Most leakage bugs come down to one of these two questions.
Why it is easy to miss
Leakage rarely raises an error. The code runs, the metrics look good, and nothing signals a problem. It is usually found late, when a deployed model underperforms its offline score.
Two habits reduce the risk early:
- Split the data before you look at, transform, or select anything based on it.
- Treat the test set as locked until the final evaluation.
The next chapter builds on these habits with train, validation, and test splits.
Exercise
A teammate standardizes the whole dataset with StandardScaler, then splits it into train and test sets and reports test accuracy. Which of the two rule-of-thumb questions does this violate, and how would you fix it?
Show answer
It violates the second question: the scaler's mean and variance were computed using test rows, which the model is not allowed to see. Fix it by splitting first, fitting the scaler on the training rows only, and then using that fitted scaler to transform both the training and test sets.