评价指标总结
本篇博文主要总结下机器学习,深度学习,自然语言处理里面的一些的评价指标及其背后的原理。机器学习分类问题精确率 (Precision)TPTP+FPTPTP+FP\frac{TP}{TP+FP}可以这样理解准确率:分母是我们这边所有预测为真的数量,包括正确预测为真的和错误预测为真的。召回率 (recall)TPTP+FNTPTP+FN\frac{TP}{TP+...
本篇博文主要总结下机器学习,深度学习,自然语言处理里面的一些的评价指标及其背后的原理。
机器学习
分类问题
记 P o s i t i v e Positive Positive 为正, N e g a t i v e Negative Negative 为负
精确率 (Precision)
T
P
T
P
+
F
P
\frac{TP}{TP+FP}
TP+FPTP
可以这样理解准确率:分母是我们这边所有预测为真的数量,包括正确预测为真的和错误预测为真的。
召回率 (recall)
T
P
T
P
+
F
N
\frac{TP}{TP+FN}
TP+FNTP
分母为
t
r
u
e
_
d
a
t
a
true\_data
true_data 那边所有为真的样本数量,包括我们预测为真的和我们错误的预测为负的。
故:精确率和召回率的区别在于分母,是我们所有预测为真的数量,还是真实为真的样本数量。
PRC
就是精确率,召回率图像
在机器学习中,通常我们可以设置阈值(threshhold,分类模型判别正负样本的阈值)来提高精确率或召回率。通过PRC能更好的调节阈值。
准确率(accuarcy)
T
P
+
T
N
A
L
L
\frac{TP+TN}{ALL}
ALLTP+TN
注:在样本类别不平衡时,一般不用准确率作为评价标准。
混淆矩阵
F1值
当精确率和召回率都很高时,F1值就会变的很高,但是在通常情况下,两者不会同时变得很高,当精确率变高了,则召回率就会降低,反之亦然,这就涉及到一个 p r e c i s i o n / r e c a l l t r a d e o f f precision/recall\ tradeoff precision/recall tradeoff。
ROC-AUC
首先了解两个概念
-
TPR:True Positive Rate(真正率)
T P T P + F N \frac{TP}{TP+FN} TP+FNTP -
FPR: False Postive Rate(假正率)
F P F P + T F \frac{FP}{FP+TF} FP+TFFP
我们通过调节和尝试分类模型不同的 t h r e s h o l d _ v a l u e threshold\_value threshold_value, 可以得到不同的 T P R , F P R TPR, FPR TPR,FPR。
其 R O C ROC ROC 曲线下方的面积构成的 A U C AUC AUC。通常越好的模型,其 A U C AUC AUC 越大。
from sklearn import metrics
def aucfun(act,pred):
fpr, tpr, thresholds = metrics.roc_curve(act, pred, pos_label=1)
return metrics.auc(fpr, tpr)
ROC曲线缺点:
- R O C ROC ROC 曲线的优点是不会随着类别分布的改变而改变,但这在某种程度上也是其缺点。因为负例 N N N 增加了很多,而曲线却没变,这等于产生了大量 F P FP FP 。像信息检索中如果主要关心正例的预测准确性的话,这就不可接受了。
- 在类别不平衡的背景下,负例的数目众多致使 F P R FPR FPR 的增长不明显,导致 R O C ROC ROC 曲线呈现一个过分乐观的效果估计。 R O C ROC ROC 曲线的横轴采用 F P R FPR FPR ,根据 F P R = F P F P + T N FPR=\frac{FP}{FP+TN} FPR=FP+TNFP,当负例 N N N 的数量远超正例 P P P 时, F P FP FP 的大幅增长只能换来 F P R FPR FPR 的微小改变。结果是虽然大量负例被错判成正例,在 R O C ROC ROC 曲线上却无法直观地看出来。当然也可以只分析 R O C ROC ROC 曲线左边一小段, 而在 P R PR PR 曲线上将会是一个大幅下降。
ROC与PR曲线比较:
- R O C ROC ROC 曲线由于兼顾正例与负例,所以适用于评估分类器的整体性能,相比而言** P R PR PR 曲线完全聚焦于正例。**
- 如果有多份数据且存在不同的类别分布,比如信用卡欺诈问题中每个月正例和负例的比例可能都不相同,这时候如果只想单纯地比较分类器的性能且剔除类别分布改变的影响,则 R O C ROC ROC 曲线比较适合,因为类别分布改变可能使得 P R PR PR 曲线发生变化时好时坏,这种时候难以进行模型比较;反之,如果想测试不同类别分布下对分类器的性能的影响,则PR曲线比较适合。
- 如果想要评估在相同的类别分布下正例的预测情况,则宜选PR曲线。
- 类别不平衡问题中, R O C ROC ROC 曲线通常会给出一个乐观的效果估计,所以大部分时候还是 P R PR PR 曲线更好。
回归
RMSE(平方根误差)
MSE就是除去上面的更号。
MAE(平均绝对误差)
由上面的公式可知:
- RMSE类似于欧式距离和 l 2 l_2 l2 范数。
- MAE类似于曼哈顿距离和 l 1 l_1 l1 范数。
- 由此,我们可推得
显然,k越大,其越侧重于 m a x ( v 0 , v 1 , . . . , ) max(v_0,v_1,...,) max(v0,v1,...,),越忽略较小值,故相对于MAE,RMSE 对离群点更加敏感。对于有许多离群点的样本,一般采用MAE 的评价标准。
以上部分内容参考自 h a n d s o n m a c h i n e l e a r n i n g s k l e a r n a n d t e n s o r f l o w hands\ on\ machine\ learning\ sklearn\ and\ tensorflow hands on machine learning sklearn and tensorflow。
自然语言处理
BLEU指标
在机器翻译领域,神经网络翻译出的句子,我们需要对他进行一个定量的评价,也就是给其打分,以判断模型生成的句子好坏,下面举个例子来说明。
N M T o u t p u t : t h e t h e t h e t h e t h e t h e t h e NMT\ output:the\ the\ the\ the\ the\ the\ the NMT output:the the the the the the the
人类翻译的结果:
- r e f e r e n c e 1 : T h e c a t i s o n t h e m a t reference1:The\ cat\ is\ on\ the\ mat reference1:The cat is on the mat
- r e f e r e n c e 2 : T h e r e i s a c a t o n t h e m a t reference2:There\ is\ a\ cat\ on\ the\ mat reference2:There is a cat on the mat
如果以 u n i g r a m unigram unigram 来计算 N M T o u t p u t NMT\ output NMT output 的精确度 p 1 p_1 p1,则 N M T o u t p u t NMT\ output NMT output 中的7个词全出现在 r e f e r e n c e reference reference 中,那 p 1 = 7 / 7 = 1 p_1 = 7/7 =1 p1=7/7=1。很完美?等等,貌似哪里不对。
很明显,这个 N M T o u t p u t NMT\ output NMT output 很不好,我们如果按照上面简单的计算精确度,就会导致生成的句子中会出现大量重复的 t o k e n token token 。由此产生了改良的版本,我们计算 N M T o u t p u t NMT\ output NMT output 中每个不同的 t o k e n token token 在 r e f e r e n c e reference reference 中出现的次数,例如 t h e the the 在 r e f e r e n c e 1 reference1 reference1 中出现2次,在 r e f e r e n c e 2 reference2 reference2 中出现1次,那么我们认为 t h e the the 这个 t o k e n token token 在 r e f e r e n c e reference reference 中出现次数为2(选取最高的那个),称作为截断次数。那么 N M T o u t p u t NMT\ output NMT output 中 p 1 = 2 / 7 p_1 = 2/7 p1=2/7。
假设这次的 N M T o u t p u t NMT\ output NMT output 为 T h e c a t t h e c a t o n t h e m a t The\ cat\ the\ cat\ on\ the\ mat The cat the cat on the mat,我们来计算下 b i g r a m bigram bigram 的次数和截断次数。
bigram_token | count | clip_count |
---|---|---|
the cat | 2 | 1 |
cat the | 1 | 0 |
cat on | 1 | 1 |
on the | 1 | 1 |
the mat | 1 | 1 |
上面表格中的 c o u n t count count 表示该 b i g r a m t o k e n bigram\ token bigram token 在 N M T o u t p u t NMT\ output NMT output 中出现的次数, c l i p c o u n t clip\ count clip count 表示该 b i g r a m t o k e n bigram\ token bigram token 在 r e f e r e n c e reference reference 中的截断次数。
故其 p 1 = c l i p _ c o u n t / c o u n t p_1 = clip\_count/ count p1=clip_count/count
公式化的结果:
p
n
=
∑
n
g
r
a
m
∈
y
^
c
o
u
n
t
c
l
i
p
(
n
g
r
a
m
)
∑
n
g
r
a
m
∈
y
^
c
o
u
n
t
(
n
g
r
a
m
)
p_n = \frac{\sum_{ngram\in \hat{y}} count_{clip}(ngram)}{\sum_{ngram\in \hat{y}} count(ngram)}
pn=∑ngram∈y^count(ngram)∑ngram∈y^countclip(ngram)
其中
y
^
\hat{y}
y^ 即为上面的
N
M
T
o
u
t
p
u
t
NMT\ output
NMT output。
但是上面的评价还是有一个缺点,当我们的
N
M
T
o
u
t
p
u
t
NMT\ output
NMT output 很简短时,
p
n
p_n
pn 可能变得很大,显然这是不合理的,于是我们加上一个
B
P
BP
BP:
B
P
=
{
1
i
f
N
M
T
_
o
u
t
p
u
t
_
l
e
n
>
r
e
f
e
r
e
n
_
l
e
n
e
x
p
(
1
−
N
M
T
_
o
u
t
p
u
t
_
l
e
n
/
r
e
f
e
r
e
n
c
e
_
l
e
n
)
o
t
h
e
r
w
i
s
e
BP= \left\{\begin{matrix} 1 & if\ NMT\_output\_len > referen\_len \\ exp(1-NMT\_output\_len/reference\_len) & otherwise \end{matrix}\right.
BP={1exp(1−NMT_output_len/reference_len)if NMT_output_len>referen_lenotherwise
综上所述:
B
l
e
u
=
B
P
∗
e
x
p
(
∑
n
=
1
N
∗
(
1
/
n
)
∗
l
o
g
(
p
n
)
)
Bleu = BP*exp\left ( \sum_{n=1}^{N}*(1/n)*log(p_n)\right )
Bleu=BP∗exp(n=1∑N∗(1/n)∗log(pn))
这样我们就能计算 B l e u 1 、 B l e u 2 、 B l e u 3.... Bleu1、Bleu2、Bleu3.... Bleu1、Bleu2、Bleu3....
Rouge指标
Rouge-N
分母表示所有 r e f e r e n c e reference reference 中所有出现的 n g r a m ngram ngram 个数。分子表示所有 r e f e r e n c e reference reference 与 N M T o u p u t NMT\ ouput NMT ouput 共现 n g r a m ngram ngram 个数。嗯这是基于召回率的方式计算。就这么简单。
Rouge-L
X X X 是参考译文, Y Y Y 是待评测译文, m , n m,n m,n 表示 X , Y X, Y X,Y 序列长度。 L C S ( X , Y ) LCS(X,Y) LCS(X,Y) 表示最长公共子序列长度。
更多推荐
所有评论(0)