【问题标题】:GridSearchCV has no attribute grid.grid_scores_GridSearchCV 没有属性 grid.grid_scores_
【发布时间】:2019-08-27 14:47:15
【问题描述】:

试过 grid.cv_results_ 没有正确的问题

from sklearn.model_selection
import GridSearchCV
params = {
    'decisiontreeclassifier__max_depth': [1, 2],
    'pipeline-1__clf__C': [0.001, 0.1, 100.0]
}
grid = GridSearchCV(estimator = mv_clf,
    param_grid = params,
    cv = 10,
    scoring = 'roc_auc')
grid.fit(X_train, y_train)
for params, mean_score, scores in grid.grid_scores_:
    print("%0.3f+/-%0.2f %r" %
        (mean_score, scores.std() / 2, params))
#AttributeError: 'GridSearchCV' object has no attribute 'grid_scores_'

尝试将grid.grid_scores_ 替换为grid.cv_results_ 目的是打印不同的超参数值组合和通过 10 倍交叉验证计算的平均 ROC AUC 分数

from sklearn.model_selection
    import GridSearchCV
    params = {
        'decisiontreeclassifier__max_depth': [1, 2],
        'pipeline-1__clf__C': [0.001, 0.1, 100.0]
    }
    grid = GridSearchCV(estimator = mv_clf,
        param_grid = params,
        cv = 10,
        scoring = 'roc_auc')
    grid.fit(X_train, y_train)
    for params, mean_score, scores in grid.grid_scores_:
        print("%0.3f+/-%0.2f %r" %
            (mean_score, scores.std() / 2, params))
    #AttributeError: 'GridSearchCV' object has no attribute 'grid_scores_'

【问题讨论】:

  • grid.cv_results_ 适用于最新的 scikit-learn v0.20.1(其中确实不存在 grid_scores_ 属性) - 检查 documentation

标签: python scikit-learn roc gridsearchcv


【解决方案1】:

在最新的 scitkit-learn libaray 中,grid_scores_ 已被贬值,并已被 cv_results_

cv_results_给出网格搜索运行的详细结果。

grid.cv_results_.keys()

Output: dict_keys(['mean_fit_time', 'std_fit_time', 'mean_score_time', 'std_score_time', 'param_n_estimators', 'params', 'split0_test_score', 
'split1_test_score', 'split2_test_score', 'split3_test_score', 'split4_test_score',
'mean_test_score', 'std_test_score', 'rank_test_score'])

cv_results_ 提供与 grid_score 相比的详细输出。结果输出以字典的形式。我们可以通过遍历字典的键从字典中提取相关指标。下面是为 cv=5 运行网格搜索的示例

 for i in ['mean_test_score', 'std_test_score', 'param_n_estimators']:
        print(i," : ",grid.cv_results_[i])

 Output:   mean_test_score  :  [0.833 0.83 0.83 0.837 0.838 0.8381 0.83]
           std_test_score  :  [0.011 0.009 0.010 0.0106 0.010 0.0102 0.0099]
           param_n_estimators  :  [20 30 40 50 60 70 80]

【讨论】:

  • 上面给出的答案和这个链接上的答案stackoverflow.com/a/59496696/11134789真的很有帮助
  • 是否可以让它包含每个时期的验证分数?我找不到路。我正在尝试绘制验证与训练历史。
猜你喜欢
  • 2020-09-18
  • 2020-01-31
  • 2016-03-15
  • 2020-07-02
  • 2017-05-22
  • 2020-10-27
  • 2020-07-02
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多