Cross-Validation and Model Evaluation
Information leakage occurs when information from the test folds influences training, which makes cross-validation scores overly optimistic.
When a Strong Score Misleads
Cross-validation is meant to answer a specific question: how well will a model perform on data it has never seen? It does this by repeatedly holding out part of the data as a test fold, training on the remaining rows, and scoring on the held-out rows. The answer it gives is only trustworthy if the test folds stay untouched by the training process. When that separation breaks, the score you measure is inflated, and it can look like a success even when the model has learned nothing useful.
What do you think happens?
Suppose the features and the target were generated completely independently, so nothing in the inputs can really explain the target. You select the most correlated features using the whole dataset, then run cross-validation on a Ridge regressor using only those selected features. What kind of score do you expect?
Reveal answer
An optimistic score that looks better than chance, even though the data contains no real signal.
The selection step was fitted on every row, including rows that later served as test folds. The features it kept were partly chosen because of patterns in the test rows, so the held-out score reflects that choice rather than a real relationship. The source describes this as an overly optimistic cross-validation score caused by information leakage.
This article works through a deliberately unusual case. The data contains no real signal at all, so any model that scores well on it is scoring well for the wrong reason. Watching how such a score can appear is the clearest way to understand how leakage works and where to look for it.
The Belief Being Tested
Many people assume that a preprocessing step such as feature selection is harmless as long as the model itself is evaluated with cross-validation. The reasoning usually goes like this: the selector only chooses columns, the Ridge regressor is the part that actually learns, and so the cross-validation score reflects the regressor's real ability. This is the belief the case study is designed to test.
A Dataset With No Signal
The starting data is a synthetic regression task with 100 samples and a large number of features. Every feature is drawn independently from a Gaussian distribution, and the response is also drawn from a Gaussian distribution. Because of how the data was created, there is no relationship between the features and the target. In principle, nothing can be learned from it, so an honest evaluation should show performance close to nothing.
The leak in this case is subtle because nothing looks wrong on the surface. The code still splits the data into folds, still trains a regressor on training rows, and still scores it on held-out rows. The problem is hidden earlier in the workflow, in a step that touched every row before the split happened. A reader who only checks that the splitting, training, and scoring look correct will miss it.
Tracing the Improper Workflow
The improper workflow runs in a fixed order. First, feature selection is fitted on the full dataset, so every row, including the rows that will later serve as test folds, contributes to deciding which features survive. Second, the reduced dataset is handed to cross-validation, which splits it into folds and fits the Ridge regressor on each training portion. The diagram below traces how data moves through those two stages.
The highlighted selection step is where the leak enters. The test folds were not held out before selection, so the selector could pick features that happen to be correlated with the target across both the training and test rows. When the Ridge regressor is later scored on those test rows, the score reflects a pattern that was partly chosen using the test rows themselves.
Following the rows through the improper run
Trace which rows influenced each stage of the improper workflow on the 100-sample, no-signal dataset.
- 1Stage one: selection
The selector is fitted on all 100 rows. Its choice of features depends on correlations computed across the whole table.
- 2Stage two: splitting
Only after selection is the reduced table divided into folds. The fold assignments no longer protect the held-out rows, because those rows already shaped which columns remain.
- 3Stage three: training
The Ridge regressor is fitted on each training portion. It only sees the features that selection kept.
- 4Stage four: scoring
The regressor is scored on held-out rows. Those rows were part of the data that chose the features, so the score is inflated.
The test rows influenced training through the feature selection step, which happened before any split.
Why Random Data Exposes the Problem
Random data makes the leak easy to see. Among thousands of random features, some will be correlated with the target purely by chance. Selection done on all the rows will find those chance correlations and keep them, and the test folds will then appear to confirm them. The confirmation is not evidence of learning, because the pattern was manufactured by the selection step rather than discovered in unseen data.
This is why the source's observation is useful beyond this one case. A score that looks too good, on data that should contain no signal, is a direct signal that some step has been fitted on rows it should not have seen. The strength of the score does not tell you what went wrong; only tracing the workflow does.
Repeating Selection Inside Each Fold
A pipeline is a sequence of data processing steps that run together. In this case, the pipeline contains a feature selector followed by a Ridge regressor. When that pipeline is passed to cross-validation, each fold repeats the whole sequence using only that fold's training rows. The selector is therefore fitted inside the loop, and the test fold is never consulted while choosing features.
The key change is not the choice of model or the scoring rule. It is the position of the selector. By moving selection inside the loop, every fold makes its own feature choices from its own training rows, and the held-out rows enter the process only when the finished pipeline is scored on them.
Comparing the Two Scores
In the source's comparison, the pipeline version produces a far less flattering number, and that is the expected outcome, not a failure. Once the selector can only see training folds, the chance correlations it finds in those folds do not carry over to the held-out data. A realistic estimate on random data should be close to nothing, and the pipeline gives exactly that kind of estimate. The lower score is the more honest one.
Tracing a Suspicious Score
When a cross-validation score looks too good, the useful question is not whether the model is good but where the result started to diverge from what the data can support. Trace the run step by step and mark every point where a transformation or selection is fitted. For each one, ask which rows it saw. If any step was fitted on rows that later appear as test rows, you have located the leak.
- 1List every stage of the workflow in the order it runs, from loading data to scoring.
- 2Mark each stage that is fitted on data, such as a feature selector, a scaler, or a model.
- 3For each marked stage, record which rows it was fitted on.
- 4Check whether any row used in a fitted stage later appears as a held-out row.
- 5If such a stage exists, move it inside the cross-validation loop so it is fitted only on training rows.
A colleague reports that a feature-selection step and a Ridge regressor achieve a strong cross-validation score. The selection step was fitted on the full table before the folds were created. Without retraining anything, identify which stage first received information from the test rows, and describe how you would restructure the workflow so that the test fold is not consulted during selection.
Hints
- Ask which stage was fitted before the split happened.
- Think about what a pipeline does inside each cross-validation loop.
- The model may be fine; focus on the step that touched every row.
Mistakes That Keep Leakage Alive
Common mistake Assuming that a selector is harmless because it only chooses columns
What it looks likeA feature selector is fitted on all rows, and then cross-validation is run on the reduced table.
Why it failsChoosing columns uses the target and the rows, so the selector can keep patterns that exist only in the held-out rows.
Do this insteadPlace the selector inside a pipeline so each fold fits it on that fold's training rows.
Common mistake Checking the regressor first when the score looks too good
Why it failsThe regressor may be behaving normally on the data it receives, while the leak lies in an earlier step.
Do this insteadTrace the workflow from the start and mark every point where something is fitted, then check which rows each one saw.
Common mistake Treating a high cross-validation score as proof of a good model
Why it failsCross-validation only promises a realistic estimate when the test folds stay untouched by training. A leak makes the score optimistic.
Do this insteadVerify the workflow first, and remember that a score is only as trustworthy as the separation it was measured under.
Common mistake Preprocessing outside the loop because it seems like a one-time setup step
Key Takeaways
- A good cross-validation score is only trustworthy when test folds stay untouched by the training process; otherwise information leakage makes the score overly optimistic.
- Feature selection fitted on the full dataset before splitting lets held-out rows shape which features the model trains on, even when the data is pure random noise.
- To locate a leak, trace the workflow step by step, mark every fitted stage, and check which rows each stage saw.
- A Pipeline keeps feature selection inside each cross-validation loop so the selector sees only training folds, producing a lower but realistic score.