Precision Recall and FScore.

数学定义

假设我们手上有60个正样本,40个负样本

我们要找出所有的正样本,系统查找出50个,其中只有40个是真正的正样本,计算上述各指标。

1
2
3
4
样本:      60正          40负
识别: 50正 50负
/ \ / \
实际上: 40正 10负 20正 30负
  • TP: 将正类预测为正类数 40
  • FN: 将正类预测为负类数 20
  • FP: 将负类预测为正类数 10
  • TN: 将负类预测为负类数 30

精确率(Accuracy):预测对的/所有 = (TP+TN)/(TP+FN+FP+TN) = 70%

准确率(Precision):TP/(TP+FP) = 80%

召回率(Recall):TP/(TP+FN) = 2/3 = 66%

F1 Score:(最大值为1 最小值为0 值越大效果越好)

参考链接:精确率和召回率 F1分数

sklearn.metrics.f1_score()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> from sklearn.metrics import f1_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> f1_score(y_true, y_pred, average='macro')
0.26...
>>> f1_score(y_true, y_pred, average='micro')
0.33...
>>> f1_score(y_true, y_pred, average='weighted')
0.26...
>>> f1_score(y_true, y_pred, average=None)
array([0.8, 0. , 0. ])
>>> y_true = [0, 0, 0, 0, 0, 0]
>>> y_pred = [0, 0, 0, 0, 0, 0]
>>> f1_score(y_true, y_pred, zero_division=1)
1.0...
0 1 2 Sum Tips
TP 2 0 0 2 x识别为x
FP 1 2 1 4 非x识别为x
FN 0 2 2 4 x识别为非x
TN 3 2 3 8 非x识别为非x
1
2
3
4
5
6
7
8
9
10
11
12
'mincro': 使用总和计算
Precision = TP / (TP + FP) = 2/6 = 1/3
Recall = TP / (TP + FN) = 2/6 = 1/3
F1Score = 2PR / (P + R) = 1/3

'macro': 对每一类别的f1_score进行简单算术平均
F1_score(0) = 0.8 / F1_score(1) = 0 / F1_score(2) = 0
F1_score('macro') = (F1_score(0) + F1_score(1) + F1_score(2)) / 3

'weighted': 对每一类别的f1_score进行加权平均,权重为各类别数在y_true中所占比例
r = ratio(0) = ratio(1) = ratio(1) = 1/3
F1_score('weighted') = r * F1_score(0) + r * F1_score(1) + r * F1_score(2) = 0.26

Reference:sklearn.metrics.f1_score