Machine Learning
Steps
Pipeline steps (scaler, model) run in order.
Steps and names
The last step of a pipeline can be any estimator; all steps before it must be transformers (fit + transform). Steps are accessed in three ways:
pipe.named_steps["olcek"] # by name
pipe["olcek"] # short form
pipe[0] # by position
pipe[:-1] # sub-pipeline without the last step
pipe[:-1].transform(X) is a practical way to inspect the transformed data the model sees.
Disabling and replacing a step
A step can be disabled with "passthrough" or replaced by another object with set_params. That is how "should there be scaling?" is asked in a grid search:
pipe.set_params(olcek="passthrough") # turn scaling off
pipe.set_params(model=SVC(C=10)) # replace the model
izgara = {"olcek": [StandardScaler(), "passthrough"]}
Steps also behave like hyperparameters.
Exercise Test the scaling effect
Build Pipeline([("olcek", StandardScaler()), ("model", SVC())]). Train it first as is, then with set_params(olcek="passthrough"), and print the test accuracy with round(..., 3) on separate lines.
Show example solution
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
pipe = Pipeline([("olcek", StandardScaler()), ("model", SVC())])
print(round(pipe.fit(X_train, y_train).score(X_test, y_test), 3))
pipe.set_params(olcek="passthrough")
print(round(pipe.fit(X_train, y_train).score(X_test, y_test), 3))
Test yourself
-
What happens if <code>LogisticRegression()</code> is put in the middle of a pipeline?
- It is moved to the end
- An error: intermediate steps must offer transform
- It works fine
- It is skipped automatically
Show answer
An error: intermediate steps must offer transform — The output of intermediate steps is the next step's input; without transform the chain can't be built.
-
Disable the scaling step.
pipe.set_params(olcek=____)
Show answer
pipe.set_params(olcek="passthrough")
-
Match the accesses.
- pipe["olcek"]
- Step by name
- pipe[0]
- First step
- pipe[:-1]
- Sub-chain without the model
- "passthrough"
- Turns a step off
Next lesson: fit and predict