J4EasyPart J · RecSys & Search Metrics

Confusion Matrix, Precision, Recall & Macro/Micro F1

⚡Flashcard Drill➔

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?
Next: Cosine Similarity & Vector Top-K Retrieval →
💡

Core Mental Anchor / Mnemonic

Precision checks predicted positives; Recall checks true positives; F1 is their harmonic mean.

📐 Mathematical Derivation & Core Formula

Precision=TPTP+FP,Recall=TPTP+FN,F1=2⋅P⋅RP+R\mathrm{Precision} = \frac{\mathrm{TP}}{\mathrm{TP} + \mathrm{FP}}, \quad \mathrm{Recall} = \frac{\mathrm{TP}}{\mathrm{TP} + \mathrm{FN}}, \quad \mathrm{F1} = 2 \cdot \frac{P \cdot R}{P + R}
### Precision-Recall Harmonic Mean & Macro/Micro Aggregation
From TP, FP, FN, TN: Precision=TPTP+FP\mathrm{Precision} = \frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}, Recall=TPTP+FN\mathrm{Recall} = \frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}, and F1=2PRP+R\mathrm{F1} = \frac{2 P R}{P + R} (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 == 0 or P+R == 0) by safely returning 0.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.
🎯 Drill This Card Now➔
← Prev: NDCG@K (Normalized Discounted Cumulative Gain)Back to Part J & All 105Next: Cosine Similarity & Vector Top-K Retrieval →