Module 4 Review
You have learned to combine preprocessing and a model into one object. This page sums up the Self-study of this module and checks the main ideas.
Summary
- Pipelines & ColumnTransformer:
- A
Pipelinechains transformers and a model: onefit, onepredict, and you save one object. - In cross-validation, a pipeline fits every step in each fold on the training part only, so no information from the validation folds leaks into the preprocessing.
- A
ColumnTransformergives different columns different preprocessing;make_column_selector,OneHotEncoder(handle_unknown="ignore")andget_feature_names_outhelp with real data. - Parameters of a step are set as
step__parameter, which lets you tune the preprocessing and the model together.
- A
Quiz
These questions are easy if you have worked through the activities. Choose an answer to see whether it is right and why.
Why does a pipeline make cross-validation correct?
- It makes the training faster, so you can use more folds in the same time. Speed is not the point. The point is what each step learns from.
- It refits the preprocessing in each fold, on the training part. Right. The validation folds then play no part in the preprocessing.
- It chooses the best model for you by trying them all. A pipeline does not choose a model; you still compare models yourself.
A pipeline has an SVR step called model. How do you set its C to 10?
pipeline.set_params(model__C=10)Right: the step name, two underscores, then the parameter name.pipeline.set_params(C=10)The pipeline would not know which stepCbelongs to.pipeline.steps[1].C = 10, the second step of the pipelinesteps[1]is the pair (name, step), not the SVR itself. Use the step name and two underscores.
Your data has numeric columns and one column with text categories. Which tool gives each type of column its own preprocessing?
- A
StandardScaler, which scales every column. A scaler cannot handle text, and it treats all columns the same way. - A second model for the text column. You need one model; the preprocessing differs per column.
- A
ColumnTransformer. Right, for example with a scaler for the numeric columns and aOneHotEncoderfor the categories.