Simon Shi的小站

人工智能,机器学习, 强化学习,大模型,自动驾驶

0%

AI 算法模型评估

模型效果评估,召回率

准确率

召回率

指标 定义与描述 示例场景
TP(True Positive) 真实类别为正类,且被模型正确预测为正类的样本数量。 疾病检测中,真实患病且被模型诊断为阳性的患者数量‌17。
TN(True Negative) 真实类别为负类,且被模型正确预测为负类的样本数量。 垃圾邮件分类中,正常邮件被正确识别为非垃圾邮件的数量‌46。
FP(False Positive)* 真实类别为负类,但被模型错误预测为正类的样本数量。 安防系统中,将正常行为误判为异常事件的数量(误报)‌17。
FN(False Negative 真实类别为正类,但被模型错误预测为负类的样本数量。 医学影像分析中,漏诊癌症病例的数量(漏报)‌37。

核心指标公式

指标名称 公式定义 应用场景
‌**准确率(Accuracy)**‌ Accuracy=(TP+TN)/(TP+TN+FP+TN​) 评估整体预测正确率,适用于类别均衡的数据集‌45
‌**召回率(Recall)**‌ Recall=TP/(TP+FN) 关注正样本的覆盖能力(如疾病筛查、搜索召回)‌16
‌**精确率(Precision)**‌ Precision=TP/(TP+FP) 关注预测结果的准确性(如垃圾邮件过滤)‌16
‌**F1值(F1-Score)**‌ F1=(2×Precision×Recall​) / (Precision+Recall) 综合精确率与召回率,用于类别不均衡数据‌56
‌**IOU(交并比)**‌ IOU=交集面积​ ‌/ 并集面积 目标检测中评估预测框与真实框的重叠度,值越接近1越好‌7

confusion_matrix

plot_confusion_matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
import matplotlib.pyplot as plt

def plot_confusion_matrix(cm, classes, title='Confusion Matrix'):
plt.figure(figsize=(8,6))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title(title, fontsize=14)
plt.colorbar()

tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)

# 添加数值标签
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, format(cm[i, j], 'd'),
ha="center", va="center",
color="white" if cm[i, j] > cm.max()/2 else "black")

plt.ylabel('True Label', fontsize=12)
plt.xlabel('Predicted Label', fontsize=12)
plt.tight_layout()

# 示例数据
true_labels = [0,1,0,1,1,0,0,1]
pred_labels = [0,1,0,0,1,0,1,1]
classes = ['Negative', 'Positive']

# 生成混淆矩阵
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(true_labels, pred_labels)

# 绘图
plot_confusion_matrix(cm, classes)
plt.show()

Seaborn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import seaborn as sns

def seaborn_confusion_matrix(cm, classes, title='Confusion Matrix'):
plt.figure(figsize=(8,6))
sns.heatmap(cm, annot=True, fmt='d', cmap='YlGnBu',
xticklabels=classes, yticklabels=classes)

plt.title(title, fontsize=14, pad=20)
plt.ylabel('True Label', fontsize=12)
plt.xlabel('Predicted Label', fontsize=12)
plt.xticks(rotation=45)
plt.yticks(rotation=0)

# 使用相同数据
seaborn_confusion_matrix(cm, classes)
plt.show()