How to Read a Confusion Matrix
If you're like me, most of the time you're confused by reading the confusion matrix. After all, it was named after such truth.
A confusion matrix is a table used for displaying the number of samples (in classification problems) that are correctly and incorrectly classified in positive and negative classes.
While a confusion matrix can help us to calculate classification metrics (like accuracy, precision, or recall, and f1 score), it is not itself a metric.
Before we see how to read this kind of matrix, let's see what it is made of.
The Elements of Confusion Matrix
A confusion matrix is made of 4 main elements: True negatives, false negatives, true positives, and false positives.
True Positives(TP): These are a number of samples that are correctly classified as positive, and their actual label is positive.
False Positives (FP): Number of samples that are incorrectly classified as positive, when in fact their actual label is negative.
True Negatives (TN): Number of samples that are correctly classified as negative, and their actual label is negative.
False Negatives (FN): Number of samples that are incorrectly classified as negative, when in fact their actual label is positive.
When you understand the above, you can read any confusion matrix. Let's use that information to read two or more classes' confusion matrices.
Reading Two Classes Matrix
Unless the matrix is transposed, the rows represent the actual labels and the column represents the predicted labels.
Here is a template of how a two-row-column confusion looks like.
While the above image gives the format, it's still hard without using a real-world example.
So, let's assume that we are building a Malignant and Benign classifier. We want to see the number of samples that are correctly/incorrectly classified in both Malignant and Benign classes.
Here is how the confusion matrix of the classifier would look like:
Let's interpret the above.
36 Malignant samples were correctly classified as Malignant. They are true negatives.
7 Malignant samples were incorrectly classified as Benign: False Positives
0 Benign samples were incorrectly classified as Malignant: False Negatives
71 Benign samples were correctly classified as Benign: True Positives
Reading Three or More Classes Matrix
Reading the confusion matrix of 3 or more classes can be a bit harder, but the idea is the same.
The row represents the actual labels, and the column represents the predicted labels.
Take an example: Reading the confusion matrix of 3 classes in mobile phone price ranges predictions.
There are 4 classes of price ranges: low, medium, high, very high.
Rows represent actual predicted prices ranges, columns represent predicted price ranges.
Interpreting the matrix above,
The model correctly classified 488 low-cost phones as low cost, 12 low-cost phones were incorrectly classified as medium-cost phones.
The model correctly classified 441 medium-cost phones as medium phones, incorrectly classified 31 medium-cost phones as low cost when in fact they are medium cost, and also incorrectly classified as 28 medium costs phones as high cost when they are not
430 were correctly classified as high cost, 35 high-cost incorrectly classified as medium-cost and very high cost.
490 very high-cost phones were correctly classified as very high cost, 10 very high-cost phones were incorrectly classified as high cost.
As you can see, reading any kind of confusion matrix comes to remembering one thing: rows represent the actual labels(unless the matrix is transposed) and columns represent predicted labels.
Understanding that will help you read and interpret any confusion matrix, whether it's for two classes (binary classification) or more than two classes (multiclass classification).
Thanks for reading.
Each week, I write one article about machine learning techniques, ideas, or best practices. You can support me by sharing the article with friends or tech communities that you are a part of.
And connect with me on Twitter!