Tinrate Wiki

Why should you split data into training and test sets before preprocessing, and what is data leakage?

Data leakage occurs when information from outside the training data influences the model, which inflates performance estimates. Splitting first and fitting preprocessing steps only on the training set helps keep evaluation honest.

What is data leakage?

Data leakage happens when a model gains access to information during training that it would not have at prediction time, or when test data influences training. The model then appears more accurate than it really is.

Common causes

  • Scaling or imputing before splitting. Computing a mean or standard deviation on the full dataset lets test data shape the training transformation.
  • Feature selection on the full dataset. Choosing features using test labels biases the result.
  • Including future information. Using variables that would only be known after the event being predicted.
  • Duplicate or near-duplicate records appearing in both training and test sets.

Why splitting first matters

The test set should represent unseen data. If preprocessing statistics are computed before the split, the test set has already influenced the training pipeline. Splitting first, then fitting every transformation only on the training portion, keeps the evaluation realistic.

Good practice

  • Split the data early in the workflow.
  • Wrap preprocessing and modelling in a single pipeline so it is fitted correctly within each cross-validation fold.
  • Keep the test set untouched until final evaluation.
  • Check time-ordered data by splitting chronologically rather than randomly.

When to ask an expert

For time series, grouped, or highly imbalanced data, the correct splitting strategy can be less obvious. A experienced practitioner can help design a split that matches how the model will actually be used.

Ask an expert in Data Science

All Data Science experts and questions

Why should you split data into training and test sets before preprocessing, and what is data leakage? | Tinrate Wiki