如何找出精确度、召回率、特异性和精确度Python中网格搜索中选择的最佳模型的F1分数?
这是我的模型的网格选择步骤。我能够找出所选模型的最佳准确度分数和最佳参数,但我想找出精确度、召回率、特异性和精确度。网格搜索中选择的最佳模型的F1分数
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [100, 200],
'max_features': ['auto', 'sqrt', None],
'max_depth': [10, 20, 30, None],
'criterion': ['gini', 'entropy'],
'min_samples_split': [5, 10,15], 'min_samples_leaf': [1,4,6],
'bootstrap': [True, False]}]
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 5,
n_jobs = -1) #n_jobs to optimise grid search process
grid_search.fit(X_train, Y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("\n")
print("Results for Grid Search")
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
print("Best Parameters:", best_parameters)
This is the Grid Selection step for my model. I am able to find out the best accuracy score and best parameters of that selected model but I want to find out precision, recall, specificity & F1 score of the best model which is selected in Grid Search
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [100, 200],
'max_features': ['auto', 'sqrt', None],
'max_depth': [10, 20, 30, None],
'criterion': ['gini', 'entropy'],
'min_samples_split': [5, 10,15], 'min_samples_leaf': [1,4,6],
'bootstrap': [True, False]}]
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 5,
n_jobs = -1) #n_jobs to optimise grid search process
grid_search.fit(X_train, Y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("\n")
print("Results for Grid Search")
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
print("Best Parameters:", best_parameters)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GridSearchCV 允许传递评分函数列表,前提是您指定一个评分函数,将选择最佳参数来重新拟合模型。
然后,您可以在
cv_results_
属性中访问整个交叉验证结果。首先将cv_results_
打包为 pandas DataFrame,然后访问与best_index_
属性对应的行会更容易:您将获得一个按分数名称索引的系列,因此您可以访问,例如
"mean_test_recall"
、"mean_test_f1"
等。请注意,对于您的问题,特异性不是 内置评分名称,但您也可以提供
GridSearchCV
和 自定义指标,所以可以传递一个可调用的计算它(例如使用 混淆矩阵)GridSearchCV
enables passing a list of scoring functions, provided that you specify a single scoring function to which the best parameters will be chosen to refit the model.Then you can access the entire cross validation results in the
cv_results_
attribute. It can be easier to first pack thecv_results_
as a pandas DataFrame and then access the row corresponding tobest_index_
attribute:The you get a Series indexed by score names, so you can access, for example,
"mean_test_recall"
,"mean_test_f1"
, etc.Note that, for your question, specificity is not a built-in scoring name, but you can also supply
GridSearchCV
with custom metrics, so it's possible to pass a callable calculating it (say by using confusion matrix)