You could make use of the cv_results_
attribute of the gridsearchCV
object as shown below:
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = svm.SVC(gamma="scale")
clf = GridSearchCV(svc, parameters, cv=5)
clf.fit(iris.data, iris.target)
Now you use clf.cv_results_
{'mean_fit_time': array([0.00049248, 0.00051575, 0.00051174, 0.00044131]),
'mean_score_time': array([0.0002739 , 0.00027657, 0.00023718, 0.00023627]),
'mean_test_score': array([0.98 , 0.96666667, 0.97333333, 0.98 ]),
'param_C': masked_array(data=[1, 1, 10, 10],
mask=[False, False, False, False],
fill_value='?',
dtype=object),
'param_kernel': masked_array(data=['linear', 'rbf', 'linear', 'rbf'],
mask=[False, False, False, False],
fill_value='?',
dtype=object),
'params': [{'C': 1, 'kernel': 'linear'},
{'C': 1, 'kernel': 'rbf'},
{'C': 10, 'kernel': 'linear'},
{'C': 10, 'kernel': 'rbf'}],
'rank_test_score': array([1, 4, 3, 1], dtype=int32),
'split0_test_score': array([0.96666667, 0.96666667, 1. , 0.96666667]),
'split1_test_score': array([1. , 0.96666667, 1. , 1. ]),
'split2_test_score': array([0.96666667, 0.96666667, 0.9 , 0.96666667]),
'split3_test_score': array([0.96666667, 0.93333333, 0.96666667, 0.96666667]),
'split4_test_score': array([1., 1., 1., 1.]),
'std_fit_time': array([1.84329827e-04, 1.34653950e-05, 1.26220210e-04, 1.76294378e-05]),
'std_score_time': array([6.23956317e-05, 1.34498512e-05, 3.57596078e-06, 4.68175419e-06]),
'std_test_score': array([0.01632993, 0.02108185, 0.03887301, 0.01632993])}
You can make use of the params
and the mean_test_score
for constructing the dataframe you are looking using the below command:
pd.concat([pd.DataFrame(clf.cv_results_["params"]),pd.DataFrame(clf.cv_results_["mean_test_score"], columns=["Accuracy"])],axis=1)
And your final dataframe looks like
C kernel Accuracy
0 1 linear 0.980000
1 1 rbf 0.966667
2 10 linear 0.973333
3 10 rbf 0.980000