Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sklearn.model_selection GridSearchCV is throwing KeyError: 'mean_train_score'

I am trying to fetch mean train score from classifierobject.cv_result_ from GridSearchCV method using 5 fold cross-validation but it's giving me *** KeyError: 'mean_train_score' in Google Colab. However, the same code is running fine in local machine Ipython notebook for sklearn version 0.19.1. Can anyone help me how can I get the output in google colab?

clf.cv_results_.keys() ouputs following -

for my local notebook -

dict_keys(['mean_fit_time', 'std_fit_time', 'mean_score_time', 'std_score_time', 'param_n_neighbors', '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', 'split0_train_score', 'split1_train_score', 'split2_train_score', 'split3_train_score', 'split4_train_score', 'mean_train_score', 'std_train_score'])

on google colab notebook -

dict_keys(['mean_fit_time', 'std_fit_time', 'mean_score_time', 'std_score_time', 'param_n_neighbors', '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'])

where is the mean_train_score in google colab. Am I missing something?

from sklearn.model_selection import GridSearchCV

neigh = KNeighborsClassifier(n_jobs=-1)
parameters = {'n_neighbors':[1, 5, 10, 15, 19 , 21, 31, 41, 51]}
clf = GridSearchCV(neigh, parameters, cv=5,\ 
                   scoring='roc_auc',n_jobs=-1) 
clf.fit(x_train, y_train)
train_auc= clf.cv_results_['mean_train_score']
train_auc_std= clf.cv_results_['std_train_score']
cv_auc = clf.cv_results_['mean_test_score'] 
cv_auc_std= clf.cv_results_['std_test_score']

like image 326
Akash Singh Avatar asked Dec 03 '22 18:12

Akash Singh


1 Answers

Try setting return_train_score=True inside GridSearchCV() to calculate the train scores (are off by default, see docs). Maybe you have kind of global variable in sklearn somewhere local set.

like image 68
ilja Avatar answered Jan 08 '23 14:01

ilja