A  Beginner’s Guide To Confusion Matrix: Machine Learning 101
Tags: datascience, confusionmatrix, statistics, ai
Published: 2020-05-28 04:36:38

---
title: A  Beginner’s Guide To Confusion Matrix: Machine Learning 101
published: true
date: 2020-05-23 14:08:54 UTC
tags: datascience, confusionmatrix, statistics, artificialintellige
cover_image : 
canonical_url: https://medium.com/swlh/a-beginners-guide-to-confusion-matrix-machine-learning-101-77280db06c4b
---

### A Beginner’s Guide To Confusion Matrix: Machine Learning 101

![A Beginner Friendly Guide to one of the most popular metrics : Confusion Matrix](https://cdn-images-1.medium.com/max/1024/0*pweSm85R89e81uPH)<figcaption>Photo by <a href="https://unsplash.com/@ffstop?utm_source=medium&amp;utm_medium=referral">Fotis Fotopoulos</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

When we build a Machine Learning model after training it on the training data, we often want to know how this model will perform in the real world. Hence we also test it on unseen data before deploying it on the cloud for production. But how can one be sure that our model is actually performing well or not? Thus evaluating Machine Learning models, prior to its deployment, is a crucial step for judging its performance. This is when the **Confusion Matrix** comes to the rescue.

When I first encountered this term while understanding Machine Learning, my first reaction was similar to as described below. 😝

> Confusion matrix_? Seriously? How do they even come up with these terms?_

Well, however, as vague and confusing this may sound, a **Confusion Matrix,** also known as an **Error Matrix,** is one of the most useful metrics to evaluate the performance of your classifier(classification model).

#### Definition

A confusion matrix is basically a nXn square matrix that describes a model’s performance, where n represents the number of classifications the model is trying to predict. It could either be a binary classification(2X2 matrix) or a multi-class classification(nXn).

#### Understanding the working of Confusion Matrix

Let us now understand how the confusion matrix helps us to evaluate the performance of a model on a 2X2 binary classification.  
For example, Let’s say we want to predict whether a person is suffering from Diabetes or Not which is clearly a binary classification(0/1 or Yes/No).

![](https://cdn-images-1.medium.com/max/1024/1*mdtqR2kyElMd0cCGM4gtuw.jpeg)

The rows represent the Predicted Values our model is predicting.  
The columns represent the Actual Values that we know beforehand.

The primary diagonal, i.e, the cells (1, 1) and (2, 2) represents the values that are predicted correctly by our model.   
While all the other cells represent values that are incorrectly predicted by our model.  
Let’s have a more in-depth understanding of each cell we are dealt with.

- The cell (1, 1) represents that our model is classifying a person to be diabetic and he/she is actually diabetic. This is also known as **True Positive**.
- The cell (2, 2) represents that our model is classifying a person to be NOT diabetic and he/she is actually NOT diabetic. This is also known as **True Negative**.
- The cell (1, 2) represents that our model is classifying a person to be diabetic and he/she is actually NOT diabetic. This is also known as **False Positive**.
- The cell (2, 1) represents that our model is classifying a person to be NOT diabetic and he/she is actually diabetic. This is also known as **False Negative**.

#### NOTE:

False Positive is also known as **Type 1 Error**.  
Similarly, False Negative is also known as **Type II Error**.

One of the measures you can derive from the confusion matrix is the _Accuracy_ and _Error_ Percentage.

1. **accuracy(%)** = _(True Positive+True Negative)\* 100/ Total\_values_
2. **error(%) =** _(False Negative+False Negative)\*100/ Total\_Values  
where_ Total\_values = TP + TN + FP + FN

Ideally, in our Machine Learning model the number of Type I error and Type II error should be as low as possible.   
Depending on the problem statement and its use case we should know up to what error percentage our model is acceptable.  
For example, the problem statement for Detecting Breast Cancer needs to have an extremely low error. Just imagine the situation if we say someone does NOT have cancer when she actually has. 😲  
There are certain methods to increase the performance of a model but in this post we’re only focussing on the evaluation aspect.

From the above Confusion Matrix we could also calculate some advanced measures to evaluate and understand our model even further. Some of these are enlisted below:

- **Sensitivity and Specificity**

_Sensitivity_ measures how well the model is able to detect events in the positive class. It is calculated as the number of correct positive predictions divided by the total number of actual positive values.  
_Sensitivity_ = _True Positive/(True Positive +False Negative)_

_Specificity_ measures how well the model is able to detect events in the negative class. It is calculated as the number of correct negative predictions divided by the total number of actual negative values.  
_Specificity_ = _True Negative/(True Negative +False Positive)_

- **Precision and Recall**

_Precision_ is the ratio of the number of correct positive predictions to the total number of predicted positive values.  
_Precision = True Positive/(True Positive +False Positive)_

_Recall_ is the ratio of the number of correct positive predictions to the total number of actual positive values. (_Sensitivity_)  
_Recall_ = _True Positive/(True Positive +False Negative)_

- **F-Score**

The F-Score or F-Measure combines both Precision and Recall.  
It is the Harmonic Mean of Precision and Recall.  
_F Score = 2 \* (recall\*precision)/(recall+precision)_

#### Image Reference:

1. [https://glassboxmedicine.com/2019/02/17/measuring-performance-the-confusion-matrix/](https://glassboxmedicine.com/2019/02/17/measuring-performance-the-confusion-matrix/)
2. [https://unsplash.com/@ffstoputm\_source=medium&utm\_medium=referral](https://unsplash.com/@ffstop?utm_source=medium&utm_medium=referral)

* * *