二分类混淆矩阵解释
二分类混淆矩阵是用来评估分类模型性能的工具,展示了模型预测结果和真实值之间的关系。它是一个 2x2 的矩阵,包含以下四个指标:
实际为正类 (Positive) | 实际为负类 (Negative) | |
---|---|---|
预测为正类 | TP (True Positive) | FP (False Positive) |
预测为负类 | FN (False Negative) | TN (True Negative) |
- TP (真阳性):模型预测为正类,且实际也是正类的数量。
- FP (假阳性):模型预测为正类,但实际是负类的数量。(错误警报)
- FN (假阴性):模型预测为负类,但实际是正类的数量。(漏检)
- TN (真阴性):模型预测为负类,且实际是负类的数量。
常用性能指标
-
准确率 (Accuracy):
Accuracy = T P + T N T P + T N + F P + F N \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} Accuracy=TP+TN+FP+FNTP+TN
表示模型预测正确的比例。 -
精确率 (Precision):
Precision = T P T P + F P \text{Precision} = \frac{TP}{TP + FP} Precision=TP+FPTP
表示在所有被预测为正类的样本中,实际为正类的比例。 -
召回率 (Recall):
Recall = T P T P + F N \text{Recall} = \frac{TP}{TP + FN} Recall=TP+FNTP
表示在所有实际为正类的样本中,被模型正确识别为正类的比例。 -
F1 分数:
F 1 = 2 ⋅ Precision ⋅ Recall Precision + Recall F1 = \frac{2 \cdot \text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} F1=Precision+Recall2⋅Precision⋅Recall
综合衡量精确率和召回率的指标。
举例说明
假设开发了一个医疗诊断模型,用来检测某种疾病(正类为“患病”,负类为“未患病”)。测试数据中有100名患者,其中:
- 真实患病者有40人,未患病者有60人。
- 模型预测结果如下:
实际患病 (Positive) | 实际未患病 (Negative) | |
---|---|---|
预测为患病 | 35 (TP) | 10 (FP) |
预测为未患病 | 5 (FN) | 50 (TN) |
计算指标
-
准确率 (Accuracy):
Accuracy = T P + T N T P + T N + F P + F N = 35 + 50 100 = 85 % \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} = \frac{35 + 50}{100} = 85\% Accuracy=TP+TN+FP+FNTP+TN=10035+50=85% -
精确率 (Precision):
Precision = T P T P + F P = 35 35 + 10 = 77.8 % \text{Precision} = \frac{TP}{TP + FP} = \frac{35}{35 + 10} = 77.8\% Precision=TP+FPTP=35+1035=77.8% -
召回率 (Recall):
Recall = T P T P + F N = 35 35 + 5 = 87.5 % \text{Recall} = \frac{TP}{TP + FN} = \frac{35}{35 + 5} = 87.5\% Recall=TP+FNTP=35+535=87.5% -
F1 分数:
F 1 = 2 ⋅ Precision ⋅ Recall Precision + Recall = 2 ⋅ 0.778 ⋅ 0.875 0.778 + 0.875 ≈ 82.3 % F1 = \frac{2 \cdot \text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} = \frac{2 \cdot 0.778 \cdot 0.875}{0.778 + 0.875} \approx 82.3\% F1=Precision+Recall2⋅Precision⋅Recall=0.778+0.8752⋅0.778⋅0.875≈82.3%
结论
通过混淆矩阵和相关指标,可以更细致地分析模型的性能。如果该模型应用于疾病检测,可能需要重点优化召回率以减少漏检(假阴性)。