Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn, get accuracy scores for each class

Is there a built-in way for getting accuracy scores for each class separatetly? I know in sklearn we can get overall accuracy by using metric.accuracy_score. Is there a way to get the breakdown of accuracy scores for individual classes? Something similar to metrics.classification_report.

from sklearn.metrics import classification_report from sklearn.metrics import accuracy_score  y_true = [0, 1, 2, 2, 2] y_pred = [0, 0, 2, 2, 1] target_names = ['class 0', 'class 1', 'class 2'] 

classification_report does not give accuracy scores:

print(classification_report(y_true, y_pred, target_names=target_names, digits=4))  Out[9]:         precision    recall  f1-score   support  class 0     0.5000    1.0000    0.6667         1 class 1     0.0000    0.0000    0.0000         1 class 2     1.0000    0.6667    0.8000         3  avg / total     0.7000    0.6000    0.6133         5 

Accuracy score gives only the overall accuracy:

accuracy_score(y_true, y_pred) Out[10]: 0.59999999999999998 
like image 548
CentAu Avatar asked Sep 29 '16 12:09

CentAu


People also ask

How do you calculate accuracy for each class in a multi-class classification Python?

To calculate accuracy, use the following formula: (TP+TN)/(TP+TN+FP+FN). Misclassification Rate: It tells you what fraction of predictions were incorrect. It is also known as Classification Error. You can calculate it using (FP+FN)/(TP+TN+FP+FN) or (1-Accuracy).

How do you calculate accuracy in multi-class classification?

Accuracy is one of the most popular metrics in multi-class classification and it is directly computed from the confusion matrix. The formula of the Accuracy considers the sum of True Positive and True Negative elements at the numerator and the sum of all the entries of the confusion matrix at the denominator.

How is accuracy score calculated in sklearn?

Count the number of matches. Divide it by the number of samples.

How do you find accuracy score?

Accuracy represents the number of correctly classified data instances over the total number of data instances. In this example, Accuracy = (55 + 30)/(55 + 5 + 30 + 10 ) = 0.85 and in percentage the accuracy will be 85%.


1 Answers

from sklearn.metrics import confusion_matrix y_true = [2, 0, 2, 2, 0, 1] y_pred = [0, 0, 2, 2, 0, 2] matrix = confusion_matrix(y_true, y_pred) matrix.diagonal()/matrix.sum(axis=1) 
like image 200
javac Avatar answered Oct 07 '22 17:10

javac