Skip to content
Share
Explore

What is a Confusion Matrix?

What is a Confusion Matrix?

A Confusion Matrix is a table used to evaluate the performance of a classification model by comparing the model’s predicted values with the actual values.
It helps answer three key questions:
How many predictions were correct?
Where did the model make mistakes?
What types of errors did the model make (in its predictions)?

Structure of a Confusion Matrix

A confusion matrix is typically structured as follows:
Table 1
Actual \ Predicted
Predicted: Class 1
Predicted: Class 2
Predicted: Class 3
...
Actual: Class 1
True Positives (TP)
False Negatives (FN)
False Negatives (FN)
...
Actual: Class 2
False Positives (FP)
True Positives (TP)
False Negatives (FN)
...
Actual: Class 3
False Positives (FP)
False Positives (FP)
True Positives (TP)
...
There are no rows in this table

Key Terms in the Confusion Matrix

True Positives (TP) → Correctly predicted class instances.
True Negatives (TN) → Correctly predicted non-class instances.
False Positives (FP) (Type I Error) → Model incorrectly predicted a class when it shouldn’t have.
False Negatives (FN) (Type II Error) → Model missed predicting a class when it should have.

Example: Confusion Matrix for a Chatbot Classifier

Let's say we have a chatbot trained to classify user inputs into 4 categories:
Greeting
Goodbye
Order Issue
Support Request
We test the chatbot on 20 messages, and it makes the following predictions:
Table 2
Actual \ Predicted
Greeting
Goodbye
Order Issue
Support Request
Greeting
5 (TP)
0 (FN)
1 (FN)
0 (FN)
Goodbye
0 (FN)
4 (TP)
1 (FN)
0 (FN)
Order Issue
1 (FP)
0 (FN)
6 (TP)
1 (FN)
Support Request
0 (FP)
1 (FP)
0 (FP)
6 (TP)
There are no rows in this table

Performance Metrics Derived from a Confusion Matrix

Using the confusion matrix, we can calculate important model performance metrics:
AccuracyOverall correctness of the model
Accuracy= TP+TNTotalPredictions\text{Accuracy} = \frac{TP + TN}{Total Predictions}Accuracy=TotalPredictionsTP+TN​
Precision → How many predicted positives were actually correct?
Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}Precision=TP+FPTP​
Recall (Sensitivity) → How many actual positives were correctly predicted?
Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN}Recall=TP+FNTP​
F1 Score → A balance between Precision and Recall
F1=2×Precision×RecallPrecision+RecallF1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}F1=2×Precision+RecallPrecision×Recall​

Why is a Confusion Matrix Useful?

Helps understand classification errors (e.g., is the model confusing Order Issues with Support Requests?). ✅ Reveals imbalances (e.g., does the chatbot handle greetings better than order issues?). ✅ Goes beyond accuracy (e.g., high accuracy can still mean poor performance for certain classes of inputs from the user).
💡 Real-World Example: If a fraud detection system falsely predicts legitimate transactions as fraud (False Positive), it could lead to customer complaints.
A Confusion Matrix helps detect and minimize such issues.

How to Generate a Confusion Matrix in R

Remember: There are 2 runtime contexts for working with your model:
A: Training time
B: Run time
library(caret)

# Assuming `predictions` is the model's predicted class labels
# And `actual_values` is the correct class labels from test data

confusionMatrix(as.factor(predictions), as.factor(actual_values))

This will output:
markdown
CopyEdit
Confusion Matrix and Statistics

Reference
Prediction Greeting Goodbye OrderIssue SupportRequest
Greeting 5 0 1 0
Goodbye 0 4 1 0
OrderIssue 1 0 6 1
SupportRequest 0 1 0 6

Overall Accuracy: 85%

Conclusion

A Confusion Matrix is an essential tool for analyzing classification model performance, especially for AI chatbots, fraud detection, medical diagnosis, and customer service automation. 🚀
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.