1

I am trying to find best parameters using GridSearch and then also find out support vectors using the best parameters.

Here is the code:

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)

Error:

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

sklearn 0.21.2

How to fix this issue?

Shubham Bajaj
  • 309
  • 1
  • 3
  • 12

1 Answers1

2

That's because GridSearchCV isn't SVC, rather it contains a SVC object. This is why it doesn't have support_vectors_ attribute, and throws error.

To access the SVC inside GridSearchCV, use its best_estimator_ attribute. So instead of

clf.support_vectors_

Call:

clf.best_estimator_.support_vectors_

Just to be on the safe side, include refit=True while instantiating GridSearchCV.

Shihab Shahriar Khan
  • 4,930
  • 1
  • 18
  • 26