【问题标题】:'GridSearchCV' object has no attribute 'support_vectors_'“GridSearchCV”对象没有属性“support_vectors_”
【发布时间】:2020-01-31 03:46:14
【问题描述】:

我正在尝试使用 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

如何解决这个问题?

【问题讨论】:

  • 你使用的是什么版本的 sklearn?
  • sklearn 0.21.2版本

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


【解决方案1】:

这是因为GridSearchCV 不是 SVC,而是它包含 SVC 对象。这就是为什么它没有support_vectors_ 属性,并抛出错误。

要访问GridSearchCV 中的SVC,请使用其best_estimator_ 属性。所以而不是

clf.support_vectors_

呼叫:

clf.best_estimator_.support_vectors_

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

【讨论】:

    猜你喜欢
    • 2016-03-15
    • 2020-07-02
    • 2017-05-22
    • 2020-10-27
    • 2020-07-02
    • 2020-09-18
    • 2019-08-27
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多