'GridSearchCV'对象没有属性'support_vectors_'

时间:2019-10-01 20:21:23

标签: python-3.x scikit-learn svm gridsearchcv

我正在尝试使用GridSearch查找最佳参数,然后还使用最佳参数找出支持向量。

代码如下:

tuned_parameters = [{'kernel': ['linear'], 'C': [0.00001,0.0001,0.001,0.1,1, 10, 100, 1000],
                     'decision_function_shape':["ovo"]}]
clf = GridSearchCV(SVC(), tuned_parameters, cv=5)
clf.fit(X, Y)
print("Best parameters set found on development set:")
print()
print(clf.best_params_)
# Predicting on the unseen test data
predicted_test = clf.predict(X_test)

# Calculating Accuracy on test data
accuracy_test=accuracy_score(Yt, predicted_test)
support_vec=clf.support_vectors_
print(support_vec)

错误:

 AttributeError: 'GridSearchCV' object has no attribute 'support_vectors_'

sklearn 0.21.2

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

那是因为GridSearchCV不是 SVC,而是包含一个SVC对象。这就是为什么它没有support_vectors_属性,并引发错误的原因。

要访问SVC内的GridSearchCV,请使用其best_estimator_属性。因此,而不是

clf.support_vectors_

致电:

clf.best_estimator_.support_vectors_

为了安全起见,在实例化refit=True时包括GridSearchCV