Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have found this site but I have not understand how to call the function, if you can help me through example.

like image 578
user2284345 Avatar asked Jun 04 '13 21:06

user2284345


People also ask

How do you calculate F1 score with precision and recall?

For example, a Precision of 0.01 and Recall of 1.0 would give : F1-score score (formula above) of 2* (0.01*1.0)/ (0.01+1.0)=~0.02. This is because the F1-score is much more sensitive to one of the two inputs having a low value (0.01 here).

What is F1-score in Python?

F1-score is a better metric when there are imbalanced classes. It is needed when you want to seek a balance between Precision and Recall. In most real-life classification problems, imbalanced class distribution exists and thus F1-score is a better metric to evaluate our model. Calculating Precision and Recall in Python

What is the difference between recall score and precision score?

Precision score is used to measure the model performance on measuring the count of true positives in correct manner out of all positive predictions made. Recall score is used to measure the model performance in terms of measuring the count of true positives in correct manner out of all the actual positive values.

How to calculate precision score in scikit-learn?

The precision score can be calculated using the precision_score () scikit-learn function. For example, we can use this function to calculate precision for the scenarios in the previous section. First, the case where there are 100 positive to 10,000 negative examples, and a model predicts 90 true positives and 30 false positives.


1 Answers

You can take advantage of scikit-learn, which is one of the best packages for machine learning in Python. Its SVM implementation uses libsvmand 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]]

Of course, you can use the libsvm tools you have mentioned, however they are designed to work only with binary classification whereas scikitallows you to work with multiclass.

like image 158
jabaldonedo Avatar answered Oct 06 '22 00:10

jabaldonedo