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

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