Skip to content

A leakage hunt: when a great score is the warning

Four leaks, four symptoms. Reproduce each, then use the app's automatic exclusions and the Leakage scan to catch them before anyone deploys the model.

Applied ~12 min8 steps

You will be able to

  • Name four ways information leaks into a validation score
  • Reproduce a leak and see the gap on new data
  • Use Left out automatically and the Leakage scan to catch suspects

Read first

01

The four leaks

Read

LeakageData leakageInformation from the validation data or the future seeps into training, so scores look better than reality. means the validation score used information you won't have when the model is used.

LeakExampleSymptom
Future or proxy columnrefund_issued when predicting churnOne feature dominates
Identifier or row orderCustomer ID, row numberA near-unique column scores high
Repeated entitiesSame patient in both setsRandom folds beat grouped folds
Fitting outside the foldsChoosing features on all rowsScore falls on genuinely new data

Each one looks like a brilliant model until it's used.

02

Too good to be true?

Predict

Predict first

You have 60 rows, 200 columns of pure noise, and a random label. You pick the 10 columns most correlated with the label using all rows, then cross-validate a simple model on those 10. Accuracy comes out far above 50%. What's happening?

Noise-only data can't score that highIt could be chance, with so many columnsIt's almost certainly fitting the labels through selection
Reveal

Selection leak. Those 10 columns were picked because they match this label by chance, using the validation rows too. Inside each fold, the model finds a pattern that doesn't exist anywhere else. Redo the selection inside each fold and accuracy falls to about 50%.

03

Reproduce them

Explore

Try each entry in The pipeline and read Cross-validated accuracy, Accuracy on new data and the Gap.

DExplorable · Leakage detective

Why does a model that scores brilliantly in testing fail on real data?

Read the pipeline on the left. Will its cross-validation score overstate real-world accuracy by more than 6 points?

How sure?

The pipeline

200
60
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Use the check to find the pipeline that matches new-data accuracy within 6 points. Then press more features, same rows: more candidates give selection more accidents to pick from.

04

Fences in the app

Read

Two checks run before anyone trusts a score.

Left out automatically, in Setup, drops identifiers, constant columns and flagged leaks using the dataset's profile, with a reason beside each. Profile the dataset in Insights first, or that list stays short.

The Leakage scan, in a run's Robustness tab, scores each feature alone against the target and flags likely leakage, suspicious and identifier. It can only find single-column leaks, so it's a net, not a guarantee.

05

Interrogate a feature

Reflect

Take the top feature of a model you've built. Ask three questions. When is its value written, relative to the outcome? Who or what writes it, and could the outcome trigger that? Would it hold the same value on the day you'd really score the row?

06

Check your understanding

Checkpoint

Check yourself

You're predicting loan default. Which feature is the likeliest leak?

Show the answer

Answer: b

Collection calls happen because payments were missed, so the feature contains the outcome. The others are known at the moment of decision.

Check yourself

You standardize all the columns using all rows, then cross-validate. What kind of leak is this?

Show the answer

Answer: a

Means and SDs are learned values, so they belong inside each fold's training rows. The app's preprocessing does this for every model.

Check yourself

The Leakage scan finds nothing. Which statement is right?

Show the answer

Answer: b

The scan looks at one feature at a time. Pair it with the Fold scheme check and your own reading of when each column is written.

07

Hunt in your own run

In the app

Open a trained run, such as the walkthrough's model of {{target}}, and go to Robustness. Set Group folds by if your rows repeat, then run the checks. Read the Leakage scan table's Flag and Why columns, and the Fold scheme gap.

Python
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline

rng = np.random.default_rng(1)
X, y = rng.normal(size=(60, 200)), rng.integers(0, 2, 60)
leaky = SelectKBest(f_classif, k=10).fit_transform(X, y)
print("selected outside", cross_val_score(LogisticRegression(), leaky, y, cv=5).mean())
inside = make_pipeline(SelectKBest(f_classif, k=10), LogisticRegression())
print("selected inside ", cross_val_score(inside, X, y, cv=5).mean())
R
set.seed(1)
X <- matrix(rnorm(60 * 200), 60); y <- rbinom(60, 1, 0.5)
top <- function(X, y) order(-abs(cor(X, y)))[1:10]
fold <- sample(rep(1:5, length.out = 60))
acc <- function(keep_fn) mean(sapply(1:5, function(k) {
  keep <- keep_fn(k)
  d <- data.frame(y = y, X[, keep])
  fit <- glm(y ~ ., binomial, d[fold != k, ])
  mean((predict(fit, d[fold == k, ], type = "response") > 0.5) == y[fold == k])
}))
outside <- top(X, y)
c(selected_outside = acc(function(k) outside),
  selected_inside = acc(function(k) top(X[fold != k, ], y[fold != k])))
08

Recap

Recap
  • Four leaks: future or proxy columns, identifiers, repeated entities, and fitting outside the folds.
  • A score that's too good is a question to investigate, not a result to report.
  • The exclusions and the scan catch single columns. The fold check and your own reasoning catch the rest.

Concepts in this lesson