What is confusion matrix?
A confusion matrix is a table that is used to measure the performance of the machine learning classification model(typically for supervised learning, in the case of unsupervised learning is usually called the matching matrix) where output can be two or more classes. Each row of the confusion matrix represents the instances in a predicted class while each column represents the instance in an actual class or vice versa.
A confusion matrix is also known as an error matrix.
In this article, we will be dealing with the various parameters of the confusion matrix and the information that we can extract from it. The structure of the confusion matrix is as shown in the figure below.
Now let’s understand what are TP, FP, FN, TN.
Here we have two classes, Yes and No, then we define,
- TP-True positive: You predicted Yes class and its actual class is also Yes.
- TN-True negative: You predicted No class and its actual class is No.
- FP-False positive: You predicted the Yes class but actually it belongs to the No class. It is also called type 1 error.
- FN-False Negative: You predicted No class but actually it belongs to the Yes class. It is also called a type II error.
So, what are the classification performance metrics that we can calculate from the confusion matrix? Let’s see.
By observing the confusion matrix we can calculate the Accuracy, Recall, Precision, and F1-score(or F measure) of the classification model. Let’s understand them by taking an example of a confusion matrix.
Information we obtain from the above confusion matrix:
- There are altogether 165 data points (i.e. observations or objects) and they are classified into two classes Yes and No.
- Our classification model predicted Yes, 110 times, and No, 55 times But according to the actual classification, there are altogether 105, Yes and 60, No’s.
The confusion matrix including the above calculations is as given below,
Now, let’s understand the above metrics in brief.
- Accuracy: The accuracy of classification can be obtained by using the formula below,
ACCURACY = TP + TN / TP + TN + FP + FN
On the basis of the above confusion matrix, we can calculate the accuracy of the model as,
Accuracy = (100+50) /(100+5+10+50)= 0.90
The error of the classification is given as ,
Error = 1- Accuracy = 1 – 0.90 = 0.1.
- Precision: It tells, out of all the classes, how much our classifier predicted correctly. It should be high as possible. In other words, Precision tells us about when it predicts a class, how often is it correct. It is calculated by using the formula below,
PRECISION= TP / TP + FP
On the basis of the above confusion matrix, we can calculate the Precision of the model as,
Precision = 100/ (100+10)=0.91
- Recall: Recall tells us about when it is actually yes, how often does our classifier predict yes. or it can also be defined as, out of all the positive classes, how much our classifier predicted correctly. It is calculated by using the formula below,
RECALL = TP / TP + FN
On the basis of the above confusion matrix, we can calculate the Recall of the model as,
- F-measure(F1-Score): F-measure(F1-Score) is obtained as the harmonic mean of recall and Precision.It is calculated by using the formula below,
On the basis of the above confusion matrix, we can calculate the F-measure of the model as,