# Pipelines: Every Step, Every Time

A pipeline chains the preparation steps and the model into one object, so make_pipeline(SimpleImputer(), StandardScaler(), LogisticRegression()) fits every step on the data it's given with one fit, repeats the same steps with one predict, names each step for named_steps, and refits every step inside each fold when it's handed to cross_val_score.

Journeyman · AI Basics · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Build a pipeline with make_pipeline that prepares the data and trains a model in one object, and use it to fit, predict and read its named_steps
- Explain why a pipeline inside cross_val_score fits its preparation steps again on each fold's training part

## Key takeaways

- A pipeline is one object that holds the preparation steps and the model, in order. One fit trains them all, and one predict runs them all.
- Each step learns only from the data the pipeline's fit is given. predict uses what each step learned, and learns nothing new.
- make_pipeline names each step after its class in lowercase, and named_steps finds a fitted step by its name. Pipeline lets you choose the names.
- Handed to cross_val_score, a pipeline is fit again on each fold's training part, so no fold's scaler or imputer has seen the rows it's scored on.

The last three lessons each added a step that learns from the training data: a scaler, an encoder, an imputer. In a real project that's a lot of objects to fit in the right order, on the right rows, and then repeat exactly for every new example, and one slip (fitting the scaler on the wrong table, forgetting to fill a blank before predicting) quietly breaks the model. You'll put every model you hand to someone else in a pipeline.

The example is Happy Tails Pet Adoption's `pets.csv`, from *Exercise: Adoption Pipeline*. Each code block is one notebook cell, run in order: the first one loads the data.

## Steps that have to stay in step

Without a pipeline, scoring the test pets takes six calls in a fixed order: fit the imputer on the training pets, fill them, fit the scaler on the filled pets, scale them, fit the model, and then for the test pets fill, scale and predict with the objects already fitted. A **pipeline** is a recipe card for that work: one object that holds the steps in order, so they always run in that order on the same rows. `make_pipeline` builds one from the steps:

```python
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

pets = pd.read_csv("pets.csv")
X = pets.drop(columns=["pet_id", "adopted_in_30_days"])
y = pets["adopted_in_30_days"]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, stratify=y, random_state=42
)
numbers = ["age_years", "weight_lbs", "has_photo"]

model = make_pipeline(SimpleImputer(strategy="median"), StandardScaler(), LogisticRegression())
model.fit(X_train[numbers], y_train)
print("Test accuracy:", round(model.score(X_test[numbers], y_test), 3))
```

```text
Test accuracy: 0.715
```

The pipeline behaves like one model. `fit` fits the imputer on the training pets and fills them, fits the scaler on the filled pets and scales them, then fits logistic regression on the result. `score` and `predict` fill and scale the test pets with what the steps learned, then hand them to the model; they learn nothing new. Every step but the last has to be a *transformer* (it has `fit` and `transform`); the last can be any model.

![A pipeline of three boxes, fill blanks with the median, standardize, and logistic regression. Along the top, fit sends the 600 training pets through all three, and each box learns from them: the medians, the means and standard deviations, the weights. Along the bottom, predict sends new pets through the same three boxes, which use what they learned and learn nothing new, and a guess comes out](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/d9f261730a9a44e1b274fd0cc4942558.webp)

## New pets get the same treatment

A new arrival with a blank weight goes straight in. The pipeline fills and scales it before the model sees it:

```python
new_pets = pd.DataFrame({"age_years": [0.8, 9.0], "weight_lbs": [None, 60.0], "has_photo": [1, 0]})
print(model.predict(new_pets))
```

```text
[1 0]
```

The puppy with a photo and no recorded weight is guessed to be adopted within 30 days (1); the 9-year-old, 60-pound dog without a photo isn't (0). Nobody had to remember to fill the blank or scale the numbers.

## Step names and named_steps

`make_pipeline` names each step after its class, in lowercase. `named_steps` finds a fitted step by that name, to look at what it learned:

```python
print(list(model.named_steps))
print("Medians:", model.named_steps["simpleimputer"].statistics_)
print("Means:", model.named_steps["standardscaler"].mean_.round(2))
```

```text
['simpleimputer', 'standardscaler', 'logisticregression']
Medians: [ 3.2  11.25  1.  ]
Means: [ 3.87 22.67  0.68]
```

