Concepts / Preprocessing with Scalers

Preprocessing with Scalers

In the flawed workflow, the scaler is fit once, before any splitting happens.

  • Programming

A Tidy Workflow That Can Distort the Score

A common way to set up a model search goes like this: scale the whole dataset first, then split it, then run a grid search over candidate settings. The reasoning seems tidy. Every row receives the same transformation, so the model sees consistent inputs in every part of the search. It is easy to assume that a preprocessing step applied before the search is just bookkeeping and cannot change how the evaluation turns out.

What do you think happens?

Before reading on: can scaling the whole dataset before the split distort the score of the grid search?

  • No, scaling is just bookkeeping and cannot affect the evaluation

  • Yes, because the scaler learns parameters from rows that later serve as held-out test data

Reveal answer
Answer:

Yes. The scaler learns its parameters from every row, including the rows that later serve as held-out test data, so the evaluation is partly informed by the data it was meant to judge.

The question to ask is which rows the scaler looks at when it learns its parameters, and whether any of those rows later act as held-out test data.

This page follows one scaler through a workflow and watches where the test rows first influence it. The goal is to see how a leak enters, how it reaches the final score, and how a pipeline prevents it.

Following the Scaler Through the Workflow

When you debug a leak, the first question is where the code first touches the full dataset. In the flawed workflow, the scaler is fit once, before any splitting happens. The scaler reads every row and stores the parameters it learns. Every later step inherits those parameters, including the grid search, which means every candidate setting is evaluated on inputs that were scaled using information from the test rows.

Full datasetall rows, including futuretest rowsFit scaler on allrowslearns smallest and largestvaluesScaled full datasetparameters include testrowsSplit into foldssplitting happens afterscalingTraining foldsinputs already carry testinformationGrid searchcompares candidate settingsReported scorelooks better than it reallyisHeld-out foldscored against inputs ithelped shapeall rows readparameters appliedthen splittraining rowsheld-out rowsevery candidateevery candidatebest setting chosen
At which step does test-fold information first flow into the scaler's parameters, and how does that information then reach every candidate model and fold?

The diagram shows the key point: the leak is not in the grid search itself. It enters at the first point where the scaler is fit on the full dataset, which happens before any split. Once that happens, the grid search faithfully reports results computed from inputs that already contain test information.

How Held-Out Information Reaches the Score

Definition

Information leakage happens when information from the test split is used during training, which can make results look better than they are.

The held-out fold is supposed to play the role of data the model has never encountered. A preprocessing step that learns from that fold, such as estimating the scale of the data, quietly gives the search a head start. The model never trains on the held-out rows directly, yet the evaluation still carries their influence, because the inputs were transformed using parameters that were computed with those rows included. The grid search then picks settings that look better than they really are.

Worked example

A Scaler Fit on the Full Dataset Versus the Training Rows

One feature has four training values, 2, 4, 6, and 8, and one held-out value, 20. Compare the scale a scaler learns when it sees all five rows with the scale it learns when it sees only the training rows.

  1. 1
    Fit on the full dataset

    The scaler reads all five values. The smallest is 2 and the largest is 20, so the learned scale runs from 2 to 20.

  2. 2
    Fit on the training rows only

    The scaler reads only 2, 4, 6, and 8. The smallest is 2 and the largest is 8, so the learned scale runs from 2 to 8.

  3. 3
    Compare the two scales

    The held-out value of 20 changed the learned largest value from 8 to 20. Every training row is now scaled against a range that the test row defined, so the training inputs already reflect information from the held-out rows.

Result

Fitting on the full dataset lets the held-out value of 20 shape the parameters used for training. Fitting only on the training rows keeps the held-out value out of the learned scale.

Refitting Inside Each Cross-Validation Split

The corrected workflow does not need a separate scaling step at all. You place the scaler and the model in one pipeline, which is a sequence of data processing steps, and you hand that pipeline to the grid search. Cross-validation then handles the scaler as part of the model. For each split, the scaler is refit using only the training portion of that split. The held-out portion is transformed with the parameters learned from the training portion, but it never contributes to learning them.

Grid searchPipelineScalerModel1. send training portion of this split2. fit on training portion only3. transform held-out portion with those parameters4. fit model on scaled training portion5. score on scaled held-out portion
For each cross-validation iteration, what is fit on the training portion, what is only transformed on the held-out portion, and in what order do these steps happen?

Read the sequence from top to bottom. The scaler's parameters are learned in step two, from the training portion alone. Step three only applies those parameters to the held-out portion. Because the held-out rows never reach the fitting step, the score in step five reflects a model that was prepared without seeing the test rows. This is the contrast with the earlier workflow, where the scaler saw all the data before the search began.

