Volume 04 and every split lesson warned: fit transforms on training data only. A sklearn Pipeline enforces that mechanically by chaining preprocessing and model into one estimators you fit, predict, and serialize once.
For AI engineers, the pipeline is the deployable unit—the same object in notebooks, batch retrains, and joblib artifacts behind your API.
Learning Objectives
By the end of this lesson, students should be able to:
- Build
PipelineandColumnTransformerchains for tabular data. - Explain why pipelines prevent train/test leakage from preprocessing.
- Use pipelines with
cross_val_scoreandGridSearchCV. - Serialize and load models with
joblibfor serving. - Inspect intermediate steps with
named_stepswhen debugging. - Treat the pipeline as the single contract between train and production.
Pipeline Anatomy
A Pipeline lists (name, transformer_or_estimator) tuples. All steps except the last must implement fit and transform; the final step is a predictor with fit and predict.
| Component | Role in pipeline |
|---|---|
SimpleImputer | Fill missing values (fit on train) |
StandardScaler | Scale numerics (fit on train) |
OneHotEncoder | Encode categoricals (fit on train) |
ColumnTransformer | Apply different prep per column group |
| Classifier / regressor | Final estimator |
End-to-End Tabular Pipeline
Saving scaler and model in two files risks version skew at deploy. Serialize one pipeline object; serving calls pipeline.predict(raw_features) only.
Pipelines in CV and Hyperparameter Search
Pipelines compose with every tool in this module. Hyperparameters target steps via double underscores: clf__max_depth, prep__num__imputer__strategy (when needed).
Without Pipeline
- Manual fit/transform ordering
- Easy to leak test stats
- Train/serve skew risk
With Pipeline
- One
fit/predictAPI - CV-safe by construction
- Single serialized artifact
CI test: fit pipeline on sample rows, dump, load, assert predictions match on frozen fixture. Catches sklearn version bumps and column order regressions.
Knowledge Check
- Short Answer: Why use a Pipeline? Answer: Chains prep and model; prevents leakage and simplifies deploy.
- True/False: Only the final pipeline step may have
predict. Answer: True for standard predict API. - Multiple Choice: Leakage-safe CV requires: (a) prep outside CV, (b) full pipeline inside CV, (c) test fit on imputer. Answer: (b).
- Short Answer: What does
ColumnTransformerdo? Answer: Runs different transformers on different column subsets. - Short Answer: Why one joblib file? Answer: Guarantees prep and model versions stay matched in production.
- True/False:
model.named_steps["clf"]accesses the classifier. Answer: True. - Multiple Choice:
handle_unknown="ignore"belongs in: (a) RandomForest, (b) OneHotEncoder, (c) StandardScaler. Answer: (b). - Short Answer: Volume 04 connection to pipelines? Answer: Pipelines enforce fit-on-train-only for all learned transforms.
- Short Answer: Nested Pipeline inside ColumnTransformer — why? Answer: Multiple steps per column group (impute then encode).
- Multiple Choice: Production inference should call: (a) scaler.transform then model.predict manually, (b) pipeline.predict on raw features, (c) refit on request. Answer: (b) with a fitted pipeline.
Key Takeaways
- Pipelines bundle preprocessing and model into one leakage-safe estimator.
ColumnTransformerhandles mixed tabular dtypes cleanly.- Serialize the whole pipeline for train/serve parity.
- Next: Model Evaluation—module capstone tying splits, metrics, and ship criteria together.
Hands-on idea: Students break a notebook that fits scaler globally, then rebuild with Pipeline + joblib round-trip test asserting identical predictions.
Discussion prompt: Your API receives JSON features. Where should JSON→DataFrame conversion live relative to the pickled pipeline?
Recap: A pipeline bundles preprocessing and the estimator so transforms fit only on training data. Continue with Model Evaluation.