The medians are the ones *Missing Values in a Model* found from the 600 training pets: the pipeline fit them on exactly the rows it was given. To choose shorter names, build a `Pipeline` from a list of (name, step) pairs:

```python
from sklearn.pipeline import Pipeline

named = Pipeline([
    ("fill", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
    ("model", LogisticRegression()),
])
named.fit(X_train[numbers], y_train)
print(named.named_steps["model"].coef_.round(2))
```

```text
[[-1.21 -0.35  0.71]]
```

Each name must be unique. *Searching for Good Settings* uses these names to reach a step's settings. In a notebook, a pipeline left as a cell's last line is drawn as a diagram of its steps; `print(model)` shows the same thing as text.

## A pipeline inside cross-validation

*Cross-Validation* scored a model on five folds in turn. Hand it a pipeline, and each fold fits the whole pipeline again on that fold's training part, the imputer and scaler included:

```python
from sklearn.model_selection import cross_val_score

scores = cross_val_score(model, X_train[numbers], y_train, cv=5)
print("Fold scores:", scores.round(3))
print("Mean:", round(scores.mean(), 3), "Spread:", round(scores.std(), 3))
```

```text
Fold scores: [0.733 0.7   0.717 0.725 0.75 ]
Mean: 0.725 Spread: 0.017
```

To see that each fold really learned its own stand-ins, `cross_validate` can hand back the five fitted pipelines:

```python
from sklearn.model_selection import cross_validate

results = cross_validate(model, X_train[numbers], y_train, cv=5, return_estimator=True)
for fitted in results["estimator"]:
    print("This fold's median weight:", fitted.named_steps["simpleimputer"].statistics_[1])
```

```text
This fold's median weight: 11.3
This fold's median weight: 11.4
This fold's median weight: 11.2
This fold's median weight: 11.0
This fold's median weight: 11.2
```

Five medians, each from the 480 pets that fold trained on, never from the 120 it was scored on. Had the imputer been fit on all 600 pets first, every fold's score would include pets whose weights helped shape the stand-in, a small leak of the answers. The *Data Leakage* chapter shows how big such leaks can get; the pipeline is how you avoid them.

## Check yourself

2 questions to try on the lesson page.

## So what

A pipeline makes the honest order the only order: every step learns from the rows it's fit on, the same steps run on every new example, and cross-validation refits all of them in every fold.

## Sources

- scikit-learn 1.9.1, [Pipeline](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html), API reference: a sequence of data transformers with an optional final predictor; intermediate steps must implement `fit` and `transform`; `fit` fits the transformers one after the other, transforming the data, then fits the final estimator; `predict` calls `transform` of each transformer, then the final estimator's `predict`; `steps` is a list of (name, estimator) tuples; `named_steps` accesses the steps by name.
- scikit-learn 1.9.1, [make_pipeline](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.make_pipeline.html), API reference: the steps' names are set to the lowercase of their types automatically.
- scikit-learn, [Pipelines and composite estimators](https://scikit-learn.org/stable/modules/compose.html), User Guide: pipelines give convenience (one `fit` and `predict`), joint parameter selection, and safety, avoiding leaking statistics from test data into the trained model in cross-validation by ensuring the same samples train the transformers and the predictor.
- scikit-learn, [Cross-validation: evaluating estimator performance](https://scikit-learn.org/stable/modules/cross_validation.html), User Guide: preprocessing should be learned from a training set and applied to held-out data, a Pipeline provides this under cross-validation, and an integer `cv` uses `StratifiedKFold` for a classifier.

## Key terms

- [Pipeline](https://www.ask-codey.com/glossary/pipeline-python)
- [make_pipeline()](https://www.ask-codey.com/glossary/make-pipeline)
- [fit()](https://www.ask-codey.com/glossary/fit)
- [predict()](https://www.ask-codey.com/glossary/predict)

## Related

- [Different Steps for Different Columns](https://www.ask-codey.com/lessons/ml-column-transformer)
- [Missing Values in a Model](https://www.ask-codey.com/lessons/ml-missing-values-in-a-model)
- [Scaling Features](https://www.ask-codey.com/lessons/ml-scaling-features)
- [Exercise: Adoption Pipeline](https://www.ask-codey.com/lessons/exercise-ml-adoption-pipeline)

Source: https://www.ask-codey.com/lessons/ml-pipelines
