本篇博文主要总结下机器学习,深度学习,自然语言处理里面的一些的评价指标及其背后的原理。
机器学习
分类问题
记 PositivePositivePositive 为正, NegativeNegativeNegative 为负
精确率 (Precision)
TPTP+FP\frac{TP}{TP+FP}TP+FPTP
可以这样理解准确率:分母是我们这边所有预测为真的数量,包括正确预测为真的和错误预测为真的。
召回率 (recall)
TPTP+FN\frac{TP}{TP+FN}TP+FNTP
分母为true_datatrue\_datatrue_data 那边所有为真的样本数量,包括我们预测为真的和我们错误的预测为负的。
故:精确率和召回率的区别在于分母,是我们所有预测为真的数量,还是真实为真的样本数量。
PRC
就是精确率,召回率图像

在机器学习中,通常我们可以设置阈值(threshhold,分类模型判别正负样本的阈值)来提高精确率或召回率。通过PRC能更好的调节阈值。
准确率(accuarcy)
TP+TNALL\frac{TP+TN}{ALL}ALLTP+TN
注:在样本类别不平衡时,一般不用准确率作为评价标准。
混淆矩阵

F1值

当精确率和召回率都很高时,F1值就会变的很高,但是在通常情况下,两者不会同时变得很高,当精确率变高了,则召回率就会降低,反之亦然,这就涉及到一个precision/recall tradeoffprecision/recall\ tradeoffprecision/recall tradeoff。
ROC-AUC
首先了解两个概念
-
TPR:True Positive Rate(真正率)
TPTP+FN\frac{TP}{TP+FN}TP+FNTP -
FPR: False Postive Rate(假正率)
FPFP+TF\frac{FP}{FP+TF}FP+TFFP
我们通过调节和尝试分类模型不同的 threshold_valuethreshold\_valuethreshold_value, 可以得到不同的TPR,FPRTPR, FPRTPR,FPR。

其ROCROCROC 曲线下方的面积构成的 AUCAUCAUC。通常越好的模型,其AUCAUCAUC 越大。
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曲线缺点:
- ROCROCROC 曲线的优点是不会随着类别分布的改变而改变,但这在某种程度上也是其缺点。因为负例NNN 增加了很多,而曲线却没变,这等于产生了大量FPFPFP 。像信息检索中如果主要关心正例的预测准确性的话,这就不可接受了。
- 在类别不平衡的背景下,负例的数目众多致使FPRFPRFPR 的增长不明显,导致ROCROCROC 曲线呈现一个过分乐观的效果估计。ROCROCROC 曲线的横轴采用FPRFPRFPR ,根据FPR=FPFP+TNFPR=\frac{FP}{FP+TN}FPR=FP+TNFP,当负例NNN 的数量远超正例PPP 时,FPFPFP 的大幅增长只能换来FPRFPRFPR 的微小改变。结果是虽然大量负例被错判成正例,在ROCROCROC 曲线上却无法直观地看出来。当然也可以只分析ROCROCROC 曲线左边一小段, 而在PRPRPR 曲线上将会是一个大幅下降。
ROC与PR曲线比较:
- ROCROCROC 曲线由于兼顾正例与负例,所以适用于评估分类器的整体性能,相比而言**PRPRPR 曲线完全聚焦于正例。**
- 如果有多份数据且存在不同的类别分布,比如信用卡欺诈问题中每个月正例和负例的比例可能都不相同,这时候如果只想单纯地比较分类器的性能且剔除类别分布改变的影响,则ROCROCROC 曲线比较适合,因为类别分布改变可能使得PRPRPR 曲线发生变化时好时坏,这种时候难以进行模型比较;反之,如果想测试不同类别分布下对分类器的性能的影响,则PR曲线比较适合。
- 如果想要评估在相同的类别分布下正例的预测情况,则宜选PR曲线。
- 类别不平衡问题中,ROCROCROC 曲线通常会给出一个乐观的效果估计,所以大部分时候还是PRPRPR 曲线更好。
回归
RMSE(平方根误差)

MSE就是除去上面的更号。
MAE(平均绝对误差)

由上面的公式可知:
- RMSE类似于欧式距离和 l2l_2l2 范数。
- MAE类似于曼哈顿距离和 l1l_1l1 范数。
- 由此,我们可推得

