Confusion Matrix, Precision, Recall, and F1 Score in Python Explained
A Confusion Matrix categorizes classification predictions into True Positives, True Negatives, False Positives, and False Negatives, forming the mathematical basis for Precision, Recall, and F1 Score evaluation.
A Confusion Matrix is a cross-tabulated contingency table that evaluates the performance of a classification model by categorizing its predictions into four distinct quadrants: True Positives (), True Negatives (), False Positives (), and False Negatives (). Core evaluation metrics including Accuracy, Precision, Recall, and the Score are derived directly from these four counts.
Imagine an airport baggage screening scanner. Every scanned bag has exactly four possible diagnostic outcomes: correctly flagging a bag with prohibited items (), correctly passing a safe bag (), raising a false alarm on a harmless suitcase (), or mistakenly allowing a dangerous bag through (). Evaluating a classifier requires understanding the specific frequency and real-world cost of each mistake.
1. The Accuracy Paradox (Why Accuracy Fails on Imbalanced Data)
In fraud detection or rare disease diagnosis where only (1 in 1,000) of samples are positive, a trivial model that always predicts negative achieves Accuracy while catching zero fraud cases (). Relying on accuracy alone conceals severe model failure.
2. Classification Metric Taxonomy Matrix
| Metric | Mathematical Formula | Core Question Answered | Optimal Application |
|---|---|---|---|
| Accuracy | What overall fraction of predictions was correct? | Balanced datasets where false alarms and misses have equal cost. | |
| Precision (PPV) | When the model flags a positive, how often is it right? | Spam filters, search engines, automated customer loan approvals. | |
| Recall (Sensitivity) | What fraction of all actual positive cases was caught? | Cancer screening, credit card fraud, disease outbreak tracking. | |
| Specificity (TNR) | What fraction of actual negative cases was identified? | Baseline clinical testing, establishing non-disease certainty. | |
| Score | Harmonic balance between Precision and Recall. | Imbalanced datasets where both precision and recall matter equally. |
3. The Mathematical Formulations
1. The 2x2 Binary Confusion Matrix
| Ground Truth / Prediction | Predicted Positive (1) | Predicted Negative (0) |
|---|---|---|
| Actual Positive (1) | True Positive (): Correct Catch | False Negative (): Missed Threat (Type II Error) |
| Actual Negative (0) | False Positive (): False Alarm (Type I Error) | True Negative (): Correct Rejection |
2. Why the Harmonic Mean for ?
The arithmetic average of and equals , falsely indicating acceptable performance. The harmonic mean gives:
The harmonic mean penalizes extreme imbalances harshly, ensuring that is high only when both Precision and Recall are strong.
3. The Generalized Score
When business requirements prioritize Recall over Precision (or vice versa), the Score assigns weight to Recall:
- Score (): Weights Recall twice as heavily as Precision (medical diagnosis, fault detection).
- Score (): Weights Precision twice as heavily as Recall (marketing outreach, auto-spam delete).
4. Worked Numerical Example (By Hand)
Suppose a diagnostic model tests patients ( actually have the disease, are healthy):
- Matrix Counts: .
- Accuracy: ( overall accuracy).
- Precision: ( of flagged patients are false alarms).
- Recall: ( of diseased patients were missed).
- Score: .
5. Multi-Class Averaging: Macro vs. Micro vs. Weighted
| Averaging Method | Calculation Mechanics | Best Used For |
|---|---|---|
| Macro Average | Unweighted arithmetic average of metric across all classes. | Highlighting performance on small, rare minority classes. |
| Micro Average | Aggregates total globally across all classes. | Measuring overall global volume correctness. |
| Weighted Average | Averages metrics weighted by the number of true instances in each class. | Accounting for class distribution support in imbalanced data. |
6. Code: Implementation From Scratch & Scikit-Learn
1. NumPy Matrix & Metric Calculations From Scratch
import numpy as np
# 10 diseased (1s), 90 healthy (0s)
y_true = np.array([1]*10 + [0]*90)
# Model catches 7 of 10 positives, makes 8 false alarms on negatives
y_pred = np.array([1]*7 + [0]*3 + [1]*8 + [0]*82)
def compute_confusion_matrix(y_true, y_pred):
tp = np.sum((y_true == 1) & (y_pred == 1))
tn = np.sum((y_true == 0) & (y_pred == 0))
fp = np.sum((y_true == 0) & (y_pred == 1))
fn = np.sum((y_true == 1) & (y_pred == 0))
return tp, tn, fp, fn
def evaluate_classification(y_true, y_pred, beta=1.0):
tp, tn, fp, fn = compute_confusion_matrix(y_true, y_pred)
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
specificity = tn / (tn + fp) if (tn + fp) > 0 else 0.0
# F-beta formula
f_beta = (1 + beta**2) * (precision * recall) / ((beta**2 * precision) + recall)
return {
"TP": tp, "TN": tn, "FP": fp, "FN": fn,
"Accuracy": round(accuracy, 3),
"Precision": round(precision, 3),
"Recall": round(recall, 3),
"Specificity": round(specificity, 3),
"F1": round(f_beta, 3)
}
results = evaluate_classification(y_true, y_pred)
for metric, val in results.items():
print(f"{metric}: {val}")2. Production Scikit-Learn Report
from sklearn.metrics import confusion_matrix, classification_report
cm = confusion_matrix(y_true, y_pred)
print("Scikit-Learn Confusion Matrix [[TN, FP], [FN, TP]]:\n", cm)
print("\nClassification Report:\n", classification_report(y_true, y_pred, digits=3))Summary
- The Confusion Matrix partitions classification outputs into True Positives, True Negatives, False Positives, and False Negatives.
- Accuracy is misleading on imbalanced datasets — always inspect Precision, Recall, and the Confusion Matrix.
- Precision measures positive prediction reliability, while Recall measures detection completeness.
- Score calculates the harmonic mean of Precision and Recall, while allows custom weighting based on business costs.
Common questions
Why is accuracy misleading for imbalanced datasets?
When one class dominates (e.g. 99% negative cases), a naive model that always predicts negative achieves 99% accuracy while failing to identify any positive samples. Precision, Recall, and F1 score expose this failure.
What is the difference between Precision and Recall?
Precision measures what proportion of positive predictions was actually correct (TP / (TP + FP)). Recall measures what proportion of all actual positive cases the model successfully detected (TP / (TP + FN)).
Why does the F1 Score use the harmonic mean instead of an arithmetic mean?
The harmonic mean heavily penalizes extreme disparities between Precision and Recall, ensuring the F1 score is high only when both metrics perform well simultaneously.
When should you use the F2 Score instead of F1?
Use the F2 score (beta=2) when false negatives are far more dangerous than false positives (such as disease diagnosis, fraud screening, and search & rescue operations) where Recall is prioritized.