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.
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...
Most helpful comment
Hi @arnau6610 ,
Great question! The
Pipelineclass uses a naming convention whereby double underscores__are used a bit like/separators in file paths. So you can think ofpreparation__num__imputer__strategya bit like a file pathpreparation/num/imputer/strategy. Specifically, it refers to thestrategyhyperparameter of the estimator namedimputerwithin the pipeline namednumwithin the pipeline namedpreparation. Thanks to this naming convention, you can refer to arbitrarily nested hyperparameters in a pipeline object.Similarly,
feature_selection__kis a bit like the pathfeature_selection/k: it's the hyperparameterkof the estimator namedfeature_selection.Hope this helps.