Can I get F1 score each time from GridSearchCV?

Question:

I want to show F1 score from gridsearch each loop of change parameter. I use f1_micro in the GridSearchCV like this.

params = {

    'max_depth':  [None, 2, 4, 6, 8, 10],
    'max_features': [None, 'sqrt', 'log2', 0.2, 0.4, 0.6, 0.8],
}

clf = GridSearchCV(
    estimator=DecisionTreeClassifier(),
    param_grid=params,
    scoring='f1_micro'
)

clf.fit(X, y)

df = pd.DataFrame(clf.cv_results_)
df.to_csv('result.csv')

It show many columns like this.

mean_fit_time   std_fit_time    mean_score_time std_score_time  param_max_depth param_max_features  params  split0_test_score   split1_test_score   split2_test_score   split3_test_score   split4_test_score   mean_test_score std_test_score  rank_test_score

I see the result in csv file it have no column F1 score. I don’t understand how to use F1 score in GridSearchCV.

Asked By: user58519

||

Answers:

As @bleuatb mentions, the _score columns are the micro-f1 scores.

Since you are using the default 5-fold cv setting, for each set of parameters there are 5 train f1 scores and 5 test f1 scores (split0 to split4).
You are getting the test scores here. To get the train scores as well, you can set return_train_score=True in GridSearchCV.
You also get the mean and standard deviation (std) over the 5-folds, as well as the ranking (rank) based on the mean test score.

Answered By: Mattravel