如何在Python中使用libSVM计算精度,召回率和F分数 [英] How to calculate precision, recall and F-score with libSVM in python
问题描述
我想在Python中使用 libsvm 计算precision
,recall
和f-score
,但是我不知道如何.我找到了此站点,但我不明白如果可以通过示例帮助我,该如何调用该函数.
I want to calculate the precision
, recall
and f-score
using libsvm in Python but I do not know how. I have found this site but I have not understand how to call the function, if you can help me through example.
推荐答案
您可以利用 scikit-learn
最好的Python机器学习软件包.它的SVM实现使用libsvm
,您可以计算出精度,召回率和f分数,如以下代码片段所示:
You can take advantage of scikit-learn
, which is one of the best packages for machine learning in Python. Its SVM implementation uses libsvm
and you can work out precision, recall and f-score as shown in the following snippet:
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)
这将产生类似于以下的输出:
Which will produce an output similar to this:
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
允许您使用多类.
Of course, you can use the libsvm tools
you have mentioned, however they are designed to work only with binary classification whereas scikit
allows you to work with multiclass.
这篇关于如何在Python中使用libSVM计算精度,召回率和F分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!