python-3.x - 'SVC' 对象没有属性 'SVC'

标签 python-3.x scikit-learn

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

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' object has no attribute 'SVC'

我真的很想了解我的错误。

非常感谢<3

最佳答案

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

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

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 更容易解决:

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-score)。最终报告应如下所示(每一行都是一个要在您的数据中预测的类):

                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

关于python-3.x - 'SVC' 对象没有属性 'SVC',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62346013/

相关文章:

python - 跟踪 sklearn 预处理中的输出列

python - class_weights 如何应用于 sklearn 逻辑回归?

python - 仅给定闭包函数,如何访问闭包变量?

python - 为什么node.js读取文件比python快?

python-3.x - 在 Google Sheet API V4 中使用 Python 将特定单元格格式化为日期

python - `statsmodels` 和 `sklearn` 中的 Logit 估计器

python - Pandas OneHotEncoder.fit(dataframe) 返回 ValueError : invalid literal for long() with base 10

python - Docstring 的前导空格不一致

python - 无法通过 ldap3 模块修改 'cn' 属性,Python 3.x

python - 如何打开/转换 .pkz 文件?