Pipelines

The object that makes leakage impossible by construction, because every fold fits its own preprocessing.

Pipelines

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

A pipeline chains transformers and a final estimator into one object with one fit and one predict.
Cross-validating the pipeline refits every step on each fold's training portion. That is what makes leakage structurally impossible.
It also removes the train/serve gap: the same object that was fitted is the thing you save and deploy.
ColumnTransformer applies different steps to different columns, which is how real tables get handled.

Overview

Many preprocessing steps learn from data. A scaler estimates means and standard deviations; an imputer estimates fill values; a feature selector uses the target. Fit those steps before splitting and information from validation or test rows can influence the model. A pipeline fits each step on the training portion and applies it to held-out rows.

In this guide
  1. Worked example: feature selection before cross-validation
  2. Compose steps for different columns
  3. What pipelines do not solve
  4. Sources and next steps

Worked example: feature selection before cross-validation

Suppose 300 rows contain only random features and random labels. If you select the 20 features most correlated with the labels using all 300 rows, then cross-validate a model, each validation fold has already influenced which features were selected. The accuracy can look better than chance even though the data has no predictive signal.

Put feature selection and the model in a pipeline passed to cross-validation. Each fold then selects features using its training rows only and measures on its held-out rows. A pipeline does not guarantee a correct evaluation by itself: the split must still reflect the task, and repeated tuning on the same validation results can overfit those results.

Compose steps for different columns

In scikit-learn, Pipeline chains transformations and a final estimator. ColumnTransformer can route numeric columns through an imputer and scaler, categorical columns through an imputer and encoder, and pass the combined representation to a model. Pipeline parameters use names such as scale__with_mean or model__C, so a search can compare preprocessing and model settings together.

For example, a numeric scaler should be fitted only on each training fold. The fitted pipeline then applies those stored statistics to validation data. At deployment, save the fitted pipeline and pass raw records through it, which reduces the chance that training and serving use different preprocessing code.

What pipelines do not solve

  • Wrong split strategy: use group-aware splits for related records and time-aware splits for future prediction.
  • Target or future leakage: a feature that contains the answer or information unavailable at prediction time remains invalid inside a pipeline.
  • Resampling in ordinary scikit-learn pipelines: resampling must happen on training folds only; use an imbalanced-learn pipeline for samplers such as SMOTE.
  • Every transformation needs fitting: a fixed log transform has no learned parameters, though keeping it in the workflow may still improve reproducibility.

Sources and next steps

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does cross-validating a pipeline do that cross-validating a model does not?

  2. Why does a pipeline remove the train/serve gap?

  3. What does a pipeline NOT protect you from?

Cheat sheet

Pipelines

Chain preprocessing and modelling into one pipeline object that prevents data leakage and locks every step into the correct order.

MACHINE LEARNING · vizlearn.in/machine_learning/ml_pipelines.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.