显然,k越大,其越侧重于max(v0,v1,...,)max(v_0,v_1,...,)max(v0,v1,...,),越忽略较小值,故相对于MAE,RMSE 对离群点更加敏感。对于有许多离群点的样本,一般采用MAE 的评价标准。
以上部分内容参考自hands on machine learning sklearn and tensorflowhands\ on\ machine\ learning\ sklearn\ and\ tensorflowhands on machine learning sklearn and tensorflow。
自然语言处理
BLEU指标
在机器翻译领域,神经网络翻译出的句子,我们需要对他进行一个定量的评价,也就是给其打分,以判断模型生成的句子好坏,下面举个例子来说明。
NMT output:the the the the the the theNMT\ output:the\ the\ the\ the\ the\ the\ theNMT output:the the the the the the the
人类翻译的结果:
- reference1:The cat is on the matreference1:The\ cat\ is\ on\ the\ matreference1:The cat is on the mat
- reference2:There is a cat on the matreference2:There\ is\ a\ cat\ on\ the\ matreference2:There is a cat on the mat
如果以unigramunigramunigram 来计算NMT outputNMT\ outputNMT output 的精确度p1p_1p1,则NMT outputNMT\ outputNMT output 中的7个词全出现在referencereferencereference 中,那p1=7/7=1p_1 = 7/7 =1p1=7/7=1。很完美?等等,貌似哪里不对。
很明显,这个NMT outputNMT\ outputNMT output 很不好,我们如果按照上面简单的计算精确度,就会导致生成的句子中会出现大量重复的tokentokentoken 。由此产生了改良的版本,我们计算NMT outputNMT\ outputNMT output 中每个不同的tokentokentoken 在referencereferencereference 中出现的次数,例如thethethe 在reference1reference1reference1 中出现2次,在reference2reference2reference2 中出现1次,那么我们认为thethethe 这个tokentokentoken 在referencereferencereference 中出现次数为2(选取最高的那个),称作为截断次数。那么NMT outputNMT\ outputNMT output 中p1=2/7p_1 = 2/7p1=2/7。
假设这次的 NMT outputNMT\ outputNMT output 为The cat the cat on the matThe\ cat\ the\ cat\ on\ the\ matThe cat the cat on the mat,我们来计算下bigrambigrambigram 的次数和截断次数。
| 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 |
上面表格中的countcountcount 表示该bigram tokenbigram\ tokenbigram token 在NMT outputNMT\ outputNMT output 中出现的次数,clip countclip\ countclip count 表示该bigram tokenbigram\ tokenbigram token 在referencereferencereference 中的截断次数。
故其p1=clip_count/countp_1 = clip\_count/ countp1=clip_count/count
公式化的结果:
pn=∑ngram∈y^countclip(ngram)∑ngram∈y^count(ngram)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^ 即为上面的NMT outputNMT\ outputNMT output。
但是上面的评价还是有一个缺点,当我们的NMT outputNMT\ outputNMT output 很简短时,pnp_npn 可能变得很大,显然这是不合理的,于是我们加上一个BPBPBP:
BP={1if NMT_output_len>referen_lenexp(1−NMT_output_len/reference_len)otherwiseBP= \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
综上所述:
Bleu=BP∗exp(∑n=1N∗(1/n)∗log(pn))Bleu = BP*exp\left ( \sum_{n=1}^{N}*(1/n)*log(p_n)\right )Bleu=BP∗exp(n=1∑N∗(1/n)∗log(pn))
这样我们就能计算Bleu1、Bleu2、Bleu3....Bleu1、Bleu2、Bleu3....Bleu1、Bleu2、Bleu3....
Rouge指标
Rouge-N

分母表示所有 referencereferencereference 中所有出现的ngramngramngram 个数。分子表示所有 referencereferencereference 与NMT ouputNMT\ ouputNMT ouput 共现 ngramngramngram 个数。嗯这是基于召回率的方式计算。就这么简单。
Rouge-L

XXX 是参考译文,YYY 是待评测译文,m,nm,nm,n 表示X,YX, YX,Y 序列长度。LCS(X,Y)LCS(X,Y)LCS(X,Y) 表示最长公共子序列长度。

1万+

被折叠的 条评论
为什么被折叠?



