← Back to Classic ML Mind Map
📊 Classic MLID: confusion-matrix

Confusion Matrix

混淆矩阵
🎯Core Definition
the fundamental counting table for binary classification, splitting samples into 4 cells by “true label × predicted label”: TP (predicted positive, actually positive), FP (predicted positive, actually negative — Type I error), FN (predicted negative, actually positive — Type II error), TN (predicted negative, actually negative), with total N=TP+FP+FN+TNN = TP + FP + FN + TN. Accuracy Acc=TP+TNNAcc = \frac{TP + TN}{N} is the only metric determined solely by the diagonal. Numerical example: 100 samples with 95 negatives and 5 positives; the model predicts everything as negative, giving TP=0,FP=0,FN=5,TN=95TP=0, FP=0, FN=5, TN=95 and Acc=95/100=95%Acc = 95/100 = 95\% — it looks excellent, yet not a single positive is found (recall = 0). This is the accuracy trap.
💡Use Cases
the starting point of every classification evaluation; all derived metrics (Precision/Recall/F1, specificity, ROC) are read off these 4 cells; for multiclass it generalizes to an N×NN \times N matrix (diagonal = correct predictions), from which macro/micro/weighted averaging is derived.
⚡Key Problems Solved
accuracy is kidnapped by the majority class on imbalanced data (a model that always predicts negative still scores 99% for a 1% prevalence disease), and it is blind to the asymmetric costs of FP vs FN; the confusion matrix exposes both error types and their scale. Macro averaging weights each class equally, Macro P=1C∑c=1CPcMacro\,P = \frac{1}{C}\sum_{c=1}^{C}P_c, so rare classes count as much as common ones; micro averaging pools all samples first, Micro P=∑cTPc∑c(TPc+FPc)Micro\,P = \frac{\sum_c TP_c}{\sum_c (TP_c + FP_c)}, and is dominated by the largest class — choose macro when classes are severely imbalanced and the rare class matters, micro when class sizes are balanced or only overall performance matters.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Confusion Matrix.
Use Cases
In production engineering, what are the primary use cases for Confusion Matrix and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Hand computation: given TP=40,FP=10,FN=20,TN=30TP=40, FP=10, FN=20, TN=30, compute accuracy, precision, recall and F1.
2
Why is a model that always predicts negative, at 99% accuracy for a 1% prevalence disease, useless? Which cell gets sacrificed?
3
Difference between Type I (FP) and Type II (FN) errors; which cell is false-blocking vs missing spam?
4
How macro vs micro averaging are computed and differ; why choose macro when classes are severely imbalanced (e.g. 1000:1)?
5
How to represent and read a multiclass confusion matrix (diagonal, row-normalized, weighted average)?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Confusion Matrix"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardLinear-Chain CRFNext Card →Precision, Recall & F1

🔗 More Classic ML Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/ML/card/confusion-matrix
中文速览与面试要点

混淆矩阵 (Confusion Matrix)

定义: 二分类评估的基础计数表, 按“真实类别 × 预测类别”把样本分成 4 格: TP (真阳性, 预测为正且实际为正)、FP (假阳性, 预测为正但实际为负, 第 I 类错误)、FN (假阴性, 预测为负但实际为正, 第 II 类错误)、TN (真阴性, 预测为负且实际为负), 总样本 N = TP + FP + FN + TN。准确率 Acc = TP + TNN 是唯一只由对角线决定的指标。数值例: 100 个样本里 95 个负类、5 个正类, 模型全部预测为负, 则 TP=0, FP=0, FN=5, TN=95, Acc = 95/100 = 95\%——看起来优秀, 但正类一个都没找到 (召回率 0), 这就是“准确率陷阱”。 使用场景: 任何分类任务的评估起点; 由这 4 格衍生出 Precision/Recall/F1、特异度、ROC 曲线等全部混淆矩阵类指标; 多分类时扩展为 N N 矩阵 (对角线为正确分类), 并派生宏平均/微平均/加权平均。 解决的问题: 准确率在不平衡数据上被多数类“绑架” (1% 患病率时全判阴性也有 99% 准确率), 且对 FP/FN 的代价差异视而不见; 混淆矩阵显式暴露两类错误及各自规模。宏平均 (macro) 对每类指标等权平均, Macro\,P = 1C _c=1^CP_c, 小类与大类权重相同; 微平均 (micro) 把所有样本汇总后一次计算, Micro\,P = _c TP_c _c (TP_c + FP_c), 被样本多的类别主导——类别极不平衡且小类重要时选宏平均, 各类样本量均衡或只关心总体表现时选微平均。

核心要点

  • 手算: 给定 TP=40, FP=10, FN=20, TN=30, 求准确率、Precision、Recall、F1?
  • 为什么 1% 患病率的筛查模型“全预测为阴性”时准确率 99% 却不可用? 哪个格子被牺牲了?
  • Type I (FP) 与 Type II (FN) 错误的区别; 垃圾邮件被误拦与漏放分别对应哪个格子?
  • 宏平均与微平均如何计算、有何区别; 类别极不平衡 (如 1000:1) 时为何选宏平均?
  • 多分类混淆矩阵如何表示与解读 (对角线 / 每行归一 / 加权平均)?