CodeBlanks Logo Icon CodeBlanks

Pipeline

Scaling and model are combined with Pipeline.

Pipeline

A Pipeline orders preprocessing and the model in one object. Each step has a name and an object; the last step is the model and the earlier ones are transformers:

from sklearn.pipeline import Pipeline
pipe = Pipeline([
    ("olcek", StandardScaler()),
    ("model", KNeighborsClassifier(n_neighbors=5)),
])
pipe.fit(X_train, y_train)
print(pipe.score(X_test, y_test))

make_pipeline does the same while naming the steps automatically: "standardscaler", "kneighborsclassifier".

Accessing the steps

Steps are reached with named_steps and parameters with the step__parameter notation. This is how a model inside a pipeline is tuned in a grid search:

print(pipe.named_steps["olcek"].mean_)
pipe.set_params(model__n_neighbors=7)
print(pipe.get_params()["model__n_neighbors"])   # 7

The double underscore (__) separates the step name from the parameter name.

Practice Named pipeline

Build and train a Pipeline on Iris from the steps ("olcek", StandardScaler()) and ("model", KNeighborsClassifier(n_neighbors=3)). Print the test accuracy with round(..., 3), then the length of named_steps["olcek"].mean_.

Starter code

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2, stratify=y)

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2, stratify=y)

pipe = Pipeline([("olcek", StandardScaler()), ("model", KNeighborsClassifier(n_neighbors=3))])
pipe.fit(X_train, y_train)
print(round(pipe.score(X_test, y_test), 3))
print(len(pipe.named_steps["olcek"].mean_))

Expected output

0.978
4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What do the transformer steps do when <code>fit</code> is called on a pipeline?

  • They only transform
  • They are skipped
  • They run after the model
  • They fit_transform and pass the result to the next step Correct answer

Explanation: Each transformer learns and transforms on the training data; the model trains on the transformed data.

2 Set the neighbour count of the model in the pipeline to 9.

Question
pipe.set_params(____=9)
Answer
pipe.set_params(model__n_neighbors=9)

Explanation: The step__parameter notation reaches nested parameters.

3 Match the expressions.

Term Meaning
Pipeline([...]) Named steps
make_pipeline Automatic names
named_steps Access to a step
model__C Nested parameter

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation