'SVC' object has no attribute 'SVC'

Viewed 3244

i`m struggling with a simple loop:

for kernel in ('linear','poly', 'rbf'):
    svm = svm.SVC(kernel=kernel, C=1)
    svm.fit(trainingdata_without_labels, trainingdata_labels)

    predicted_labels = svm.predict(testdata_without_labels)
    print("testing success ratio with "+ kernel + "kernel :" + str(accuracy_score(testdata_labels, predicted_labels)))

It works fine for the first loop, but then i get:

AttributeError: 'SVC' object has no attribute 'SVC'

I really would like to understand my mistake.

Many thanks in advance <3

1 Answers

You are overwriting the svm with the first loop.

Try to change the name of the classifier, for instance:

for kernel in ('linear','poly', 'rbf'):
    classifier_svm = svm.SVC(kernel=kernel, C=1)
    classifier_svm.fit(trainingdata_without_labels, trainingdata_labels)

    predicted_labels = classifier_svm.predict(testdata_without_labels)
    print("testing success ratio with "+ kernel + "kernel :" + str(accuracy_score(testdata_labels, predicted_labels)))

Moreover, I think that what you try to do, finding the optimal kernel, its easier to solve using GridSearchCV:

from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC


tuned_parameters = [{'kernel': ['linear', 'poly', 'rbf'],
                     'C': [1]}
                   ]

clf = GridSearchCV(SVC(), tuned_parameters, scoring='accuracy')
clf.fit(trainingdata_without_labels, trainingdata_labels)


print("Best parameters set found on development set:\n")
print(clf.best_params_)
print("\nGrid scores on development set:\n")
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
    print("%0.3f (+/-%0.03f) for %r"
          % (mean, std * 2, params))

print("\nDetailed classification report:\n")
print("The model is trained on the full development set.")
print("The scores are computed on the full evaluation set.")

y_true, y_pred = testdata_labels, clf.predict(testdata_without_labels)

print(classification_report(y_true, y_pred))

With this code snippet you are going to train the model with the 3 kernels and also make a 5 fold cross validation. And finally compute the classification report (precission, recall, f1-score) of the testing variables. The final report should look like this (each row will be a class to predict in your data):

                precision    recall  f1-score   support

           0       1.00      1.00      1.00        27
           1       0.95      1.00      0.97        35
           2       1.00      1.00      1.00        36
           3       1.00      1.00      1.00        29
           4       1.00      1.00      1.00        30
           5       0.97      0.97      0.97        40
           6       1.00      0.98      0.99        44
           7       1.00      1.00      1.00        39
           8       1.00      0.97      0.99        39
           9       0.98      0.98      0.98        41

    accuracy                           0.99       360
   macro avg       0.99      0.99      0.99       360
weighted avg       0.99      0.99      0.99       360
Related