A student standardises features and applies PCA on the full dataset before running 5-fold cross-validation, obtaining 92% accuracy, whereas fitting both inside each fold yields 81%. Why does the first setup leak information, how does a pipeline fix it, and why is supervised feature selection before CV even more damaging?

ML systems and evaluation, Data leakage
Level L3 Intermediate. Length Short. Nature Th+Mat Both.

What's being asked

Whether you understand how out-of-fold statistics leak into validation splits and how supervised selection magnifies the bias.

Key idea

Any preprocessing step whose parameters depend on the data—means, variances, PCA components or feature scores—is part of the fitted model; estimating it on the full dataset lets validation points influence the representation before evaluation.

Steps

  1. Explain unsupervised leakage: computing global mean, standard deviation or PCA directions across all rows allows the validation fold’s distribution (its outliers and covariance structure) to shape the training features.
  2. Explain the pipeline fix: wrap standardiser, PCA and classifier in a pipeline so `.fit()` estimates preprocessing parameters strictly on the 4 training folds and `.transform()` applies those fixed parameters to the held-out validation fold.
  3. Contrast with supervised feature selection: selecting the top-k features correlated with target labels y across all n samples uses the validation labels directly, so even pure random noise features when p≫n (far more features p than samples n) can look highly predictive in CV.

Common mistakes

How long your answer should be

4–5 sentences.

Last checked on 4 December 2026.

Read this

Previous in ML systems and evaluation: For true values y=[2,4,6,8] and linear-regression predictions y^=[3,3,7,7] (fitted with p=1 feature), what are R2 and adjusted R2, how can test-set R2 become negative, and why does adding a pure-noise feature never reduce training R2 whereas adjusted R2 penalises it?
Next in ML systems and evaluation: Why does reporting the best validation score from k-fold hyperparameter tuning give an optimistically biased error estimate, how does nested cross-validation fix this, and how many total model fits are required for 5 outer folds, 4 inner folds and 6 hyperparameter candidates?