Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F1-score per class for multi-class classification

I'm working on a multiclass classification problem using python and scikit-learn. Currently, I'm using the classification_report function to evaluate the performance of my classifier, obtaining reports like the following:

>>> print(classification_report(y_true, y_pred, target_names=target_names))
             precision    recall  f1-score   support

    class 0       0.50      1.00      0.67         1
    class 1       0.00      0.00      0.00         1
    class 2       1.00      0.67      0.80         3

avg / total       0.70      0.60      0.61         5

To do further analysis, I'm interesting in obtaining the per-class f1 score of each of the classes available. Maybe something like this:

>>> print(calculate_f1_score(y_true, y_pred, target_class='class 0'))
0.67

Is there something like that available on scikit-learn?

like image 570
Guillermo Guardastagno Avatar asked Jun 03 '16 13:06

Guillermo Guardastagno


People also ask

Can I use F1 score for multi-class classification?

Implementation. In the Python sci-kit learn library, we can use the F-1 score function to calculate the per class scores of a multi-class classification problem. We need to set the average parameter to None to output the per class scores.

What is the best metric for multiclass classification?

Most commonly used metrics for multi-classes are F1 score, Average Accuracy, Log-loss. There is yet no well-developed ROC-AUC score for multi-class.


1 Answers

Taken from the f1_score docs.

from sklearn.metrics import f1_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]

f1_score(y_true, y_pred, average=None)

Ouputs:

array([ 0.8,  0. ,  0. ])

Which is the scores for each class.

like image 128
piman314 Avatar answered Sep 17 '22 12:09

piman314