GridSearchCV for estimating optimal tuning parameter

I am facing key error : 0 as a problem in these codes of lines

grid.cv_results_[0].parameters

print(grid.cv_results_[0].cv_validation_scores)

print(grid.cv_results_[0].mean_validation_scores)

>Solution :

GridSearchCV's cv_results_ returns a dictionary. Therefore you can’t access it via index. For example:

svc = svm.SVC()
grid = GridSearchCV(svc, parameters)
grid.fit(data, target)
print(grid.cv_results_.keys())

Output will be:

['mean_fit_time', 'mean_score_time', 'mean_test_score',...
 'param_C', 'param_kernel', 'params',...
 'rank_test_score', 'split0_test_score',...
 'split2_test_score', ...
 'std_fit_time', 'std_score_time', 'std_test_score']

You can access cv_results_ items like:

grid.cv_results_['params'] ##instead of grid.cv_results_[0].parameters
grid.cv_results_['std_test_score'] ##for test scores
grid.cv_results_['mean_test_score'] ##for mean test scores

Leave a Reply