【发布时间】:2020-02-11 12:43:36
【问题描述】:
我想通过 GridSearchCV 优化 SVM 的超参数。但是最佳估计器的得分与运行具有最佳参数的支持向量机时的得分相差很大。
#### Hyperparameter search with GridSearchCV###
pipeline = Pipeline([
("scaler", StandardScaler()),
("svm", LinearSVC(loss='hinge'))])
param_grid=[{'svm__C': c_range}]
clf = GridSearchCV(pipeline, param_grid=param_grid, cv=5, scoring='accuracy')
clf.fit(X,y)
print('\n Best score: ',clf.best_score_)
#### scale train and test data ###
sc = StandardScaler()
sc.fit(X)
X = scaler.transform(X)
X_test = sc.transform(X_test)
###### test best estimator with test data ###################
print("Best estimator score: ", clf.best_estimator_.score(X_test, y_test))
##### run SVM with the best found parameter #####
svc = LinearSVC(C=clf.best_params_['svm_C'])
svc.fit(X,y)
print("score with best parameter: ", svc.score(X_test,y_test))
结果如下:
最好成绩:0.784
最佳估计分数:0.6991
最佳参数得分:0.7968
我不明白为什么best estimator 和svm 的分数不一样?以下哪个结果是正确的测试精度?为什么 0.6991 的 Best estimator 的得分这么差?我是不是做错了什么?
【问题讨论】:
-
答案很有帮助。非常感谢
-
抱歉。刚做完。由于疏忽,我只按了“这个答案很有用。”
标签: python-3.x scikit-learn svm gridsearchcv standardized