5
A
回答
10
您可以利用scikit-learn
,這是Python中用於機器學習的最佳軟件包之一。它的SVM實現使用libsvm
,你可以計算出準確率,召回和F-得分如下面的代碼片段:
from sklearn import svm
from sklearn import metrics
from sklearn.cross_validation import train_test_split
from sklearn.datasets import load_iris
# prepare dataset
iris = load_iris()
X = iris.data[:, :2]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# svm classification
clf = svm.SVC(kernel='rbf', gamma=0.7, C = 1.0).fit(X_train, y_train)
y_predicted = clf.predict(X_test)
# performance
print "Classification report for %s" % clf
print
print metrics.classification_report(y_test, y_predicted)
print
print "Confusion matrix"
print metrics.confusion_matrix(y_test, y_predicted)
這將產生類似這樣的輸出:
Classification report for SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.7,
kernel=rbf, max_iter=-1, probability=False, shrinking=True, tol=0.001,
verbose=False)
precision recall f1-score support
0 1.00 1.00 1.00 9
1 0.90 0.69 0.78 13
2 0.64 0.88 0.74 8
avg/total 0.86 0.83 0.84 30
Confusion matrix
[[9 0 0]
[0 9 4]
[0 1 7]]
當然,你可以使用你提到的libsvm tools
,但是它們只能用於二進制分類,而scikit
則允許你使用多類。
相關問題
- 1. 如何在python
- 2. 如何在Python
- 3. 如何在python
- 4. 如何在python
- 5. 如何在python
- 6. 如何在python
- 7. 如何在python
- 8. 如何在Python
- 9. 如何在Python
- 10. 如何在Python
- 11. 如何在python
- 12. 如何在Python
- 13. 如何在python
- 14. 如何在Python
- 15. 如何在python
- 16. 如何在python
- 17. 如何在Python
- 18. 如何在python
- 19. 如何在Python
- 20. 如何在python
- 21. 如何在Python
- 22. 如何在python
- 23. 如何在python
- 24. 如何在python
- 25. 如何在python
- 26. 如何在python
- 27. 如何在python
- 28. 如何在python
- 29. 如何在python
- 30. 如何在Python
你正在加載什麼數據集?例如,如果我的數據集在文本文件中如何使用它們? – 2013-07-11 12:44:53
在該示例中,我使用了一個名爲「scikit-learn」的預定義數據集,稱爲「虹膜」。對於特定的數據集,您需要將您的文本數據轉換爲numpy矩陣。 – jabaldonedo