Paul's Python CoursePPC

Pipelines: steps that travel together

Not met yet

make_pipeline(StandardScaler(), KNeighborsClassifier()) chains steps into one model. Its fit fits each step in turn: the scaler measures each column's mean and spread, then the k-NN learns from the scaled readings. Its predict scales new readings with those same means and spreads, then asks the k-NN. Because the pipeline is one model, cross_val_score and GridSearchCV refit all of it in every fold, so the fold being scored never helps measure anything. Each step is named after its class, in lower case: standardscaler, kneighborsclassifier.

Try it

main.py
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

# cloud (oktas) and visibility (metres): very different rulers
X = np.array([[1, 30000], [2, 25000], [7, 26000], [8, 4000]])
usable = np.array([1, 1, 0, 0])

model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=1))
model.fit(X, usable)
print(model.predict([[7, 29000]]))
print(list(model.named_steps))

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks scaling all the data and then cross-validating is the same as scaling inside each fold.
  • Thinks a pipeline measures new readings' own means and spreads when it predicts, rather than reusing the ones from fit.
  • Thinks a pipeline is only a list of steps to run by hand, rather than one model with its own fit and predict.

Where it comes up