您可以使用对象的cv_results_
属性,gridsearchCV
如下所示:
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)
现在你使用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])}
您可以使用params
和mean_test_score
来构建您正在查看的数据框,使用以下命令:
pd.concat([pd.DataFrame(clf.cv_results_["params"]),pd.DataFrame(clf.cv_results_["mean_test_score"], columns=["Accuracy"])],axis=1)
你的最终数据框看起来像
C kernel Accuracy
0 1 linear 0.980000
1 1 rbf 0.966667
2 10 linear 0.973333
3 10 rbf 0.980000
希望这可以帮助!