2013-06-04 15 views
5

計算精度,召回率和F-分數與LIBSVM我想precisionrecallf-score使用LIBSVM在Python,但我不知道如何計算。我發現this site,但我不明白如何調用該函數,如果你能幫助我通過例子。如何在python

回答

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則允許你使用多類。

+0

你正在加載什麼數據集?例如,如果我的數據集在文本文件中如何使用它們? – 2013-07-11 12:44:53

+0

在該示例中,我使用了一個名爲「scikit-learn」的預定義數據集,稱爲「虹膜」。對於特定的數據集,您需要將您的文本數據轉換爲numpy矩陣。 – jabaldonedo