J4EasyPart J · RecSys & Search Metrics
Confusion Matrix, Precision, Recall & Macro/Micro F1
Compute binary confusion matrix counts alongside Precision, Recall, and Macro/Micro F1 scores.
⏱️ Time:
O(N)💾 Space:
O(1)🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
def compute_classification_metrics(y_true: np.ndarray, y_pred: np.ndarray) -> dict:
"""手写二分类混淆矩阵与评估指标"""
tp =
fp = int(np.sum((y_true == 0) & (y_pred == 1)))
fn = int(np.sum((y_true == 1) & (y_pred == 0)))
tn = int(np.sum((y_true == 0) & (y_pred == 0)))
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
f1 =
return {
"tp": tp, "fp": fp, "fn": fn, "tn": tn,
"precision": float(precision),
"recall": float(recall),
"f1": float(f1)
}
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: Should you prioritize Precision or Recall in spam filtering vs. cancer screening?
💡
Core Mental Anchor / Mnemonic
Precision checks predicted positives; Recall checks true positives; F1 is their harmonic mean.
📐 Mathematical Derivation & Core Formula
### Precision-Recall Harmonic Mean & Macro/Micro Aggregation
From TP, FP, FN, TN: , , and (harmonic mean strongly penalizes imbalance toward zero).
For multi-class tasks, Macro-F1 averages per-class F1 equally (highlighting minority classes), while Micro-F1 sums global counts across classes (weighted by class frequency).
From TP, FP, FN, TN: , , and (harmonic mean strongly penalizes imbalance toward zero).
For multi-class tasks, Macro-F1 averages per-class F1 equally (highlighting minority classes), while Micro-F1 sums global counts across classes (weighted by class frequency).
🔄 Tensor Dimensions & Shape Flow
y_true, y_pred (N,) -> count TP, FP, FN, TN -> closed-form Precision, Recall, F1
🛡️ Numerical Stability & Pitfalls Checklist
- Guard zero denominators (
TP+FP == 0orP+R == 0) by safely returning0.0. - Prioritize Recall when missed positives are costly (fraud/medical), Precision when false alarms hurt UX.
💻 Reference Code Implementation
import numpy as np
def compute_classification_metrics(y_true: np.ndarray, y_pred: np.ndarray) -> dict:
"""手写二分类混淆矩阵与评估指标"""
tp = int(np.sum((y_true == 1) & (y_pred == 1)))
fp = int(np.sum((y_true == 0) & (y_pred == 1)))
fn = int(np.sum((y_true == 1) & (y_pred == 0)))
tn = int(np.sum((y_true == 0) & (y_pred == 0)))
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0.0
return {
"tp": tp, "fp": fp, "fn": fn, "tn": tn,
"precision": float(precision),
"recall": float(recall),
"f1": float(f1)
}
🧪 Runnable Assertions & Validation
Copy and run directly in Python / Jupyter:
import numpy as np
yt = np.array([1, 1, 0, 0])
yp = np.array([1, 0, 1, 0])
m = compute_classification_metrics(yt, yp)
assert m["tp"] == 1 and m["fp"] == 1 and m["fn"] == 1 and m["tn"] == 1
assert np.isclose(m["precision"], 0.5)
assert np.isclose(m["recall"], 0.5)
assert np.isclose(m["f1"], 0.5)
print("✓ 混淆矩阵与 F1 综合指标自测通过")🎯 Core Follow-up Inquiries & Reference Answers
Q1:Should you prioritize Precision or Recall in spam filtering vs. cancer screening?
Spam filtering prioritizes Precision to avoid sending legitimate emails to spam; cancer screening prioritizes Recall to avoid missing true positive cases that can undergo follow-up tests.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.