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

发布于 2025-01-12 09:13:57 字数 1008 浏览 5 评论 0原文

这是我的模型的网格选择步骤。我能够找出所选模型的最佳准确度分数和最佳参数,但我想找出精确度、召回率、特异性和精确度。网格搜索中选择的最佳模型的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2025-01-19 09:13:57

GridSearchCV 允许传递评分函数列表,前提是您指定一个评分函数,将选择最佳参数来重新拟合模型。

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

然后,您可以在 cv_results_ 属性中访问整个交叉验证结果。首先将 cv_results_ 打包为 pandas DataFrame,然后访问与 best_index_ 属性对应的行会更容易:

cv_results = pd.DataFrame(grid_search.cv_results_)
best_model_results = cv.results.loc[grid_search.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.

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. 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 = 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.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文