Confusion Matrix in Machine Learning

Jaimini chaturvedi
4 min readAug 1, 2019

Usually in supervised learning, A confusion matrix is a matrix (table) that can be used to performance measurement technique for Machine learning classification.

It is a kind of matrix(table) which helps you to summarize, describe or evaluate the performance of the classification model on a set of test data for that the true values are known.

Each column of the confusion matrix represents the instances of an actual class and each row represents the instances of a predicted class. , but it can be the other way around as well, i.e. column for predicted classes and row for actual classes.

The confusion matrix visualizes the accuracy of a classifier by comparing the actual and predicted classes.

In a 2-class case, i.e. “negative” and “positive”, the confusion matrix may look like this:

2 rows and 2 columns are consist of,

True Positives, True Negatives

False Positives, False Negatives

  • TP(True Positive): True Positive: Predicted values correctly predicted as actual positive
  • FP(False Positive): Predicted values incorrectly predicted an actual positive. i.e., Negative values predicted as positive
  • FN(False Negative): False Negative: Positive values predicted as negative
  • TN(True Negative): True Negative: Predicted values correctly predicted as an actual negative

Let’s understand it with an example of predict the text(Image having text or not):

· True Positives (TP): We predict for Positive (image containing text) & actual have the image containing text.

· True Negatives (TN): We predict for Negative (image not containing text) & actual don’t have text in image.

· False Positives (FP): We predict for Positive (image containing text) & in actual don’t have the text. (Also known as a “Type I error.”)

· False Negatives (FN): We predict for Negative (image not containing text) & In actual have the text in image. (Also known as a “Type II error.”)

Now let’s take a numerical example ofanimal classification,

Let us assume that we have a sample of 20 animals, e.g. 10 dogs and 10 cats. The confusion matrix of our recognition algorithm may look like the following table:

In above confusion matrix our system correctly predicted eight of the 10 actual dogs, but in two cases it took a dog for a cat. The ten actual cats were correctly recognized in nine cases but in one case a cat was taken to be a dog.

Now the understand the list of rates(Accuracy, True positive rate, False positive rate, Precision and Recall) that are calculated from a confusion matrix for a binary classifier

Accuracy : Accuracy means overall how often classifier is correct

True Positive Rate(Dog) : When it’s actually yes, how often does it predict yes?

True Negative Rate (Dog): · When it’s actually no, how often does it predict no?

Precision :

The precision metric shows the accuracy of the positive class. It measures how likely the prediction of the positive class is correct(Means when it predicts yes, how often is it correct).

Recall :

Recall is the fraction of cases where the algorithm correctly predicted out of all of the cases which are labelled..

Recall metric. Recall is also called sensitivity or true positive rate.

Note:

High recall, low precision: Indicates that most of the positive examples are correctly recognized (low FN) but there are a lot of false positives.

Low recall, high precision: Indicates that we miss a lot of positive examples (high FN) but those we predict as positive are indeed positive (low FP).

F1 score :

F1 Score is the weighted average of Precision and Recall. Therefore, this score takes both false positives and false negatives into account. Intuitively it is not as easy to understand as accuracy, but F1 is usually more useful than accuracy, especially if you have an uneven class distribution. Accuracy works best if false positives and false negatives have similar cost. If the cost of false positives and false negatives are very different, it’s better to look at both Precision and Recall.

F1 Score = 2*(Recall * Precision) / (Recall + Precision)

If you enjoyed reading this post, please share and give some clapps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

--

--