Parameter Selection using Grid Search
Cross-validation and grid search both repeat work.
Where a Tuning Search Quietly Goes Wrong
When you tune a model, the natural picture is a search over the model's own settings. You prepare the data once, perhaps transforming it or extracting features from it, and then you run a search that varies only the model. The processing steps feel like a fixed preface that has nothing to do with the search. The source for this topic explains why that instinct leads to trouble, and why the Pipeline class is the tool that corrects it.
What do you think happens?
You have a preprocessing step and a model, and you want cross-validation and a parameter search to evaluate both together. Where should the preprocessing step live so that it is treated as part of the evaluated workflow?
Reveal answer
Inside the evaluation loop, so it is part of the workflow that gets refit and scored each time.
The source identifies the misconception as treating preprocessing as a fixed preface outside the evaluation. If the processing lives outside the loop, the evaluation no longer measures the workflow you actually plan to use.
Tracing a Search That Keeps the Scaler Outside
Follow what happens to a single preprocessing step, here a scaler, when it is applied to the full data set before the data is split. The source states that a processing step left outside the repeated refitting loop means the evaluation no longer measures the workflow you actually plan to use. Trace the path below and notice where the test rows enter the picture.
In this path the scaler has already seen the test rows before the split happens. Its statistics were shaped by data that the evaluation is supposed to hold back. The score on the test rows therefore no longer represents what happens when a truly new data point arrives. The mistake lives in where the step sits, not in the model itself. The same kind of error occurs whenever a step is prepared once, outside the loop that repeatedly refits and scores the workflow.
What the Pipeline Class Packages
The Pipeline class is described in the source as a general-purpose tool for chaining together multiple processing steps in a machine learning workflow. Its purpose is to package those steps so they behave as one unit. The source describes the Pipeline as a single Python object with the fit, predict, and transform interface. From the outside, you call the same methods you would call on one model, and the object passes the work along through its steps.
The diagram shows the chain as one fixed path. Because the steps are connected in a fixed order, the chain applies transformers in the right order every time. Every new data set passes through the same path, including the test set, so no step can be skipped by accident. The source notes an important consequence: an evaluation procedure does not need to know how many steps the chain contains. It calls the same methods on the pipeline, and the pipeline passes the work along. The source also notes that this allows writing more succinct code.
Refitting the Whole Chain in Every Loop
Cross-validation and grid search both repeat work. Cross-validation refits the workflow on different portions of the data, and grid search tries different parameter settings. The source says that in both cases the evaluation repeatedly refits the workflow on different portions of the data, and that cross-validation and grid search need the whole chain so every step is evaluated consistently. Trace the nesting below to see exactly what gets rebuilt and in what order.
The loop has a specific meaning when you compare several models or processing choices. Each pass rebuilds the whole chain with a different set of choices, so the comparison is always between complete workflows. A candidate is never judged by its model alone while its preprocessing is silently held fixed. This is the reason the source points to pipelines for parameter selection: the candidates differ as whole workflows, and each candidate is simply another version of the same chained object.
Worked Example: Comparing Two Scalers
Comparing two processing choices with one pipeline definition
You want to compare two processing choices, scaler A and scaler B, each followed by the same model, using cross-validation. How do you set this up so the comparison is honest?
- 1Define the chain once
Build one pipeline whose steps are a scaler slot followed by the model. The model stays the same in every candidate.
- 2Vary only the intended choice
Let the search swap scaler A for scaler B. Keep every other part of the pipeline definition the same. The source advises keeping the pipeline definition fixed across candidates and varying only the choices you intend to compare.
- 3Run the loop over whole workflows
For each candidate and each fold, the whole chain is refit from scratch: the scaler is fit on that fold's training portion, then the model is fit on the transformed training data.
- 4Score each complete candidate
Each candidate's score reflects the full workflow, including the scaler. Because the scaler was refit inside each fold, the held-out portion never influenced the scaler statistics used on it.
- 5Compare the candidates
The two candidates are now directly comparable: both are complete workflows that differ only in the scaler choice.
A fair comparison of two scalers, each evaluated as part of the same complete workflow, with no processing step held outside the evaluation.
Mistakes That Pipelines Prevent
The source says that building the chain by hand makes errors easy to introduce. The errors it names are forgetting to apply all transformers on the test set, and not applying them in the right order. Both happen precisely when processing is treated as separate from the evaluation. The following mistakes show how each one arises and how a single pipeline object guards against it.
Common mistake Preparing the data once, before cross-validation or grid search begins
What it looks likeA scaler is fit on the full data set, and then the data is split into folds for the search.
Why it failsThe processing step is outside the loop that refits and scores the workflow, so the evaluation no longer measures the workflow you plan to use.
Do this insteadPut the processing steps inside a single Pipeline and pass that pipeline to the cross-validation or grid search.
Common mistake Forgetting to apply all transformers to the test set
What it looks likeThe training data is transformed by two steps, but the test data is transformed by only one.
Why it failsThe source identifies this as one of the hand-built errors. The model then receives test rows in a different form from the training rows.
Do this insteadLet the pipeline apply the same chain of steps to every new data set, including the test set.
Common mistake Applying the transformers in the wrong order
What it looks likeA model step is placed before a processing step that it depends on.
Why it failsThe source names the wrong order as a hand-built error, because the steps no longer form the intended workflow.
Do this insteadDefine the steps once in the intended order so the chain runs in that sequence every time.
Common mistake Searching over the model alone while holding preprocessing fixed
What it looks likeOnly the model's regularization setting changes across candidates, while the feature extraction stays as a fixed preface.
Why it failsCandidates are no longer complete workflows, and the search cannot judge choices made in the processing steps.
Do this insteadInclude the processing choices you want to compare as part of the pipeline that the search varies.
Practice: Finding the Step Outside the Loop
A colleague runs a grid search over the regularization strength of a model. Before the search begins, they fit a scaler on the full data set and transform it, and then they pass the transformed data to the search. Explain why the reported scores may not describe the workflow they actually plan to use, and describe how you would restructure the setup so the scaler is part of the evaluated workflow.
Hints
- Ask which steps are refit on each fold and which are prepared once.
- Think about which rows the scaler's statistics were computed from.
- A single Pipeline object can hold the scaler and the model together, and the search can then vary the model's settings within it.
Key Takeaways
- Preprocessing belongs inside the evaluated workflow. A step left outside the cross-validation or grid search loop means the evaluation no longer measures the workflow you actually plan to use.
- The Pipeline class packages several processing steps into one object with the fit, predict, and transform interface, so the chain behaves as a single unit and applies its steps in a fixed order.
- Cross-validation and grid search refit the whole pipeline for every candidate and every fold, so each candidate is a complete workflow that can be compared fairly.
- To compare processing and model choices, keep the pipeline definition the same across candidates and vary only the choices you intend to compare.