2

我正在尝试使用GridSearchCV实现决策树分类器。实施后我试图访问cv_results_.mean_train_score但我得到关键错误。

    tuned_parameters = [{'max_depth': [1, 5, 10, 25, 50, 75, 100, 150, 250, 500, 750, 1000], 
                         'min_samples_split' : [5, 10, 25, 50, 75, 150, 250, 500]}] 
    cv_timeSeries = TimeSeriesSplit(n_splits=4).split(X_train)
    base_estimator = DecisionTreeClassifier(class_weight='balanced') 
    gsearch_cv = GridSearchCV(estimator=base_estimator, 
                              param_grid=tuned_parameters, 
                              cv=cv_timeSeries, 
                              scoring='roc_auc', 
                              n_jobs=-1)
    gsearch_cv.fit(X_train, y_train)

当我试图访问 gsearch_cv 的所有键时,我无法找到字典键 mean_train_score。

4

3 回答 3

11

在 GridSearchCV 中添加以下参数

GridSearchCV(return_train_score=True)

于 2019-06-16T10:41:18.550 回答
3

您能否发布生成错误的代码?

mean_train_score 是 cv_results_ 的关键,因此要获得他的值,您应该:

gsearch_cv = GridSearchCV(estimator=base_estimator, 
                          param_grid=tuned_parameters, 
                          cv=cv_timeSeries, 
                          scoring='roc_auc', 
                          return_train_score=True,
                          n_jobs=-1)
gsearch_cv.fit(X_train, y_train)
gsearch_cv.cv_results_['mean_train_score']

您可以在 sklearn 页面https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html上找到完整示例

于 2019-06-02T16:04:14.840 回答
-2

尝试以下更改:

gsearch_cv = GridSearchCV(estimator=base_estimator,
                          param_grid=tuned_parameters,
                          cv=cv_timeSeries,
                          scoring='roc_auc',
                          return_train_score=True,
                          n_jobs=-1,
                          return_train_score=True)
于 2020-06-07T22:05:20.637 回答