Confusion Matrix for N X N Matrix

Akash Borgalli
3 min readJan 10, 2022

--

Hello Everyone,

Hope you all are doing well??

Today I would be talking about how to evaluate the performance of the Classification model using the confusion Matrix. So, Let’s get Started…

What is Confusion Matrix? (There is nothing confusing in this type of matrix until you know it)

  • It shows the difference between Actual and Predicted values. It tells us how many data points were correctly predicted and which ones were not correctly predicted.
  • It is a N * N Matrix, where n is the no. of categories in the output column which is also called as target attribute.
  • So if there are 2 classes, you will get a 2*2 Matrix. If there are 3 classes, then you will get a 3*3 matrix and so on. Basically, the point I am trying to make here is more the classes that many numbers of confusion matrix it will try to create but they would be having the equal number of rows and columns.

To begin with, let’s try to know some common terms and there are TP(True Positive), FP(False Positive), True Negative(TN), and FN(False Negative).

We would try to see a 3 x 3 Matrix where we would be calculating TP, TN, FP, FN values using the tricks that I am going to show you. This trick can be applied for 4*4, 5*5…N*N Matrix as well.

So let’s jump right into it…

Consider this confusion matrix has classes A, B, C in an output column of the dataset in the below Figure 1.

Figure 1

We will try calculating TP(True Positive), FP(False Positive), True Negative(TN), and FP(False Negative) values for Class A.

TruePositive(A): It tells the actual value and also the predicted values are the same. The true positive rate for class A is nothing but the actual value and the predicted value is the same which means the value for cell 1 is 15.

FalsePositive(A): It tells the actual value is negative in our case it is Class B & C but the model has predicted it as positive i.e Class A. It’s the addition of values of the corresponding column except for the TP value.

FalsePositive(A) = (cell 4 + cell 7)

= (7 + 2)

= 9

TrueNegative(A): It tells the actual value and also the predicted values are the same meaning for Class A-B & C are Negative classified. It’s the addition of values of all rows and columns leaving the class that we are calculating which is, Class A.

TrueNegative(A) = (cell 5 + cell 6 + cell 8 + cell 9)

= 15 + 8 +3 + 45

= 71

FalseNegative(A): This tells the actual value is positive in our case it is Class A but the model has predicted it as negative which is Class B & C. It’s calculated by the addition of neighboring rows except for the TP value.

FalseNegative(A) = (cell 2 + cell3)

= 2 + 3

= 5

It’s time now to calculate Precision, Recall, and Accuracy for Class A.

Precision: It tries to answer how many positive results you would be able to give correctly.

Precision(A) = Correctly Predicted / Total Predicted = 15/24 = 0.625

Recall: It tries to predict how many positive predictions were actually positive predicted

Recall(A)= Correctly Classified / Total Actual = 15/20 = 0.75

Accuracy of this classifier = Total correctly classified / Total Actual = 15 + 15+ 45/100 = 0.75

Similarly, you can calculate TP, FP, FN, TN, Precision, Recall for Class B and C

We all got to know how we can evaluate our classification model but why we should use Confusion Matrix?

So answer to this question would be with the help of Confusion Matrix, we can calculate Recall, Precision, Accuracy, AUC-ROC curve to evaluate the performance of the model.

Thank you for patiently reading it and Keep Learning!!

--

--

Akash Borgalli
Akash Borgalli

Written by Akash Borgalli

I am Azure AI Engineer. I work in CloudThat as Research Assosiate. I love learning new things related to AI and ML.

No responses yet