I am learning about multiclass classification using scikit learn. My goal is to develop a code which tries to include all the possible metrics needed to evaluate the classification. This is my code:
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer, precision_score, recall_score, f1_score
param_grid = [
{'estimator__randomforestclassifier__n_estimators': [3, 10], 'estimator__randomforestclassifier__max_features': [2]},
# {'estimator__randomforestclassifier__bootstrap': [False], 'estimator__randomforestclassifier__n_estimators': [3, 10], 'estimator__randomforestclassifier__max_features': [2, 3, 4]}
]
rf_classifier = OneVsRestClassifier(
make_pipeline(RandomForestClassifier(random_state=42))
)
scoring = {'accuracy': make_scorer(accuracy_score),
'precision_macro': make_scorer(precision_score, average = 'macro'),
'recall_macro': make_scorer(recall_score, average = 'macro'),
'f1_macro': make_scorer(f1_score, average = 'macro'),
'precision_micro': make_scorer(precision_score, average = 'micro'),
'recall_micro': make_scorer(recall_score, average = 'micro'),
'f1_micro': make_scorer(f1_score, average = 'micro'),
'f1_weighted': make_scorer(f1_score, average = 'weighted')}
grid_search = GridSearchCV(rf_classifier, param_grid=param_grid, cv=2,
scoring=scoring, refit=False)
grid_search.fit(X_train_prepared, y_train)
However when I try to find out the best estimator, I get the following error message:
print(grid_search.best_params_)
print(grid_search.best_estimator_)
AttributeError: 'GridSearchCV' object has no attribute 'best_params_'
Question: How is it possible that even after fitting the model I do not get the best estimator?
I noticed that if I set refit="some_of_the_metrics"
, I get an estimator but I do not understand why I should use it since it would fit the method to optimize a metric instead of all of them.
Therefore, how can I get the best estimator for all the scores? And what is the the point of refit?
Note: I tried to read the documentation but it still does not make sense to me.