What Is the Confusion Matrix and How to Use It

A guide to the confusion matrix and how to use it

Khalid Salem
Analytics Vidhya
4 min readSep 3, 2020

--

By CANVA

What is the Confusion Matrix?

After building the machine learning model, we want to know how our model is doing. Can the model classify correctly or not. We do that by using a confusion matrix, which we use mainly for classification models. To get a better understanding of what is confusion matrix, let’s explain it in this example.

By CANVA

Let us say we have 1000 records of emails, and we want to apply a machine learning method to predict whether or not an email is a spam. We split these records to Training and Testing data, then we train the machine learning model with the training data, and then, we test our model using the testing data.

Now, we need to know the performance of our model on the testing data. We do that by creating a confusion matrix NxN, where “N” here refers to the number of labels; since we’re only having two categories, which are “Spam” and “Not Spam,” we’ll have a 2x2 Confusion Matrix.

Here, the rows represent the number of the model’s predicted value, and the columns represent the actual value of the output.

By Author

How to use the confusion matrix?

So, after applying the classification model to the testing data, we’ll fill the confusion matrix with how many emails are classified correctly as spam or not.

In the top left corner of the matrix, we enter the number of emails that the model predicts spam when it’s spam in real.

By Author

We call the top left corner the True Positive. It’s true because it’s classified correctly as positive — positive here is for spam while negative is for not spam.

While for the top right corner, it’s the False Positive, and it’s the number of emails that are not spam in real, but the model mis-predicted it as spam.

By Author

For the bottom left corner, it’s the False Negative, and it’s the number of emails that are spam in real, but the model mis-predicted it as not spam. Finally, the bottom right corner is the True Negative, and it’s the number of emails that are not spam in real, and the model predicted it correctly as not spam.

By Author

For example, here, there were 400 True Positives, spam emails were classified correctly, and 500 True Negatives, non-spam emails were classified correctly.

By Author

However, here, the model miss-classified 50 emails that weren’t spam by saying they are and miss-classified 50 emails that were spam by saying they aren’t.

By Author

Finally, the numbers along the diagonal in the green box tell us the number of correctly classified emails by the model.

By Author

While the numbers along the diagonal in the red box below tell us the number of emails classified wrongly by the model.

By Author

What can you get from the confusion matrix?

After we fill the matrix, we can calculate the model’s accuracy by summing the numbers in the green box above and dividing them by the total number of records. The confusion matrix is useful when we have many machine learning methods to apply. We want to know the best one with the highest accuracy, so we’ll have a confusion matrix for each machine learning method and pick the best one.

I hope you now understand what is the confusion matrix and how it works.

Thank you.

--

--