Sub topic 9
Data leakage and how it happens
This chapter assumes you can write basic Python, know what training, validation and test sets are, and have used train_test_split from scikit-learn.
What leakage is
Data leakage happens when information the model should not have at training time influences how it is fitted. That information can come from the test rows, from the validation rows, or from the future. The evaluation score then looks better than the model deserves, and the model disappoints once it is deployed.
Leakage is dangerous because nothing fails. The code runs and the score looks good.
Common sources
- Preprocessing fitted on all rows. Scaling, imputing missing values, PCA and feature selection all learn statistics from data. If they see the test rows, those rows shape the training features.
- Target leakage. A feature encodes the label or is only known after the outcome. An example is a
treatment_givencolumn when you predict a diagnosis. - Duplicates. The same row appears on both sides of the split, so the model is tested on examples it has memorized.
- Related rows. Several samples from the same patient, device or customer end up in both sets. Grouped splits handle this.
- Future information. Time-ordered data is shuffled, so the model trains on events after the ones it is tested on. Time-aware splits handle this.
- Repeated test-set use. Each time you tune against the test score, the test set leaks into your decisions.
Example: scaling before splitting
The code below shows the most common version of this mistake, next to the correct order.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
# Leaky: statistics from the test rows influence the training features.
X_scaled_leaky = StandardScaler().fit_transform(X)
X_train_l, X_test_l, y_train_l, y_test_l = train_test_split(
X_scaled_leaky, y, test_size=0.2, random_state=42
)
# Correct: split first, learn scaling statistics from the training set only.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
scaler = StandardScaler().fit(X_train)
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test)In the leaky version, fit_transform computes each column's mean and standard deviation from all 569 rows. The test rows are part of that calculation. In the correct version, fit sees only the training rows. The test rows are only transformed with those fixed numbers, which is what happens to new data in production.
For scaling, the score difference is usually small. Feature selection or target-based encoding done before the split can inflate scores by a lot, because those steps use the labels.
Spotting leakage
Look for these warning signs:
- A score that is much higher than similar published work or a simple baseline would suggest.
- One feature that dominates the model's feature importances.
- A large gap between test performance and performance on genuinely new data.
Two questions catch most cases:
- For each feature, will its value exist at the moment of prediction?
- For each step that learns from data, did it see only training rows?
Exercise
You impute missing values with the column mean, computed on the full dataset, and then call traintestsplit. Is this leakage, and how do you fix it?
Show answer
Yes. The column means include test rows, so test information flows into the training features. Split first, fit the imputer (for example SimpleImputer) on the training set only, then use it to transform both the training and test sets. A pipeline does this automatically.