Skip to main content

How to Chain Preprocessing and a Model with a Pipeline in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A model rarely takes raw data: text has to be encoded and numbers sometimes scaled, the same way for training and for every prediction afterwards. A scikit-learn Pipeline joins those preparation steps and the model into one object, so fit and predict can't drift apart. This guide builds a tiny fruit classifier that takes raw rows, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
pipeline = Pipeline([("step_name", transformer), ..., ("model_name", model)])
pipeline.fit(X, y)
predictions = pipeline.predict(new_X)
PartWhat it means
[(name, step), ...]The steps, in order, each with a name you choose. Every step but the last prepares data; the last is the model
pipeline.fit(X, y)Fits each preparation step and passes its output to the next, then trains the model on the result
pipeline.predict(new_X)Runs the same, already-fitted preparation on new rows, then predicts

Step 1: Build and Use a Pipeline​

The example decides whether a fruit is an apple from its color and weight. Color is text, so the first step one-hot encodes it; the second is a decision tree. Create pipeline.py:

The pipeline.py script in a macOS-style code window: a fruit DataFrame with color and weight columns and is_apple labels, a Pipeline with a ColumnTransformer step named prepare that one-hot encodes color and passes weight through, and a DecisionTreeClassifier step named model; it fits the pipeline and prints predictions for a red fruit of 155 grams and a yellow fruit of 118 grams.

pipeline.py
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.tree import DecisionTreeClassifier

fruit = pd.DataFrame(
{"color": ["red", "yellow", "red", "green", "yellow", "green"],
"weight": [150, 120, 160, 140, 115, 130]}
)
is_apple = [1, 0, 1, 1, 0, 1]

pipeline = Pipeline([
("prepare", ColumnTransformer([("color", OneHotEncoder(), ["color"])], remainder="passthrough")),
("model", DecisionTreeClassifier(random_state=0)),
])
pipeline.fit(fruit, is_apple)

new_fruit = pd.DataFrame({"color": ["red", "yellow"], "weight": [155, 118]})
print(pipeline.predict(new_fruit))
Terminal
uv run python pipeline.py
Output
[1 0]

A terminal running uv run python pipeline.py, which prints the predictions 1 and 0: the red fruit is an apple and the yellow one isn't.

The new rows went in raw, with color still as words: the pipeline encoded them exactly as it encoded the training rows, then predicted an apple for the red fruit and not for the yellow one.

Step 2: Know What the Pipeline Saves You​

Without a pipeline you'd fit the encoder, transform the training data, train the model, and then remember to transform, never re-fit, every batch of new data before predicting. The pipeline does those calls in the right order every time. It also keeps cross-validation honest: tuning a pipeline with GridSearchCV fits the preparation on each training fold only, so nothing about the held-out fold leaks into it.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving a column out of the new rowsValueError: columns are missing: {'weight'}Give new rows every column the pipeline was trained with
Fitting the preparation on all the data before splittingInformation from the test rows leaks into trainingSplit first, and fit the whole pipeline on the training rows

Troubleshooting​

SymptomLikely causeFix
ValueError: columns are missing: {...}New rows lack a column used in trainingAdd the column, with the same name
AttributeError: 'Pipeline' object has no attribute 'model'Steps aren't attributes of the pipelinepipeline.named_steps["model"]

FAQ​

How do I change a setting inside a pipeline?​

Use the step's name, two underscores and the setting: pipeline.set_params(model__max_depth=3). How to Set the Parameters of a Pipeline Step in scikit-learn covers it.

Is a pipeline a model?​

It behaves like one: it has fit, predict and score, so anything that takes a model, such as GridSearchCV, takes a pipeline.

Conclusion​

Pipeline([("prepare", ...), ("model", DecisionTreeClassifier(...))]) turned raw rows with text into predictions, [1 0], with one fit and one predict. Put every preparation step in the pipeline, give it raw rows, and fit it on training data only.

Additional Resources​

  • Pipeline — steps, named_steps and set_params.