Concepts / Using Pipelines in Grid Searches

Using Pipelines in Grid Searches

The search is therefore not measuring generalization cleanly.

  • Programming

A Routine That Looks Safe

Many people who are new to grid search follow a sensible-looking routine. They scale their features first, using a scaler such as MinMaxScaler, and then run a grid search over an SVM's parameters on the scaled data. The reasoning feels clean: scaling is just a preparation step, so it should not matter whether it happens before or during the search. This belief, that preprocessing done outside the cross-validation loop is safe, is a common misconception, and this article explains why it fails and how pipelines address it.

What do you think happens?

In the scale-first routine, at what point does information from the test fold first enter the process?

Reveal answer
Answer:

When the scaler is fit on the full dataset, before any cross-validation split is made. That fit step touches every row, including the rows that will later be held out as test folds.

The scaler learns its values from whatever data it is fit on. If that data includes the held-out rows, those rows have already shaped the preprocessing that every split inherits.

Tracing the Scale-First Workflow

Information leakage occurs when information from the test fold is used during preprocessing. In the scale-first workflow, the scaler is fit once on the full dataset before any cross-validation split is made. Its learned values then flow into every split. Each training fold and each test fold has already been transformed using statistics that came from the complete data.

Full datasetFull datasetScaler fit on allrowstest rows includedCross-validationsplitsCross-validationsplitsScaler fit ontraining splitsper splitGrid search scoresshaped by test rowsGrid search scorestest folds unseenfittransform, then splitevaluatesplitfit per splitevaluate
Where does test-fold data enter the search when the scaler is fit once up front, and how does moving the scaler inside the pipeline change that path?

Read the top diagram from top to bottom. The scaler's fit step touches every row, and that is the point where the test fold stops being separate. Everything downstream inherits that contact, so the scores at the bottom are computed on data that was prepared with knowledge of the held-out rows. The search is therefore not measuring generalization cleanly.

Refitting the Scaler Inside Each Split

A pipeline changes the order of operations. Instead of scaling once up front, the pipeline puts the scaler and the SVC together so that the grid search treats them as one estimator. For each split in the cross-validation, the MinMaxScaler is refit using only the training splits. No information from the test split enters the parameter search through the scaler. The learned scale is therefore a product of training data alone for that split.

Training splitsused to fitFit MinMaxScalertraining rows onlyTransform trainingrowssame learned scaleTrain SVCon scaled training rowsTransform test foldtransform only, no fitScore SVCon scaled test fold
For one CV split, which data is used to fit the scaler, which data is only transformed, and when is the SVC trained?

Walk through the loop above for a single split. The scaler is fit only on the training rows of that split. Those same training rows are then transformed with the learned scale and used to train the SVC. The test fold of that split is only transformed with the scale that was learned from training data; it is never used to fit anything. The next split repeats the whole sequence with its own training rows, so the preprocessing is refit for every split.

In the real world

Imagine a team with a dataset of sensor readings, where each reading has been measured on a different scale. They build a pipeline that holds a MinMaxScaler followed by an SVC, then run a grid search over the SVC's parameters. For each cross-validation split, the scaler learns its minimum and maximum from that split's training rows only. The held-out rows of that split are transformed with those training-derived values, so the score reflects how the whole pipeline behaves on rows it did not learn from.

Writing the Parameter Grid

Once the preprocessing lives inside a pipeline, the parameter grid has to name the step that owns each parameter. The source uses a pipeline whose second step is an SVC named svm. Both parameters being searched, C and gamma, belong to that step. The syntax is the step name, followed by two underscore characters, followed by the parameter name. The double underscore separates the two parts, so the key for C is svm__C and the key for gamma is svm__gamma.

svmname of the pipeline step__double underscore separatorCparameter of that step
How does a key like svm__C map to a specific step and parameter inside the pipeline, and what does each part of the string refer to?
python
Notes on the code
  • svm__C names the C parameter of the step called svm
  • svm__gamma names the gamma parameter of the same step
  • The candidate values here are illustrative only

The step name is what lets grid search route each candidate value to the correct part of the pipeline. A plain key such as C, without the step name, does not say which estimator inside the pipeline should receive that value. This is why the source describes the modified syntax as a slight but necessary change from ordinary grid search.

  • Common mistake

    Using a plain key such as C when the estimator sits inside a pipeline

    What it looks like
    param_grid = {"C": [0.1, 1, 10]}
    Why it fails

    Without the step name, the grid does not say which estimator inside the pipeline should receive the value.

    Do this instead

    Use the step name and the double underscore, as in svm__C.

  • Common mistake

    Using a single underscore instead of a double underscore

    Why it fails

    The double underscore is the separator that splits the step name from the parameter name.

    Do this instead

    Write the key as step name, two underscore characters, parameter name.

  • Common mistake

    Scaling the full dataset before building the grid search, then putting only the SVC in the pipeline

    Why it fails

    The scaler is still fit on rows that include the test folds, so leakage remains.

    Do this instead

    Place the scaler and the SVC together in the pipeline so the scaler is refit on the training splits of each split.

Checking Your Understanding

EASY

A colleague built a pipeline holding a MinMaxScaler and an SVC named svm. Their grid uses the keys C and gamma. Explain what is wrong with that grid, and rewrite the keys so each one reaches the correct parameter.

Hints
  • Ask which estimator inside the pipeline should receive a plain key such as C.
  • The source's syntax is the step name, two underscores, then the parameter name.
MEDIUM

In one cross-validation split, the scaler has been fit on the training rows. Which rows are transformed with that learned scale, and which rows must never be used to fit anything? Trace the answer through the sequence of the split.

Hints
  • Consider which rows the scaler learns from and which rows it only transforms.
  • Think about when the SVC is trained relative to the test-fold scoring.

Key Takeaways in Brief

  1. 1
    Preprocessing done once before cross-validation can let test-fold information shape the parameter search, which is information leakage.
  2. 2
    A pipeline holds the scaler and the SVC together, so the grid search treats them as one estimator.
  3. 3
    For each split, the MinMaxScaler is refit using only that split's training data, so no test-fold information enters through the scaler.
  4. 4
    Grid keys name the step and the parameter with a double underscore, such as svm__C and svm__gamma.

At a glance

  • Preprocessing fit once on the full dataset before cross-validation lets test-fold information influence the search.
  • A pipeline places the scaler and SVC together so the grid search treats them as one estimator.
  • The pipeline refits the MinMaxScaler on the training splits of each cross-validation split.
  • Pipeline grid keys use the step name, a double underscore, and the parameter name, such as svm__C.

Related concepts

More concepts starting with U