Rotating the Held-Out Fold

Cross-validation divides the rows into folds and rotates which fold is held out. The example below uses three folds for illustration. In each iteration, one fold plays the held-out role and the other folds form the training portion. The pipeline must refit the scaler for each of these assignments, using only the rows that are training in that iteration.

Fold Aheld out in split 1Fold Bheld out in split 2Fold Cheld out in split 3
Which rows are in the training portion and which are in the held-out portion in each split, and how does that assignment rotate across iterations?

In split 1, Fold A is the held-out portion and Folds B and C form the training portion. The scaler must learn its parameters from Folds B and C only. In split 2, Fold B is held out, so the scaler is refit on Folds A and C. In split 3, the scaler is refit on Folds A and B. Each refit uses a different training portion, and each held-out fold is transformed only after the refit for its own split. No single scale is shared across the three iterations in a way that lets a held-out fold shape its own inputs.

Not All Leaks Are Equally Damaging

Not every leak is equally damaging. The impact depends on the nature of the preprocessing step. Estimating the scale of the data from the test fold usually does not have a terrible impact. Using the test fold in feature extraction and feature selection, however, can lead to substantial differences in outcomes. A useful way to compare steps is to ask whether the step learns parameters from the data at all, and if so, how much of the test fold it absorbs.

Preprocessing stepLearns parameters from data?Typical leakage impact if fit on full dataset
Scaler that uses global statistics (such as min or max)Yes, from the rows it is fit onUsually smaller, since it estimates the scale of the data
Feature extractionYes, from the rows it is fit onCan be substantial
Feature selectionYes, from the rows it is fit onCan be substantial

How much a leak from fitting on the full dataset tends to matter, by kind of preprocessing step, as described in the source

The table makes one point clear. A scaler and a feature selection step both learn from the data, so both can leak if they are fit on the full dataset. The size of the damage differs, and the source treats feature extraction and feature selection as the cases where a leak is most likely to produce substantial differences. Whatever the step, the safe pattern is the same: fit it inside the pipeline, on training portions only.

Mistakes That Bring the Leak Back

The leak returns easily, because the flawed order of operations looks reasonable. The following mistakes are the ones most likely to reintroduce it.

  • Common mistake

    Scaling the whole dataset before splitting it

    What it looks like

    Fit the scaler on every row, then split the scaled data into folds and run the grid search.

    Why it fails

    The scaler's parameters come from rows that later serve as held-out test data, so the evaluation is partly informed by the data it is meant to judge.

    Do this instead

    Place the scaler and the model in one pipeline and pass the pipeline to the grid search, so the scaler is refit on each training portion.

  • Common mistake

    Assuming preprocessing before the search is just bookkeeping

    Why it fails

    A step that learns parameters from data can change the evaluation, even when it is applied identically to every row.

  • Common mistake

    Assuming all preprocessing steps leak equally

    Why it fails

    The source notes that estimating scale usually matters less than feature extraction and feature selection, but any step that learns from the full dataset still needs to stay out of the held-out data.

Checking Your Own Workflow

MEDIUM

A colleague's workflow fits a scaler on the full dataset, splits the scaled data into three folds, and runs a grid search over several candidate settings. Describe the first point where held-out information enters the process, and explain how you would restructure the workflow so that the scaler is refit only on each training portion.

Hints
  • Ask which rows the scaler reads when it learns its parameters, and whether any of those rows are later held out.
  • Think about placing the scaler and the model in one pipeline and passing that pipeline to the grid search.
EASY

Two preprocessing steps are being considered for a workflow: a scaler and a feature selection step. Both learn from the data. Which one would you expect to cause a more substantial difference if fit on the full dataset, according to the source's description? Explain your reasoning in terms of what each step absorbs from the test fold.

Hints
  • The source singles out feature extraction and feature selection as the steps where leakage can make substantial differences.
  • Estimating the scale of the data usually does not have a terrible impact.

Summary of the Pipeline Fix

The flawed and corrected workflows differ in one place: where the scaler is fit. Fitting it on the full dataset before splitting lets test rows shape the parameters used for every candidate. Placing the scaler inside a pipeline gives each split its own refit, using only that split's training portion, while the held-out portion is only transformed.

At a glance

  • Information leakage happens when information from the test split is used during training, which can make results look better than they are.
  • A scaler fit on the full dataset before splitting lets held-out rows shape the parameters that every training row and candidate setting depends on.
  • A pipeline that holds the scaler and the model refits the scaler for each split, using only that split's training portion, and only transforms the held-out portion.
  • The damage from a leak depends on the preprocessing step: estimating scale usually matters less than feature extraction and feature selection, but all steps that learn from data should stay inside the pipeline.

Related concepts

More concepts starting with P