为了获得交叉验证的最佳F1分数,我这样做
grid_search = GridSearchCV(pipeline, param_grid=param_grid, cv=10, verbose=10, scoring='f1')grid_result = grid_search.fit(X_train, y_train)print("best parameters", grid_search.best_params_)print('Best score : {}'.format(grid_search.best_score_))
但是对于测试分数,我也需要F1分数而不是准确率
print("Test Score",grid_search.best_estimator_.score(X_test,y_test.reshape(y_test.shape[0])))
有没有类似f1_score()
的函数可以使用,还是我应该自己编写这个函数?
回答:
你可以使用以下方法计算F1分数:
-
classification report
(示例在此) -
Scikit-learn的f1_score函数:(http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html)