Machine learning · Kaggle
How a leaky encoding made my CV score lie by 8×
Leaky encoding inflated the cross-validation gain by 8× the real gain on the leaderboard.
In the Kaggle Playground competition on airline passenger satisfaction, I improved my ROC AUC from 0.95836 to 0.96056 over six rounds. That is a small gain, so I needed to be sure each step was real and not noise. The tool I relied on was cross-validation (CV): if CV says a change helps, the leaderboard should agree.
It did agree. In every round, my CV score predicted the leaderboard to within 0.0005. This post is about the one technique that can quietly break that agreement, and the experiment I ran to see how badly.
What target encoding is
Models need numbers, but many useful columns are categories, like a travel class or a route. Target encoding replaces each category with the average of the target for that category. If 80% of business-class passengers were satisfied, every "Business" row gets the value 0.80.
This is powerful because it hands the model a summary of what each category means for the outcome. It is also dangerous for exactly the same reason.
Where the leak comes from
The leaky way is to compute those averages once, on the whole training set, and then run cross-validation. Each row's own label is now part of its own feature. When CV later "tests" on a fold, the rows in that fold already carry information about their answers. The model looks better than it is.
The problem is worst for rare categories. If a category appears only three times, its average is heavily shaped by each of those three labels, so the feature nearly gives the answer away.
The fold-safe version
The fix is to compute the encoding inside each fold, using only that fold's training rows. The validation rows are encoded with averages they did not help create, which is exactly the situation the model faces on the real test set.
# For each CV fold: learn the averages on the training part only
for train_idx, valid_idx in folds.split(X, y):
means = y.iloc[train_idx].groupby(X["route"].iloc[train_idx]).mean()
X_valid_enc = X["route"].iloc[valid_idx].map(means) # no peeking
I also smoothed the averages toward the overall mean, so a category seen only a few times does not get an extreme value.
The experiment
To measure the damage, I added the same encoded feature both ways and compared what CV promised with what the leaderboard delivered. The fold-safe version gave a CV gain that the leaderboard confirmed. The leaky version reported a gain about 8 times larger than the improvement that actually showed up on unseen data.
If I had trusted the leaky number, I would have kept stacking features on a false signal and been surprised by the final ranking.