close

How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?

  1. How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?

    Lot of very detailed answers here but I don't think you are answering the right questions. As I understand the question, there are two concerns:

  2. compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn

    Lot of very detailed answers here but I don't think you are answering the right questions. As I understand the question, there are two concerns:

Method 1

Lot of very detailed answers here but I don’t think you are answering the right questions. As I understand the question, there are two concerns:

  1. How to I score a multiclass problem?
  2. How do I deal with unbalanced data?

1.

You can use most of the scoring functions in scikit-learn with both multiclass problem as with single class problems. Ex.:

from sklearn.metrics import precision_recall_fscore_support as score

predicted = [1,2,3,4,5,1,2,1,1,4,5] 
y_test = [1,2,3,4,5,1,2,1,1,4,1]

precision, recall, fscore, support = score(y_test, predicted)

print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('fscore: {}'.format(fscore))
print('support: {}'.format(support))

This way you end up with tangible and interpretable numbers for each of the classes.

| Label | Precision | Recall | FScore | Support |
|-------|-----------|--------|--------|---------|
| 1     | 94%       | 83%    | 0.88   | 204     |
| 2     | 71%       | 50%    | 0.54   | 127     |
| ...   | ...       | ...    | ...    | ...     |
| 4     | 80%       | 98%    | 0.89   | 838     |
| 5     | 93%       | 81%    | 0.91   | 1190    |

Then…

2.

… you can tell if the unbalanced data is even a problem. If the scoring for the less represented classes (class 1 and 2) are lower than for the classes with more training samples (class 4 and 5) then you know that the unbalanced data is in fact a problem, and you can act accordingly, as described in some of the other answers in this thread. However, if the same class distribution is present in the data you want to predict on, your unbalanced training data is a good representative of the data, and hence, the unbalance is a good thing.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read