How to Understand A Confusion Matrix?

Bassem Essam
4 min readFeb 13, 2021
Photo by Emily Morter on Unsplash

After fitting your data to a ML algorithm, you have predictions of the independent variable. Now you need to evaluate how well is your model. Also, we need to set the proper decision thresholds of the model that fits the use case. In this blog we will review the confusion matrix. How to be understood and interpreted.

First, assume that the classification problem we have is binary which means that the predictions of the model is 1 or 0. If the predicted output belongs to that class or not. This can be illustrated in the following diagram.

Confusion Matrix for binary classification problem

The actual values are represented in columns and predicted values represented in rows. Now we have four possibilities.

1- True Positive: The predicted value is positive and correct.

2- True Negative: The predicted value is negative and correct.

3- False Positive (Type I Error): The predicted value is positive and incorrect (not predicted correctly).

4- False Negative (Type II Error): The predicted value is negative and incorrect(not predicted correctly).

Always keep in mind that we need to maximize the TP,TN diagonal as much as possible and we have to minimize FN,FP diagonal as much as possible, since they are the correctly predicted values.

Sensitivity and Specificity:

Sensitivity (True Positive Rate — TPR): It’s the number of correct positives (TP) out of the actual positive results. This rate is measuring how sensitive is our model to predict true positive values. It is called also Recall.

Sensitivity (TPR) from Confusion Matrix

Specificity (True Negative Rate — TNR): It’s the number of correct negatives out of the predicted negative values. It measures how specific is the model.

Let’s assume that our ML model is for detecting fraud credit card transactions. In this case, I need to classify each and every fraud transactions even if it is a normal one. Hence, I need to build a model with high sensitivity as getting all TP is more important (FN should be minimum).

On the other hand, If our ML model is for detecting Spam e-mails. In this case, If the user received an e-mail that is not a spam but the model classifies it as a spam, the user might miss an important mail. So, a model with high specificity is required as FP should be minimum in order to reduce the probability of classifying a mail as spam and it is not a spam (FP).

Accuracy and Precision:

Accuracy: It is the percentage of all correctly predicted values out of all predictions done. It tells us how accurate is our model.

Accuracy from Confusion Matrix

Precision: It is the percentage of true positive predictions out of total predicted positive values. It is called also Positive Prediction Value.

Precision from Confusion Matrix

Negative Prediction Value: It’s the percentage of True Negative predictions out of all negative predictions.

Negative Prediction Value from Confusion Matrix

Hint: To memorize all formulas, imagine the confusion matrix in you mind and apply the following rules.

1- Sensitivity(Recall), Specificity → Vertical columns, True values in the nominator.

2- Precision, Negative Prediction Value → Horizontal rows, True values in the nominator.

3- Accuracy → All true values / Total.

Thanks for Reading.

You can reach me at

https://www.linkedin.com/in/bassemessam

--

--