Handson-ml: I don't understand the exercise 5 of chapter 2

Created on 1 Nov 2019  路  2Comments  路  Source: ageron/handson-ml

This is the code:

param_grid = [{
    'preparation__num__imputer__strategy': ['mean', 'median', 'most_frequent'],
    'feature_selection__k': list(range(1, len(feature_importances) + 1))
}]

grid_search_prep = GridSearchCV(prepare_select_and_predict_pipeline, param_grid, cv=5,
                                scoring='neg_mean_squared_error', verbose=2)
grid_search_prep.fit(housing, housing_labels)

prepare_select_and_predict_pipeline is the following pipeline:

preparation_and_feature_selection_pipeline= Pipeline([
    ('preparation', full_pipeline),
    ('feature_selection', TopFeatureSelector(feature_importances, k))
])

What I don't understand is how does GridSearchCV know that preparation__num__imputers__strategy refers to a hyperparameter of the SimpleImputer transformer and feature_selection__k refers to the hyperparameter k of the TopFeatureSelector transformer.

I would appreciate a response, this is something that I am not able to understand despite trying.

Most helpful comment

Hi @arnau6610 ,

Great question! The Pipeline class uses a naming convention whereby double underscores __ are used a bit like / separators in file paths. So you can think of preparation__num__imputer__strategy a bit like a file path preparation/num/imputer/strategy. Specifically, it refers to the strategy hyperparameter of the estimator named imputer within the pipeline named num within the pipeline named preparation. Thanks to this naming convention, you can refer to arbitrarily nested hyperparameters in a pipeline object.

Similarly, feature_selection__k is a bit like the path feature_selection/k: it's the hyperparameter k of the estimator named feature_selection.

Hope this helps.

All 2 comments

Hi @arnau6610 ,

Great question! The Pipeline class uses a naming convention whereby double underscores __ are used a bit like / separators in file paths. So you can think of preparation__num__imputer__strategy a bit like a file path preparation/num/imputer/strategy. Specifically, it refers to the strategy hyperparameter of the estimator named imputer within the pipeline named num within the pipeline named preparation. Thanks to this naming convention, you can refer to arbitrarily nested hyperparameters in a pipeline object.

Similarly, feature_selection__k is a bit like the path feature_selection/k: it's the hyperparameter k of the estimator named feature_selection.

Hope this helps.

This should have been included at least in a footnote somewhere...

Was this page helpful?
0 / 5 - 0 ratings