Machine Learning
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.
Exercise 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_.
Show example solution
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_))
Test yourself
-
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
Show answer
They fit_transform and pass the result to the next step — Each transformer learns and transforms on the training data; the model trains on the transformed data.
-
Set the neighbour count of the model in the pipeline to 9.
pipe.set_params(____=9)
Show answer
pipe.set_params(model__n_neighbors=9)
-
Match the expressions.
- Pipeline([...])
- Named steps
- make_pipeline
- Automatic names
- named_steps
- Access to a step
- model__C
- Nested parameter
Next lesson: Summary - Scaling