Solved – Find true negatives in a confusion matrix

computational-statisticsconfusion matrixmachine learning

I'm trying to find the True negative in a confusion matrix, I have computed successfully from scratch the precision and recall/sensibility, now i need to compute the accuracy and specificity.
This is my confusion Matrix:

cm

Computing the precision for 0 class

Precision=  TP*100/(TP+FP)
precision = 66*100/(66+2+0)
precision = 97.0588

Computing the precision for 1 class

Precision=  TP*100/(TP+FP)
precision = 81*100/(81+1+1)
precision = 97.5903

Computing the precision for 2 class

Precision=  TP*100/(TP+FP)
precision = 56*100/(56+3+0)
precision = 94.9152

Using the Pycm library I got:

PPV(Precision or positive predictive value) 0.97059  0.9759 0.94915

where 0.97059 is the precision for the class 0, and the next for 1 and the last for the 2 class.

Computing the recall for 0 class

recall =  TP*100/(TP+FN)
recall =  66/(66+2+0)
recall =  97.0588

Computing the recall for 1 class

recall =  TP*100/(TP+FN)
recall =  81/(81+2+3)
recall =  94.1860

Computing the recall for 2 class

recall =  TP*100/(TP+FN)
recall =  56/(56+0+1)
recall =  94.1860

Using Pycm library I got:

TPR(recall or true positive rate)  0.98507 0.94186  0.98246

where 0.98507 is the precision for the class 0, and the next for 1 and the last for the 2 class.

What happen now if I want to compute the accuracy? equation: Accuracy = (TP+TN)*100/(TP+TN+FP+FN) the equation is ok? I'm using the constant 100 to get the percent and not 0.x or 0.00xx but 90.x etc.

accuracy equation

I would like to know how I can get the True Negatives (TN) to compute the accuracy and specificity, currently using Pycm library I'm getting this values for the 3 classes:

ACC(Accuracy) 0.98571       0.96667       0.98095

Best Answer

I'm one of the PyCM developers.

  1. Your precision calculation method is completely correct.
  2. For recall calculation you should consider improperly classified items in each row :

class 0 :

recall =  TP*100/(TP+FN)
recall =  66/(66+1+0)
recall =  98.5074

class 1 :

recall =  TP*100/(TP+FN)
recall =  81/(81+2+3)
recall =  94.1860

class 2 :

recall =  TP*100/(TP+FN)
recall =  56/(56+0+1)
recall =  98.2456
  1. Accuracy formula is correct.
  2. Finding TN for each class :

You should consider class vs other and add up items that classified correctly as other, in other words eliminate row and col related to class and add up remaining.

class 0 :

accuracy = (TP+TN)*100/(TP+TN+FN+FP)
accuracy = (66+141)/(66+141+2+1)
accuracy = 98.5714

class 1 :

accuracy = (TP+TN)*100/(TP+TN+FN+FP)
accuracy = (81+122)/(81+122+2+5)
accuracy = 96.6666

class 2 :

accuracy = (TP+TN)*100/(TP+TN+FN+FP)
accuracy = (56+150)/(56+150+1+3)
accuracy = 98.0952

Best Regards

Sepand Haghighi