outofworld
outofworld

Reputation: 384

Getting keyerror from sklearn.model_selection.GridSearchCV

I am trying to implement decision tree classifier using GridSearchCV. After implementation i tried to to access cv_results_.mean_train_score but i am getting key error.

    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)

When i am trying to access all the keys of gsearch_cv, i am unable to find dict key mean_train_score.

Upvotes: 2

Views: 8197

Answers (3)

Vikas Singh
Vikas Singh

Reputation: 11

Try the following change:

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)

Upvotes: -1

Matteo Peluso
Matteo Peluso

Reputation: 452

Could you please post the code that generates the error?

mean_train_score is a key of cv_results_ so to get his values you shall:

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']

You can find a complete example on the sklearn page https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

Upvotes: 3

Akshay Indalkar
Akshay Indalkar

Reputation: 359

add the parameter following parameter in GridSearchCV

GridSearchCV(return_train_score=True)

Upvotes: 11

Related Questions