最佳评分指标不存在于GridSearchCV的best_param_中

时间:2019-11-11 16:56:51

标签: python scikit-learn

我有以下内容:

from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier as RFC

par = {"n_estimators":n_estimators,
"max_depth":max_depth,
"class_weight":weight}


scores = {"AUC":"roc_auc","score":my_score} #Scores metric


rfc=RFC()

grid_rfc=GridSearchCV(rfc,
param_grid=par,
cv=10,
scoring=scores,
iid=False,
refit="AUC")

grid_rfc.fit(x_train,y_train)

然后我可以使用grid_rfc.best_param获得最佳参数,但是未列出提供最佳参数的score

据我了解,scoreRFC试图最大化的那个,所以我不明白为什么它不在最佳参数中。

编辑:

我缺少的不是RF产生的得分,而是使用了哪个得分函数来拟合给出最佳结果的树(例如,score字典中的“ AUC”或“ my_score” )

1 个答案:

答案 0 :(得分:0)

根据GridSearchCV documentation,您可以使用best_score_中的best_param_获得最佳分数。

由于您的代码不完整,我无法测试此示例,但是实现应如下所示:

from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier as RFC

par = {"n_estimators":n_estimators,
"max_depth":max_depth,
"class_weight":weight}


scores = {"AUC":"roc_auc","score":my_score} #Scores metric


rfc=RFC()

grid_rfc=GridSearchCV(rfc,
param_grid=par,
cv=10,
scoring=scores,
iid=False,
refit="AUC")

grid_rfc.fit(x_train,y_train)

# Print the best score
print(grid.best_score_)

现在您可能会注意到它看起来与真实的precision略有不同,因此请参见this Stack Overflow post进行更多讨论。