简体   繁体   English

如何找出在 Python 中网格搜索中选择的最佳 model 的精度、召回率、特异性和 F1 分数?

[英]How to find out precision, recall, specificity & F1 score of the best model which is selected in Grid Search in Python?

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这是我的 model 的网格选择步骤。我能够找出所选 model 的最佳准确度分数和最佳参数,但我想找出在网格中选择的最佳 model 的精度、召回率、特异性和 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)

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. GridSearchCV允许传递评分函数列表,前提是您指定单个评分 function,将选择最佳参数以重新拟合 model。

grid_search = GridSearchCV(
    estimator=classifier,
    param_grid=parameters,
    scoring=['accuracy', 'f1', 'precision', 'recall'],
    refit="accuracy",  # Or any other value from `scoring` list
)

Then you can access the entire cross validation results in the cv_results_ attribute.然后您可以在cv_results_属性中访问整个交叉验证结果。 It can be easier to first pack the cv_results_ as a pandas DataFrame and then access the row corresponding to best_index_ attribute:首先将cv_results_为 pandas DataFrame 然后访问与best_index_属性对应的行会更容易:

cv_results = pd.DataFrame(grid_search.cv_results_)
best_model_results = cv.results.loc[grid_search.best_index_]

The you get a Series indexed by score names, so you can access, for example, "mean_test_recall" , "mean_test_f1" , etc.你会得到一个由分数名称索引的系列,这样你就可以访问,例如, "mean_test_recall""mean_test_f1"等。

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 )请注意,对于您的问题,特异性不是内置的评分名称,但您也可以为GridSearchCV提供自定义指标,因此可以通过可调用计算它(例如使用混淆矩阵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到我的 word2vec model 的准确度、精确度、召回率和 f1 分数? - How to find accuracy, precision, recall, f1 score for my word2vec model? 如何计算 python 中平衡逻辑回归 model 的精度、召回率和 f1 分数 - How to compute precision,recall and f1 score of an balanced logistic regression model in python 如何在迁移学习 vgg16 模型中获得准确率、召回率、f1 分数? - How to get precision, recall, f1 score in transfer learning vgg16 model? 如何在训练 SSD 后预测 Precision、Recall 和 F1 分数 - How to predict Precision, Recall and F1 score after training SSD Tensorflow:计算精度、召回率、F1 分数 - Tensorflow: Compute Precision, Recall, F1 Score 精度,召回率,F1得分与sklearn相等 - Precision, recall, F1 score equal with sklearn 如何在 python 中找到已经训练和保存的 model 的 f1 分数 - How to find f1 score of a already trained & saved model in python 自定义 Keras 指标函数(召回率、精度、F1 分数)无法加载 H5 model - Custom Keras metric functions (Recall, Precision, F1 Score) isn't enabling the Loading of H5 model Tensorflow Precision / Recall / F1 分数和混淆矩阵 - Tensorflow Precision / Recall / F1 score and Confusion matrix 在Keras获得每班的精确度,召回率和F1分数 - Getting precision, recall and F1 score per class in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM