首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'SVC‘对象没有属性'SVC’

'SVC‘对象没有属性'SVC’
EN

Stack Overflow用户
提问于 2020-06-12 14:09:03
回答 1查看 3.8K关注 0票数 2

我在为一个简单的循环而挣扎:

代码语言:javascript
复制
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)))

它适用于第一个循环,但之后我得到:

AttributeError:'SVC‘对象没有属性SVC

我真的很想明白我的错误。

事先非常感谢<3

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 14:11:12

您正在用第一个循环覆盖svm。

例如,尝试更改分类器的名称:

代码语言:javascript
复制
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)))

此外,我认为您所要做的,找到最优内核,使用GridSearchCV更容易解决:

代码语言:javascript
复制
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))

使用这个代码片段,您将使用3个内核来训练模型,并进行5倍的交叉验证。最后计算出测试变量的分类报告(分数、回忆、F1分数)。最后的报告应该如下所示(每一行都是要在数据中预测的类):

代码语言:javascript
复制
                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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62346013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档