gpt4 book ai didi

pandas - 如何打印 Sklearn 中 GridSearch 中使用的召回率和准确率以及参数?

转载 作者:行者123 更新时间:2023-11-30 08:37:51 24 4
gpt4 key购买 nike

我想打印准确性,回顾网格中使用的每个参数,如何做到这一点。

我的 Gridsearch 代码

from sklearn.grid_search import GridSearchCV
rf1=RandomForestClassifier(n_jobs=-1, max_features='sqrt')
#fit_rf1=rf.fit(X_train_res,y_train_res)

# Use a grid over parameters of interest
param_grid = {
"n_estimators" : [50, 100, 150, 200],
"max_depth" : [2, 5, 10],
"min_samples_leaf" : [10,20,30]}




from sklearn.metrics import make_scorer
from sklearn.metrics import precision_score,recall_score
scoring = {'precision': make_scorer(precision_score), 'Recall': make_scorer(recall_score)}
CV_rfc = GridSearchCV(estimator=rf1, param_grid=param_grid, cv= 10,scoring=scoring)
CV_rfc.fit(X_train_res, y_train_res)

我的预期输出

{'max_depth': 10, 'min_samples_leaf': 2, 'n_estimators': 50,'accuracy':.97,'recall':.89}
{'max_depth': 5, 'min_samples_leaf':10 , 'n_estimators': 100,'accuracy':.98,'recall':.92}

最佳答案

如果您将scoring设置为评分者列表,则可以在CV_rfc.cv_results_中获取每个评分者的平均分数。

例如:

from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
X, y = make_classification()
base_clf = RandomForestClassifier()
param_grid = {
"n_estimators" : [50, 100, 150, 200],}
CV_rf = GridSearchCV(base_clf, param_grid, scoring=['accuracy', 'roc_auc'], refit=False)
CV_rf.fit(X, y)

print(CV_rf.cv_results_)

你会得到如下输出:

{'mean_fit_time': array([ 0.05867839,  0.10268728,  0.15536443,  0.19937317]),
'mean_score_time': array([ 0.00600123, 0.01033529, 0.0146695 , 0.02000403]),
'mean_test_accuracy': array([ 0.9 , 0.91, 0.89, 0.91]),
'mean_test_roc_auc': array([ 0.91889706, 0.94610294, 0.94253676, 0.94308824]),
'mean_train_accuracy': array([ 1., 1., 1., 1.]),
'mean_train_roc_auc': array([ 1., 1., 1., 1.]),
[...]
}

所以 mean_test_[scoring] 就是您所追求的。请注意,您可以将 cv_results_ 作为 Pandas DataFrame 导入。这对可读性有很大帮助!

关于pandas - 如何打印 Sklearn 中 GridSearch 中使用的召回率和准确率以及参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47882126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com