Confusion Matrix is No More a Confusion| Evaluate Classification Problems
The Confusion Matrix: A Powerful Tool for Evaluating Classification Models
If you‘re involved in data science and machine learning, you‘ve likely come across the concept of a confusion matrix. Despite its perplexing name, a confusion matrix is actually a very useful tool for understanding the performance of a classification model. In this post, we‘ll demystify the confusion matrix and explore how you can leverage it to gain valuable insights into your model‘s strengths and weaknesses.
What is a Confusion Matrix?
A confusion matrix, also known as an error matrix, is a specific table layout that allows visualization of the performance of a classification algorithm. Each row of the matrix represents the instances in an actual class while each column represents the instances in a predicted class. The name stems from the fact that it makes it easy to see if the system is confusing two or more classes.
The confusion matrix is a 2×2 table for a binary classification problem, with four possible outcomes:
- True Positives (TP): The model predicted the positive class and the actual value was also positive.
- False Positives (FP) or Type I Error: The model predicted the positive class but the actual value was negative.
- False Negatives (FN) or Type II Error: The model predicted the negative class but the actual value was positive.
- True Negatives (TN): The model predicted the negative class and the actual value was also negative.
Here‘s what a confusion matrix looks like:
Predicted: No Predicted: Yes
Actual: No TN FP
Actual: Yes FN TP
Calculating Evaluation Metrics
From the confusion matrix, we can calculate several important evaluation metrics:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Accuracy measures the overall correctness of the model‘s predictions. While it‘s an intuitive metric, accuracy alone can be misleading if the classes are imbalanced. For example, if 95% of samples are negative, a model that always predicts the negative class will have 95% accuracy but fails to identify any positive cases.
Precision = TP / (TP + FP)
Precision tells us what proportion of positive identifications were actually correct. A model with high precision has a low false positive rate. Precision is important to optimize when the cost of a false positive is high, such as email spam detection where a non-spam email getting marked as spam is highly undesirable.
Recall (Sensitivity) = TP / (TP + FN)
Recall tells us what proportion of actual positives were identified correctly. A model with high recall has a low false negative rate. Recall is important to optimize when the cost of a false negative is high, like cancer diagnosis where a missed malignant tumor could be life-threatening.
F1 Score = 2 Precision Recall / (Precision + Recall)
The F1 score is the harmonic mean of precision and recall, providing a balanced evaluation metric. F1 is useful when you want to seek a balance between precision and recall and there is an uneven class distribution. The F1 score favors classifiers that have similar precision and recall.
Interpreting Confusion Matrices
Let‘s walk through a few examples of interpreting confusion matrices for different applications.
Example 1: Medical Diagnosis
Consider a model that predicts whether a patient has a certain disease based on test results. Here‘s the resulting confusion matrix:
Predicted: Healthy Predicted: Sick
Actually Healthy 95 5
Actually Sick 10 90
- TP = 90 sick patients correctly diagnosed
- TN = 95 healthy patients correctly diagnosed
- FP = 5 healthy patients incorrectly diagnosed as sick
- FN = 10 sick patients incorrectly diagnosed as healthy
Accuracy = (90 + 95) / 200 = 0.925
Precision = 90 / (90 + 5) = 0.947
Recall = 90 / (90 + 10) = 0.900
In this medical context, false negatives (sick patients told they are healthy) are more concerning than false positives. The 90% recall indicates the model is doing a good job catching most of the sick cases, but there is room for improvement. The high precision of 95% means when it does predict a patient is sick, it‘s usually correct. Overall this model looks promising but the false negatives should be analyzed further to see if the recall can be increased.
Example 2: Spam Email Classification
Consider an email spam classifier with the following confusion matrix on a test set:
Predicted: Ham Predicted: Spam
Actually Ham 980 20
Actually Spam 30 970
- TP = 970 spam emails correctly classified
- TN = 980 ham (non-spam) emails correctly classified
- FP = 20 ham emails incorrectly classified as spam
- FN = 30 spam emails incorrectly classified as ham
Accuracy = (970 + 980) / 2000 = 0.975
Precision = 970 / (970 + 20) = 0.980
Recall = 970 / (970 + 30) = 0.970
For spam detection, false positives (legitimate emails getting classified as spam) are more problematic than letting a few spam emails slip through the cracks. The 98% precision indicates the model is rarely making the mistake of classifying a ham email as spam. While not perfect, the 97% recall means the vast majority of actual spam is getting caught. This looks like a fairly robust spam classifier.
Example 3: Customer Churn
Consider a model predicting whether a customer will churn (stop doing business with a company) or not churn. The confusion matrix is:
Predicted: Not Churn Predicted: Churn
Actual: Not Churn 9000 400
Actual: Churn 500 100
- TP = 100 churned customers correctly predicted
- TN = 9000 non-churned customers correctly predicted
- FP = 400 non-churned customers incorrectly predicted to churn
- FN = 500 churned customers incorrectly predicted to not churn
Accuracy = (100 + 9000) / 10000 = 0.910
Precision = 100 / (100 + 400) = 0.200
Recall = 100 / (100 + 500) = 0.167
Despite the 91% accuracy looking decent at first glance, when we look at precision and recall it‘s apparent the model is struggling to identify the churned customers. Only 20% of the customers predicted to churn actually churned, and it‘s only identifying 17% of the customers who ended up churning.
The low precision and recall for the churn class is likely due to the class imbalance, with non-churned being the dominating class. In this case, accuracy is an overly optimistic metric. To improve this churn model, techniques for dealing with class imbalance should be explored, such as adjusting class weights, oversampling the minority class, or trying different algorithms. Looking at the confusion matrix and related metrics surfaced an important weakness of the model.
Additional Confusion Matrix Techniques
- Normalized confusion matrix: Each row is divided by the row total to see the percentage breakdown
- Plotting a confusion matrix: Use a heatmap to visualize the matrix, which can more powerfully show the distribution compared to raw numbers
- Multi-class confusion matrix: The concept extends beyond binary classification to cases with 3 or more classes
Key Takeaways
- A confusion matrix provides a wealth of information on a classification model‘s performance
- Accuracy alone doesn‘t tell the full story, especially for imbalanced datasets
- Precision and recall provide additional insight into Type I and Type II errors
- F1 score is a way to combine precision and recall into a single balanced metric
- Based on the confusion matrix, you can determine next steps for improving your model