Creating Processing Pipelines
The pipeline result matches the result of performing the transformations by hand, which makes it a reliable check of the chained process.
Asking How a Pipeline Performs on Unseen Data
Once a pipeline has been built and fitted, the natural next question is how well it performs on data it has not seen. A pipeline bundles a preprocessing step, such as a scaler, together with a classifier, such as an SVM. The useful thing about this bundle is that you can treat it as one object and ask it for a score. You do not have to manage the individual pieces yourself every time you evaluate.
The rest of this article follows a single call to score. By the end, you should be able to say exactly what the pipeline does with the test data, why its answer agrees with the answer you would get by doing the steps yourself, and why this same object can be handed to cross-validation and grid search tools.
Tracing a Score Call Through Two Stages
When score is called on the pipeline, the work happens in two stages. In the first stage, the test data is transformed using the scaler. In the second stage, the score method of the SVM is called on that scaled test data. The pipeline does not score the raw test features directly. The estimator only ever sees the output of the transformation.
What do you think happens?
When you call score on a pipeline with raw test features, which component touches the data first?
Reveal answer
The scaler, which transforms the test data before the SVM sees it
The pipeline calls the scaler first to transform the test data. Only then does it call score on the SVM, using the scaled data as input.
Follow the diagram from left to right. The raw test features enter the pipeline and reach the scaler first. The scaler's output, the scaled test features, is what the SVM receives. The SVM's score method then produces one number, and that number is the value the pipeline returns. Nothing in this call asks you to scale the test set yourself before passing it in.
Why the Pipeline Matches the Manual Steps
Calling score on a pipeline first transforms the test data with the scaler, then calls score on the SVM using the scaled data.
Earlier in the chapter, the same evaluation was carried out by hand: the test data was transformed, and then the score method of the estimator was called on the result. The source notes that the score produced by the pipeline is identical to that manual result. This is the check that gives you confidence in the pipeline. It performs the same work as the manual steps, but through a single call.
| Stage | Manual evaluation | Pipeline evaluation |
|---|---|---|
| Transform test data | You apply the scaler to the test features yourself | The pipeline applies the scaler as its first stage |
| Score the classifier | You call score on the SVM with the scaled data | The pipeline calls score on the SVM with the scaled data |
| Result | A score from the two separate calls | A single score from one call |
| Your responsibility | Remember to transform the test set first each time | The pipeline carries its preprocessing with it |
The manual and pipeline evaluations perform the same two stages in the same order.
The two approaches agree because they run the same stages in the same fixed order. The pipeline does not change the arithmetic; it only packages it. Because the data flows through the preprocessing and then the classifier in that order, the single number returned by the pipeline reflects both parts of the process.
Checking a pipeline against the manual steps
You have fitted a pipeline with a scaler followed by an SVM, and you want to confirm that it does the same work as the manual procedure on a test set.
- 1Run the manual procedure
Transform the test features with the fitted scaler, then call score on the fitted SVM with those transformed features. Record the result.
- 2Run the pipeline
Call score on the whole pipeline with the same raw test features. The pipeline transforms the features with the scaler and then scores the SVM.
- 3Compare the two results
If the pipeline's score equals the manual score, the chained process is behaving as expected. This is the reliability check the source describes.
The pipeline score and the manual score agree, so the single pipeline call can be trusted as a substitute for the separate steps.
Locating the Source of a Discrepancy
Suppose the pipeline's score and your hand-computed score ever disagree. Because the estimator only ever receives the output of the transformation, the difference must come from one of two places: the transformation step, which produces the data the estimator sees, or the estimator step, which scores that data. Tracing the chain stage by stage tells you which one to examine.
Passing a Pipeline to Cross-Validation and Grid Search
The main benefit the source emphasizes is that a pipeline can be used as a single estimator in cross_val_score or GridSearchCV. Those tools expect one estimator to work with. The pipeline is that estimator, even though it contains a scaler and an SVM internally. From the tool's point of view, there is one object to evaluate, and the preprocessing is part of it.
This is what makes pipelines useful in grid searches. A grid search tries combinations of hyperparameters to find the best settings. If the search only covered the classifier, the preprocessing would sit outside the optimization. Because the pipeline is a single estimator, the optimization can cover the entire pipeline, including the preprocessing stage. The search therefore evaluates each candidate setting together with the transformation that feeds the classifier.
Mistakes Beginners Make With Pipelines
Common mistake Scaling the test set by hand before calling the pipeline's score
What it looks likeTransforming the test features with the scaler, then passing the already-scaled features to pipeline.score
Why it failsThe pipeline's first stage transforms its input. Pre-scaling the data means the scaler is applied to the test set twice, which is not the flow the pipeline is designed for.
Do this insteadPass the raw test features to the pipeline and let it perform the transformation as its first stage.
Common mistake Assuming the estimator scores the raw features
What it looks likeExpecting the SVM's score to reflect the unscaled test values
Why it failsThe source is explicit that the pipeline does not score the raw test features directly. The estimator only sees the output of the transformation.
Do this insteadThink of every score call as passing through the transformation first, then the estimator.
Common mistake Treating the pipeline as a separate, unrelated object from its steps
What it looks likeTuning only the classifier in a grid search while leaving the preprocessing out of the search
Why it failsOnly a single-estimator pipeline lets the search cover the preprocessing stage along with the classifier.
Do this insteadPass the whole pipeline to GridSearchCV so the optimization covers the entire chain.
Common mistake Believing a mismatch between pipeline and manual scores is impossible rather than something to trace
Keeping Training and Test Roles Separate
The pipeline is fitted before score is called. When score runs on test data, the test features are transformed using the scaler and then scored by the SVM. Keeping these roles distinct is what makes the evaluation honest: the test set is used to measure performance, not to shape the preprocessing or the classifier. The comparison below separates what happens during fitting from what happens during this evaluation.
Practicing the Pipeline Trace
A colleague says their pipeline score on the test set is different from a score they computed by transforming the test features with the scaler and then calling score on the SVM. Without changing any code, list the stages you would check, in the order the data travels, to locate where the difference could come from.
Hints
- Start from the raw test features that enter the pipeline.
- Ask whether the estimator ever sees raw features or only transformed output.
- Check the transformation step before the estimator step.
Key Takeaways
- Calling score on a pipeline transforms the test data with the scaler first, then calls score on the SVM using the scaled data.
- The pipeline's score matches the manual transform-then-score result because it runs the same stages in the same fixed order.
- If the two results ever disagree, trace the data flow: the estimator only sees transformed output, so the discrepancy lies in either the transformation step or the estimator step.
- Because a pipeline is a single estimator, it can be passed to
cross_val_scoreor GridSearchCV, letting hyperparameter optimization cover preprocessing as well as the classifier.