Understanding Confusion Matrix

Introduction 

The confusion matrix is one of the most confusing topics in machine learning. In this article, we going to learn everything about the confusion matrix-like what is a confusion matrix, why do we need a confusion matrix, and how to use the confusion matrix, so let's start with why we need a confusion matrix?

Why do we need a confusion matrix?

Suppose we have a problem in identifying the square and circle so we need to design a classifier that takes input as an image and identifies that in the image, there are how many shapes are circle and how many shapes are square. Suppose you successfully designed that classifier.


Figure Classifier

So if we want to check the accuracy of the designed classifier or wants to evaluate the classifier and how accurate result are produced by the classifier we need a confusion matrix.


Figure-Need for confusion Matrix

Here we got the confusion matrix. Sonfusion matrix is a matrix that helps to check the model's accuracy, by giving different conditions to the Model and taking back the response from the model, and checking how accurate results are being processed by the model or classifier.

What is the confusion matrix?


Figure-Confusion matrix

So confusion matrix is an NXN matrix that helps in evaluating a classifier based on the number of times the classifier guesses the actual result that was desired or confused. Before I start to explain in detail I want to introduce you to these terms that are being used in the confusion matrix 

  1. Actual values
  2. Predicted values

Actual values

Let's take an example to understand actual values. Suppose we have a classifier that predicts color, and we have a red ball as input. Our classifier can guess whether it is a blue or red ball but the actual color of the ball is red. The red color is an actual value. In the case of a classifier if the classifier predicts some result, then the result could be true or false but the actual value of the data is always correct. The prediction could be correct or incorrect.

Predicted values

Predicted values are predicted outputs or predictions done by the classifier. Let's take an example to understand the concept of the predicted Values. Suppose you are going to the office and you assume, that after 10 minutes you will be in your office. The amount of time that you predicted is a predicted value. In the case of a classifier when it produces some output the output values are the predicted value, whether it is right or wrong.

Now let's focus on other terms used in the confusion matrix 

  1. True-Negative
  2. True-Positive 
  3. False-Negative 
  4. False-Positive

True-Negative  

True negative contains the data that is wrong like in a shapes collection circles are not square. Suppose we created a classifier to identify the squares. So in the collection, all the circles are not square. It's true that squares are not a circle but the response is negative that is why it's true negative

True-Positive 

True positive means that the values are truly positive. Let's take our old example in a collection of shapes. If we want to identify the squares and we get a shape to identify that is square it means it's true that it is a square and the response is also positive, that is why its true-positive

False-Negative

False-negative means if in the shapes collection there is a shape actually square and the classifier predicts it as circle. It means this prediction is false. Because this shape is actually a square, but the classifier says that it is a circle. So in this condition, the response is also negative. So that is why it's called a false negative.

False-Positive 

False-positive means that if in a collection of shapes we get a shape that is a circle, and the classifier says that this shape is a square, in this condition classifier’s prediction is false but its response is positive because the classifier says yes it is a square. So that is why it's called a false positive.

Note

It’s very important to understand true positive, true negative, false positive, and false negative to understand the confusion matrix. If it is not clear then please read it again then we go to the next point.

Let’s calculate the Accuracy, Error Rate, Precision, and Recall. For an example first I want to introduce these terms 

Accuracy

Accuracy defines the accuracy of the classifier. To calculate the Accuracy we use this formula

Accuracy=(TP+TN)/ (Total no of prediction )

Here 

  • TP - stands for True Positive 
  • TN - stands for True Negative

Error Rate

Error Rate defines the rate of error in a classifier. To calculate the Error Rate we have two ways. If we have the value of accuracy then we can get Error Rate by just using Formula 

Error Rate =( 1- Accuracy)

Or 

If we don’t have the value of Accuracy then we can another formula 

Error Rate =(FP+FN)/ (Total no of prediction )

Here

  • FP - stands for False Positive 
  • FN - stands for False Negative 

Both processes give the same result

Precision

We can calculate the precision value by using this formula

Precision =TP/(Predicted yes)

Here 

  • TP - stands for True Positive 

Recall

To calculate Recall value we use this formula

Recall=TP/(Actual yes )

Let's take an example.

Suppose we have a classifier which identifies the shapes in the collection of shapes and this is the performance of the classifier, which is given in this Confusion Matrix, let's evaluate the classifier


Figure-Confusion Matrix Example

Here in this matrix

  • Total no of prediction=165
  • TP(True Positive)=100
  • TN(True Negative)=50
  • FN(False Negative)=5
  • FP(False Positive)=10

So let's calculate The Accuracy first of the classifier

Accuracy=(TP+TN)/ (Total no of prediction )
Accuracy=(100+50)/165
Accuracy=150/165
Accuracy=0.91

Let's calculate Error Rate of the classifier

Error Rate =( 1- Accuracy)
Error Rate =( 1-0.91)
Error Rate =0.09

Or we can calculate it with another formula that is,

Error Rate =(FP+FN)/ (Total no of prediction )
Error Rate =(10+5)/165
Error Rate =(15)/165
Error Rate =0.09

Let's calculate Precisionvalue of the classifier

Precision =TP/(Predicted yes)
Predicted yes=TP+FP
Precision =100/(100+10)
Precision=100/(110)
Precision =0.90

Let's calculate Recall value of the classifier

Recall=TP/(Actual yes )
Actual yes=TP+FN
Actual yes=100+5
Actual yes=105
Recall=100/(105)
Recall=0.95

Summary

In this article, we have learned about the confusion matrix, and also we are now able to calculate the Recall, Error Rate, Accuracy, and precision. Confusion matrix is a very important matrix to observe the actual performance of the classifier Model based on actual values vs predicted values.


Similar Articles