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
The question is misleading. Accuracy scores for each class equal the overall accuracy score. Consider the confusion matrix:
from sklearn.metrics import confusion_matrix
import numpy as np
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
#Get the confusion matrix
cm = confusion_matrix(y_true, y_pred)
print(cm)
This gives you:
[[1 0 0]
[1 0 0]
[0 1 2]]
Accuracy is calculated as the proportion of correctly classified samples to all samples:
accuracy = (TP + TN) / (P + N)
Regarding the confusion matrix, the numerator (TP + TN) is the sum of the diagonal. The denominator is the sum of all cells. Both are the same for every class.