Understanding confusion matrix

gaurav gupta
4 min readJun 3, 2021

--

In machine learning, Classification is used to split data into categories. But after cleaning and preprocessing the data and training our model, how do we know if our classification model performs well? That is where a confusion matrix comes into the picture.

A confusion matrix is used to measure the performance of a classifier in depth. In this simple guide to Confusion Matrix, we will get to understand and learn confusion matrices better.

What is a Confusion Matrix?

A Confusion matrix is an N x N matrix used for evaluating the performance of a classification model, where N is the number of target classes. The matrix compares the actual target values with those predicted by the machine learning model. This gives us a holistic view of how well our classification model is performing and what kinds of errors it is making.

For a binary classification problem, we would have a 2 x 2 matrix as shown below with 4 values:

  • The target variable has two values: Positive or Negative
  • The columns represent the actual values of the target variable
  • The rows represent the predicted values of the target variable

But wait — what’s TP, FP, FN, and TN here? That’s the crucial part of a confusion matrix. Let’s understand each term below.

Understanding True Positive, True Negative, False Positive, and False Negative in a Confusion Matrix

True Positive (TP)

  • The predicted value matches the actual value
  • The actual value was positive and the model predicted a positive value

True Negative (TN)

  • The predicted value matches the actual value
  • The actual value was negative and the model predicted a negative value

False Positive (FP) — Type 1 error

  • The predicted value was falsely predicted
  • The actual value was negative but the model predicted a positive value
  • Also known as the Type 1 error

False Negative (FN) — Type 2 error

  • The predicted value was falsely predicted
  • The actual value was positive but the model predicted a negative value
  • Also known as the Type 2 error

Just from looking at the matrix, the performance of our model is not very clear.

To find how accurate our model is, we use the following metrics:

Accuracy:

The accuracy is used to find the portion of correctly classified values. it tells us how often our classifier is right. It is the sum of all true values divided by total values.

Precision:

Precision is used to calculate the model’s ability to classify positive values correctly. it is the true positives divided by the total number of predicted positive values.

Recall:

It is used to calculate the model’s ability to predict positive values. “How often does the model predict the correct positive values?”. It is the true positives divided by the total number of actual positive values.

F1-Score:

It is the harmonic mean of Recall and Precision. It is useful when you need to take both Precision and Recall into account.

Why you need a Confusion matrix?

Here are the pros/benefits of using a confusion matrix.

  • It shows how any classification model is confused when it makes predictions.
  • The confusion matrix not only gives you insight into the errors being made by your classifier but also the types of errors that are being made.
  • This breakdown helps you to overcomes the limitation of using classification accuracy alone.
  • Every column of the confusion matrix represents the instances of that predicted class.
  • Each row of the confusion matrix represents the instances of the actual class.
  • It provides insight not only into the errors which are made by a classifier but also errors that are being made.

So in this article, I have explained what is confusion matrix, how to calculate the performance of our model using the confusion matrix using various metrics, and why do we need it.

--

--

gaurav gupta
gaurav gupta

No responses yet