# How to Understand A Confusion Matrix?

After fitting your data to a ML algorithm, you have a predictions of the independent variable. Now you need to evaluate how well you model is. In classification problems, we need to build a model that is able to classify the data in a way that suits our use case. In this blog we will view the confusion matrix. How to be understood and interpreted.

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

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 is our model sensitive to predict true positive values. It is called also **Recall.**

**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.

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

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

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