Top 10 Most Important Machine Learning Topics · Question 03

Why do we use training, validation and test sets, and how can data leakage occur between them?

Interview preparation resource from Gate Smashers.

Interview-ready answer

The training set fits model parameters, the validation set supports model selection and hyperparameter tuning, and the test set provides a final unbiased estimate after all choices are fixed. Leakage occurs when information unavailable at prediction time, or information from validation/test data, influences training. I prevent it by splitting at the correct entity and time boundary, fitting every preprocessing step only on training data, using pipelines, and keeping the test set untouched until final evaluation.

Most Important Machine Leaning Topics diagram explaining Why do we use training, validation and test sets, and how can data leakage occur between them
Role of each dataset split
SplitUsed forMust not be used for
TrainingFit model and preprocessing parametersFinal performance claim
ValidationTune hyperparameters, select models and thresholdsDirect parameter fitting
TestOne-time final evaluation of the frozen pipelineRepeated tuning or feature selection
Understand it clearly

Purpose of each split

The three splits answer different questions. Training data asks, ‘Can the algorithm learn parameters?’ Validation data asks, ‘Which model and settings should we choose?’ Test data asks, ‘How well should the final frozen procedure generalize to unseen data?’ Repeatedly checking the test result during development turns the test set into another validation set.

Common leakage paths

  • Preprocessing leakage: Scaling, imputing, feature selection or dimensionality reduction is fitted before splitting, so statistics from held-out data enter training.
  • Target leakage: A feature contains the target directly or contains information created after the outcome occurred.
  • Entity leakage: Records for the same user, patient, device or document appear in different splits.
  • Temporal leakage: Future observations or future aggregates are used to predict an earlier event.
  • Tuning leakage: The test set influences hyperparameters, thresholds, features or model choice.
  • Augmentation leakage: Near-duplicate augmented versions of one source example cross split boundaries.

Safe workflow

  1. Define the real prediction time and the information available at that moment.
  2. Split raw examples by the correct unit, such as user, group or time.
  3. Fit preprocessing and feature selection only on each training fold.
  4. Tune models using validation data or nested cross-validation.
  5. Freeze the complete pipeline and decision threshold.
  6. Evaluate once on the untouched test set.

Cross-validation nuance

Random K-fold cross-validation is not automatically correct. Use stratified folds for imbalanced classification, group-aware folds when entities repeat, and time-series validation when the future must be predicted from the past.

Why it matters

Leakage produces an optimistic metric rather than a better model. It is especially dangerous because the evaluation can look excellent while the production system fails immediately.