Concepts / Using Pipelines with Grid Search for Model Comparison

Using Pipelines with Grid Search for Model Comparison

A Pipeline is built from a list of (step name, estimator or transformer) tuples, and the list order defines the sequence of data processing.

  • Programming

Reading the Pipeline Line

You will meet one line of Python again and again when you combine model selection with preprocessing. It builds a Pipeline out of two pieces: a scaler that prepares the data and a classifier that learns from it. Before reading any explanation, look at what the line constructs and predict what the object will do when you give it data. The rest of this article unpacks exactly what happens when Python runs that line, and how the same object can then be compared against alternatives.

python

What a Pipeline Is Built From

Definition

A Pipeline is built from a list of (step name, estimator or transformer) tuples, and the list order defines the sequence of data processing. Each tuple pairs a step name, which is a string you choose, with an object that performs that step.

In the line above, 'preprocessing' is the name of the first step and StandardScaler() is the object that performs it. 'classifier' is the name of the second step and SVC() is the object that performs that one. The name is the label you use to refer to the step later, and the object is the work the step actually does.

Once a pipeline exists, you can read its structure back. The steps attribute holds the list of (name, object) pairs you supplied, in the same order. Because the order is preserved, the first element of that list is the first step the data passes through. Printing it is the quickest way to confirm what you actually built.

python

How Data Moves Through the Chain

Think of the pipeline as a conveyor with ordered stations. Raw features enter the first station, the preprocessing step, and whatever that station produces is what gets handed to the next station, the classifier. The classifier never sees the raw features directly in this pipeline. Its input is whatever the preprocessing step produces. That is the practical meaning of a sequence of data processing steps.

Read the diagram from left to right. The data reaches the classifier only after the scaler has processed it. Placing the preprocessing inside the pipeline keeps that work in a single ordered chain, so the same ordering is applied every time the pipeline is trained or used to predict.

Why Step Names Matter

Naming each step explicitly gives every part of the pipeline an identity you can refer to later. The name is not decoration. When you want to change a setting inside one step, or ask the search procedure to vary that step, you need a stable way to point at it. The name is that pointer.

When a parameter lives inside a step, you address it by combining the step name with the parameter name. In scikit-learn this is written with a double underscore between the two parts, so the C setting of the classifier step is written as classifier__C. The part before the underscores names the step, and the part after names the parameter of that step's object.

python

Deciding Whether to Scale

The reason the pipeline has a preprocessing step at all comes from the estimator. The SVC might need the data to be scaled, so scaling is part of the work that must happen before the classifier sees the data. The RandomForestClassifier, by contrast, needs no preprocessing. Different estimators may therefore require different preprocessing, and the pipeline is the place where you express that difference.

EstimatorPreprocessing step in the pipelineReason given in the source
SVCStandardScaler before the classifierThe SVC might need the data to be scaled
RandomForestClassifierNo preprocessing stepNo preprocessing is necessary for it

Two estimators compared on the iris dataset, with different preprocessing requirements

The practical question to ask for any estimator is whether its input needs to be prepared before learning. If the estimator is one that the source says needs scaling, the scaler belongs in the pipeline ahead of it. If the source says no preprocessing is necessary, adding a scaler is not required. Deciding this per estimator, and writing it into the pipeline, keeps the comparison honest.

Searching Over Pipeline Steps

A pipeline can be treated as a fixed recipe, but grid search can also go one step further. GridSearchCV can search over the actual steps being performed in the pipeline, such as whether to use StandardScaler or MinMaxScaler, or whether to use no preprocessing at all. In this approach the choice of preprocessing becomes one more thing being compared, alongside the classifier's own settings.

python

Here the key 'preprocessing' replaces the whole step object, so the search compares scalers as alternatives, and the value 'passthrough' stands for using no preprocessing. The key 'classifier__C' still varies a setting inside the classifier. Every combination of these choices is one candidate in the comparison.

Mistakes When Reading Pipeline Results

  • Common mistake

    Assuming the classifier receives the raw features

    What it looks like

    Reading the pipeline as if SVC() sees the original columns directly

    Why it fails

    In a pipeline the classifier's input is whatever the preprocessing step produces

    Do this instead

    Trace the chain from the first step to the last and name the data each step receives

  • Common mistake

    Writing a parameter key with a step name that does not exist

    What it looks like

    Using a key such as svc__C when the step is named 'classifier'

    Why it fails

    The step name is the pointer to the step, so a wrong name points at nothing

    Do this instead

    Print pipe.steps and copy the step names exactly from that output

  • Common mistake

    Adding a scaler to every estimator by habit

    What it looks like

    Placing StandardScaler in front of a RandomForestClassifier out of routine

    Why it fails

    The source states that no preprocessing is necessary for the RandomForestClassifier

    Do this instead

    Decide per estimator whether preprocessing is needed before adding a step

  • Common mistake

    Forgetting that the step order defines the processing order

Tracing a Surprising Result

When a pipeline gives a result you did not expect, the most useful habit is to find the step where the data stopped looking the way you intended. Because a pipeline is an ordered chain, you can walk it from the first step to the last. Start by printing the step names with pipe.steps, then ask for each step whether it received the input you assumed.

  1. 1
    Print pipe.steps and confirm the names and their order
  2. 2
    For the first step, ask what raw input it received and what it produced
  3. 3
    Pass that output to the next step and repeat the same question
  4. 4
    Stop at the first step whose output differs from what you intended
MEDIUM

A teammate's pipeline has steps in the order classifier first and then preprocessing. Predictions look wrong. Using the steps order, explain where you would look first and what you would check.

Hints
  • The list order defines the sequence of data processing
  • Ask what the classifier receives when it is the first step

Key Takeaways

  • A Pipeline is a list of (step name, object) tuples, and the list order defines the sequence in which data is processed
  • Step names give each part of a pipeline an identity, so you can refer to a step and its parameters later, such as classifier__C
  • Whether a preprocessing step such as StandardScaler is needed depends on the estimator, so the pipeline is where that difference is expressed
  • GridSearchCV can compare preprocessing steps as candidates, but the larger search space should be planned carefully
  • When results look wrong, walk the steps in order with pipe.steps and find the first step whose output differs from what you expected

Related concepts

More concepts starting with U