Confusion matrix for Binary classification in Machine Learning

A confusion matrix is a technique for summarizing the performance of a classification algorithm. It is represented in an N x N matrix form where N is the number of target or output classes.

Let’s say we have gender classification problem where input is sample of 12 pictures where 8 are Males and 4 of Females .Lets classify Males belong to class 1(Positive) and Females belong to class 0(Negative) .

SampleActualPredictedTrue Condition
1MFFN
2MMTP
3FFTN
4MMTP
5MMTP
6FFTN
7MMTP
8FMFP
9MMTP
10FFTN
11MMTP
12MFFN

The 2X2 confusion matrix would be represented as below.

Actual
PredictedMaleFemale
Male61
Female23
Actual
Predicted PN
PTP = 6FP = 1
NFN = 2TN = 3

The table on right represents the same information in terms of true condition.Lets understand the terminologies

TP = A Positive’s prediction is True
FP = A Positive’s prediction is False ( Type I Error )
FN = A Negative’s prediction is True
TN = A Negative’s prediction is False ( Type II Error )

Classification accuracy is measured through different metrics.

  • Accuracy

This gives how much we predicted correctly.

Accuracy = correct predictions / total predictions
= ( TP + TN ) / ( TP + FP +TN +FN )

  • Precision

Precision = (TP) / (TP+FP)
Measure how many “Positive” predictions are actually correct from all “Positive” predictions .ie when predicted “Positive” how often it is correct .

  • Recall

Recall = (TP) / (TP+FN)
It is the measure of correctness of for predictions made for all the “Positive” class .

Classification report through sklearn

# Example of a confusion matrix in Python
from sklearn import metrics

#You need to do actual Predictions to arrive at below values
Actual    = [1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1]
Predicted = [0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0]

cm = metrics.confusion_matrix(Actual, Predicted)
print("============== Confusion Matrix ==================")
print(cm)

print("============ Classification Report ===============")
cr=metrics.classification_report(Actual, Predicted)
print(cr)

print("Precision score: {}".format(metrics.precision_score(Actual,Predicted)))
print("Recall    score: {}".format(metrics.recall_score(Actual,Predicted)))
print("F1        Score: {}".format(metrics.f1_score(Actual,Predicted)))

The output is  below.

Confusion_matrix
Classification Report

References

Leave a Reply