从sklearn.model_selection.GridSearchCV获取密钥错误

时间:2019-06-02 15:47:38

标签: python machine-learning decision-tree gridsearchcv

我正在尝试使用 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。

3 个答案:

答案 0 :(得分:0)

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

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

中找到完整的示例

答案 1 :(得分:0)

在GridSearchCV中的参数后面添加参数

GridSearchCV(return_train_score = True)

答案 2 :(得分:-1)

尝试以下更改:

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)