先看以下示例,区分micro和macro的区别,这里直接调用sklearn封装好的接口,如果没有安装sklearn可以使用以下命令进行安装:
pip install scikit-learn
如果直接使用
pip install sklearn
是没有作用的,将会安装一个未知的第三方库。
先看一段简单的代码示例:
from sklearn.metrics import precision_score, recall_score, f1_score
a = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3] #真实
b = [1, 1, 2, 1, 2, 2, 2, 2, 3, 1, 3, 3] #预测
print(precision_score(a, b, average='micro'), precision_score(a, b, average='macro'))
print(recall_score(a, b, average='micro'), recall_score(a, b, average='macro'))
print(f1_score(a, b, average='micro'), f1_score(a, b, average='macro'))
'''
0.6666666666666666 0.7000000000000001
0.6666666666666666 0.6722222222222222
0.6666666666666666 0.6626984126984127
'''
我们可以构建混淆矩阵进行说明,混淆矩阵的四个基本概念:
所以可以对上述实例构建混淆矩阵
1类 | 2类 | 3类 | 总数 | |
---|---|---|---|---|
TP | 2 | 3 | 3 | 8 |
FP | 2 | 2 | 0 | 4 |
FN | 1 | 1 | 2 | 4 |
TN | 7 | 6 | 7 | 20 |
precision、recall和F1计算公式如下所示
p r e c i s i o n = T P T P + F P precision=\frac{TP}{TP+FP} precision=TP+FPTP
r e a l l = T P T P + F N reall=\frac{TP}{TP+FN} reall=TP+FNTP
F 1 = 2 ∗ p r e c i s i o n ∗ r e c a l l p r e c i s i o n + r e c a l l F1=\frac{2*precision*recall}{precision+recall} F1=precision+recall2∗precision∗recall
micro:利用总数的TP、FP和FN进行计算
m i c r o − p r e c i s i o n = 8 8 + 4 = 0.66666 micro-precision=\frac{8}{8+4}=0.66666 micro−precision=8+48=0.66666
m i c r o − r e c a l l = 8 8 + 4 = 0.66666 micro-recall=\frac{8}{8+4}=0.66666 micro−recall=8+48=0.66666
m i c r o − F 1 = 2 ∗ 0.66 ∗ 0.66 0.66 + 0.66 = 0.666 micro-F1=\frac{2*0.66*0.66}{0.66+0.66}=0.666 micro−F1=0.66+0.662∗0.66∗0.66=0.666
macro:先计算各个类别的TP、FP和FN,在进行汇总计算
m a c r o − p r e c i s i o n = 1 n ∑ i = 1 n p r e c i s i o n i = 1 3 ( 2 2 + 2 + 3 3 + 2 + 3 3 + 0 ) = 7 10 macro-precision=\frac{1}{n} \sum_{i=1}^n precision_i=\frac{1}{3}(\frac{2}{2+2}+\frac{3}{3+2}+\frac{3}{3+0})=\frac{7}{10} macro−precision=n1i=1∑nprecisioni=31(2+22+3+23+3+03)=107
m a c r o − r e c a l l = 1 n ∑ i = 1 n r e c a l l i = 1 3 ( 2 2 + 1 + 3 3 + 1 + 3 3 + 2 ) = 121 180 = 0.67222 macro-recall=\frac{1}{n} \sum_{i=1}^n recall_i=\frac{1}{3}(\frac{2}{2+1}+\frac{3}{3+1}+\frac{3}{3+2})=\frac{121}{180}=0.67222 macro−recall=n1i=1∑nrecalli=31(2+12+3+13+3+23)=180121=0.67222
m a c r o − F 1 = 1 n ∑ i = 1 n F 1 i = 0.66269 macro-F1=\frac{1}{n} \sum_{i=1}^n F1_i=0.66269 macro−F1=n1i=1∑nF1i=0.66269
通过上述示例就可以清楚地知道micro和macro的区别了。