Confusion Matrix is an n*n matrix used to evaluate the performance of a classification model. The general idea is to count the number of times instances of class A(color green) are classified as class B(color red).
For Example, You can see in the above image
The first row of this matrix considers Green colors (the positive class).32 were wrongly classified as non-greens (false negatives), while the remaining 112 were correctly classified as greens (true positives).The second row of the matrix considers the red color (the negative class).56 of them were correctly classified as reds (they are called true negatives), while the remaining 64 were wrongly classified as reds (false positives).
#Implementation using Sklearn
from sklearn.metrics import confusion_matrix
confusion_matrix(actual_values, predicted_values)
>> array([[56, 64],
[ 32, 112]])
A perfect classifier would have only true positives and true negatives, so its confusion matrix would have nonzero values only on its main diagonal (top left to bottom right).
from sklearn.metrics import confusion_matrix
confusion_matrix(actual_values, predicted_values)
>> array([[120, 0],
[ 0, 144]])
Other Evaluation Metrics related to Confusion Matrix
- Precision: Accuracy of the Positive Prediction is called Precision of the classifier.
- Recall [sensitivity | the true positive rate (TPR)]: The ratio of positive instances that are correctly detected by the classifier.
#precesion = TP / TP + FP
#recall = TP / TP + FN
from sklearn.metrics import precision_score, recall_score
precision_score(actual_values, predicted_values) #== 112/ (112+ 64) = 0.63
recall_score(actual_values, predicted_values) #== 112/ (112+ 32) = 0.77
It is often convenient to combine precision and recall into a single metric called the F1 score.
#F1 score = 2 / 1 / precesion + 1 / recall
from sklearn.metrics import f1_score
f1_score(actual_values, predicted_values)
What if I told you that I was confused while writing this article ๐ . Just kidding..... Please like ๐ / share, if you find this article useful.Thank you ๐
โThe bad news is time flies. The good news is youโre the pilot.โ
โ Michael Altshuler