简体   繁体   English

如何在python中使用libSVM计算精度,召回率和F-score

[英]How to calculate precision, recall and F-score with libSVM in python

I want to calculate the precision , recall and f-score using libsvm in Python but I do not know how. 我想用Python中的libsvm来计算precisionrecallf-score ,但我不知道如何。 I have found this site but I have not understand how to call the function, if you can help me through example. 我找到了这个网站,但我不知道如何调用该函数,如果你可以帮助我通过例子。

You can take advantage of scikit-learn , which is one of the best packages for machine learning in Python. 您可以利用scikit-learn ,这是Python中机器学习的最佳软件包之一。 Its SVM implementation uses libsvm and you can work out precision, recall and f-score as shown in the following snippet: 它的SVM实现使用libsvm ,你可以计算精度,召回和f-score,如下面的代码片段所示:

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]]

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. 当然,您可以使用您提到的libsvm tools ,但是它们只能用于二进制分类,而scikit允许您使用多类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python计算Precision、Recall和F-score? - How to calculate Precision, Recall and F-score using python? 具有F分数,精度或召回力的LIBSVM评估 - LIBSVM Evaluation with F-Score, Precision or Recall 如何计算此模型的召回率、准确率和 f 分数? - how can I calculate recall, precision and f-score for this model? 在一次传递中计算精度,召回和F分数 - 蟒蛇 - Calculating Precision, Recall and F-score in one pass - python 使用 KNN 分类器具有召回率、F 分数、精度 1 - Having recall, F-score, precision 1 with KNN classifier 精度,召回率,F分数要求相等的输入 - Precision, Recall, F-score requiring equal inputs 确定 H2O 随机森林模型的准确度、精确度、召回率和 F-Score - Determining the accuracy, precision, recall and F-Score of an H2O Random Forest Model 使用交叉验证计算 4 个模型的召回率和精度以及 F 分数时出错? - Error when computing recall and precision and F-score of 4 models using cross validation? 基于示例的 f-score 小于 Sklearn 中的精度和召回率 - example-based f-score is smaller than precision and recall in Sklearn 如何在pyspark中使用MultiClassMetrics计算f-score? - How to calculate the f-score using MultiClassMetrics in pyspark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM