Pipelines and Reproducibility
A model usually consumes a representation built by earlier steps: missing-value replacement, numerical scaling, category encoding, or feature extraction. Those steps belong to the learning procedure and the saved predictor. A preprocessing pipeline keeps their order and fitted state connected to the estimator.
This series explains how to assemble that predictor, evaluate it within the right data boundaries, and preserve enough information to use or repeat it. A pipeline fits the rows you supply; it does not automatically choose a valid split or make every experiment reproducible.
Two kinds of composition
Pipeline is sequential. One step’s output becomes the next step’s input. A mean imputer can fill an age column before a standardizer learns that filled column’s mean and spread.
ColumnTransformer uses separate column branches. An age branch can contain that imputer-and-scaler pipeline, while a city branch learns a one-hot vocabulary. The branches receive their selected raw columns and join their output features for the same rows.
fit_transform↓ filled agesScaler.fit_transformfit_transform↓ category indicators2 · Estimator: Ridge.
fitThe sequential example traces four raw training inputs through imputation, scaling, and ridge. The mixed-column example adds a city vocabulary and shows the precise output layout. Follow those examples before adding more transforms.
Keep three data uses distinct
| Operation | Data it may learn from | What happens to other rows |
|---|---|---|
| Fit a candidate in one CV fold | That fold’s training inputs and permitted training targets. | Validation rows use saved transforms and supply a score. |
| Select a recipe | Development data and the declared validation procedure. | A separate final test stays outside the selection. |
| Refit and serve the chosen recipe | The intended final training pool. | Future rows are transformed and predicted without fitting new state. |
These distinctions prevent a common mistake: fitting preprocessing once on all data and then cross-validating only the estimator. The held-out rows have already influenced that representation. Pass the complete pipeline to the evaluator instead.
Other boundaries still need explicit decisions: whether a user must be unseen, whether a feature existed at the prediction time, and whether a score was used to select a model. Avoiding Leakage with Pipelines supplies a fitted-state audit and links each remaining decision to the relevant split notes.
Preserve both the predictor and the experiment
Saving only ridge’s coefficients loses the transformations those coefficients expect. Saving only constructor parameters loses the learned means and vocabulary. Conversely, a complete fitted object does not explain which data and split produced it unless that history is recorded.
A fixed seed also cannot identify a split by itself: a change in row order can assign different records to the same sampled positions. Preserve ordered row identities when you need to replay the same evaluation.
Five chapters, one complete workflow
Trace fit versus predict, see why transform order matters, and build a complete numerical pipeline in Python and C++.
Route numerical and categorical fields, nest sequential steps, inspect output names, and understand sparse combination.
Inspect which rows produced fitted statistics and separate preprocessing protection from split and information boundaries.
Package the fitted pipeline, schema, versions, and raw prediction probes; verify a reload in a fresh process.
Separate predictor replay, retraining, and robustness; preserve split identities and the full experiment record.
Build the smallest useful recipe, then extend it
Start with the transformations the data and estimator require. Inspect a small batch before and after each branch, then check the fitted statistics, output width, feature order, and sparse or dense representation. A larger pipeline is easier to understand when every step has a specific purpose.
Keep the choices of transformations tied to their own chapters: missing values, feature scaling, and categorical encoding. This series handles their composition. The cross-validation series handles how the full learning procedure is compared and reported.
References and further reading
- Scikit-learn: pipelines and composite estimators — sequential pipelines, column branches, and nested parameter access.
- Scikit-learn: common pitfalls — fitting boundaries, consistent preprocessing, and controlled randomness.
- Scikit-learn: model persistence — preserving learned objects and the environment needed to reuse them.