I have the following confusion matrix:
[[5584 0 11 6 6 24 27 3 262 0]
[ 0 6419 39 14 3 41 4 4 214 4]
[ 17 21 5287 67 59 19 48 34 402 4]
[ 20 13 95 5238 0 150 21 36 505 53]
[ 9 12 29 8 5245 6 26 13 384 110]
[ 21 15 21 127 45 4469 66 11 594 52]
[ 25 15 37 2 31 73 5566 6 163 0]
[ 18 9 43 13 37 12 4 5724 250 155]
[ 12 52 33 83 2 101 24 4 5509 31]
[ 19 18 25 51 102 25 1 147 517 5044]]
and I would like to calculate the accuracy of each class. I have found on the internet that it should be calculated as follows: (TP+TN)/(TP+TN+FP+FN)
but when I try to implemented I basically get lost as I do not understand how to calculate any of the members except the TP
which is the item on the diagonal.
I also thought to calculate it as the TP over the total number of times a class exists in the dataset (i.e. the row sum) but that would be equivalent to calculate the recall.
Would you be able to make a practical example on how to calculate the accuracy of class number 5 please?
This is what I tried so far in Python:
def accuracy_scores(confusion_matrix):
msg = "accuracy\n\n"
accuracies = []
for i in range(confusion_matrix.shape[0]):
tp = confusion_matrix[i][i]
fn = sum(np.delete(confusion_matrix[i], i))
fp = sum(np.delete(confusion_matrix[:, i], i))
tn = sum(sum(np.delete(np.delete(confusion_matrix, i, 0), i, 1)))
accuracy = (tp+tn)/(tp+ fn+ fp+ tn)
accuracies.append(accuracy)
msg += str(round(accuracy,2))+"\n"
msg += f"accuracy avg: {round(np.mean(accuracies),2)}\n\n"
return msg