Confusion Matrix Evaluation
Confusion Matrix Evaluation
Understanding the Confusion Matrix
What is a Confusion Matrix?
A confusion matrix is a table used to evaluate the performance of a classification model.
It compares actual values with predicted values, providing insight into model performance.
Structure of a Confusion Matrix
For a binary classification problem, the confusion matrix is represented as a 2x2 table with the following components:
Predicted Positive
Predicted Negative
Actual Positive
True Positive (TP)
False Negative (FN)
Actual Negative
False Positive (FP)
True Negative (TN)
Example 1: Spam Email Classification
Scenario: An email classifier to detect spam emails.
Predicted Spam:
TP = 90
FN = 10
Predicted Not Spam:
FP = 5
TN = 95
Definitions:
True Positives (TP): Model correctly predicts the positive class.
True Negatives (TN): Model correctly predicts the negative class.
False Positives (FP): Model incorrectly predicts positive when it is actually negative (Type I Error).
False Negatives (FN): Model incorrectly predicts negative when it is actually positive (Type II Error).
Evaluation Metrics from Confusion Matrix
Example 2: Medical Diagnosis for a Disease
Scenario: A model predicting whether a person is diseased.
Predicted Diseased:
TP = 50
FN = 5
Predicted Healthy:
FP = 10
TN = 100
Performance Metrics Derived from Confusion Matrix
Key Metrics:
Accuracy:
Calculated as (TP + TN) / (TP + TN + FP + FN)
Precision (Positive Predictive Value):
Calculated as TP / (TP + FP)
Indicates the proportion of positive identifications that were actually correct.
Recall (Sensitivity):
Calculated as TP / (TP + FN)
Measures how many actual positives were correctly predicted.
F1-Score:
Calculated as 2 × (Precision × Recall) / (Precision + Recall)
Balances precision and recall, useful for uneven class distribution.
Precision Interpretation:
Answers: "Out of all the predicted positives, how many were actually positive?"
Recall Interpretation:
Answers: "Out of all the actual positives, how many did we correctly predict?"
Practical Applications
Example: Fraudulent Credit Card Transactions
Predicted Fraud (Positive):
TP = 40
FN = 10
Predicted Not Fraud (Negative):
FP = 5
TN = 100
Calculations:
Precision (Positive Predictive Value):
Precision = TP / (TP + FP) = 40 / (40 + 5) = 40 / 45 = 0.89
Recall (Sensitivity or True Positive Rate):
Recall = TP / (TP + FN) = 40 / (40 + 10) = 40 / 50 = 0.80
When to Focus on Precision vs. Recall
Scenarios:
Prioritize Precision:
Spam Detection: Minimize important emails flagged as spam.
Medical Diagnosis: Avoid unnecessary stress due to misdiagnosis.
Fraud Detection: Prevent falsely accusing legitimate customers of fraud.
Prioritize Recall:
Spam Detection: Catch all spam, accepting some important emails may be flagged.
Medical Diagnosis: Crucial if missing a disease may be life-threatening (e.g., cancer detection).
Fraud Detection: Ensure no fraud cases are missed, which may lead to financial losses.
Conclusion
High Precision:
Interpretation: 89% of flagged transactions were truly fraudulent.
Important when False Positives are costly.
High Recall:
Interpretation: Detected 80% of actual fraud cases.
Important when False Negatives are costly.