CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

model.py
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

# buraya yaz

Sample solution

model.py
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))

Expected output

0.958
0.916

Questions and answers

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

1 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 Correct answer
  • It works fine
  • It is skipped automatically

Explanation: The output of intermediate steps is the next step's input; without transform the chain can't be built.

2 Disable the scaling step.

Question
pipe.set_params(olcek=____)
Answer
pipe.set_params(olcek="passthrough")

Explanation: "passthrough" turns the step off so data passes through unchanged.

3 Match the accesses.

Term Meaning
pipe["olcek"] Step by name
pipe[0] First step
pipe[:-1] Sub-chain without the model
"passthrough" Turns a step off

All lessons in Intermediate