学习了评价指标中的precision , recall ,F-measure,因为之前接触过这三个指标,所以在1.1节与1.2节回顾了一下,1.3节举例说明这三个指标在recommender system(推荐系统)中的应用,最后写了precision与 recall用在cod suggestion中以及存在的疑惑。第二节是plan。明天也会看一下搜索引擎中用的RMSE,希望老师明天或者下周有时间能找我交谈一下。
文章目录
1. evaluate
1.1 accuracy --> precision & recall -->F-measure
对于垃圾邮件识别,如果有10封邮件,9封垃圾邮件,如果我们全识别成垃圾邮件,那么accuracy = 9 /10 =90%,这样的准确率虽然比较高,但是造成的后果所有的邮件被拦截,如果某一地区有了流行性疾病,10个人中有8个患病,那医院全都诊断所有人患病,这样accuracy很高,但是这个衡量标准(accuracy)不足以说明问题,因为这里我们对于类别不在视为平等,我们比较关心诊断的结果要可靠,尽可能所有真正的患者找出来。那么就有precision与recall。
比如10个就诊的人中7人患病,我们关心—诊断的结果要可靠,尽可能所有真正的患者找出来,也就是与我们relevant的是患病。
true = [1,1,1,1,1,0,0,0,0,0] #10个就诊的人中5人患病,(1表示患病,0表示健康)
predict = [1,1,1,0,0,1,1,1,0,0] #就诊结果中,有6人患病,3人是真正的患者
p
r
e
c
i
s
i
o
n
=
真
正
的
患
者
且
诊
断
为
患
者
诊
断
为
患
者
=
3
7
=
50
%
precision = \frac{\quad真正的患者且诊断为患者\quad}{\quad诊断为患者\quad} = \frac{ \ 3\ }{\quad 7\quad } = 50\%
precision=诊断为患者真正的患者且诊断为患者=7 3 =50%
r
e
c
a
l
l
=
真
正
的
患
者
且
诊
断
为
患
者
真
正
的
患
者
=
3
5
=
60
%
recall\quad = \frac{\quad真正的患者且诊断为患者\quad }{\quad真正的患者\quad} = \frac{ \ 3\ }{ \quad5\quad } = 60\%
recall=真正的患者真正的患者且诊断为患者=5 3 =60%
precision (also called positive predictive value) is the fraction of relevant instances among the retrieved instances,
while recall (also known as sensitivity) is the fraction of relevant instances that have been retrieved over the total
amount of relevant instances. Both precision and recall are therefore based on an understanding and measure
of relevance. ---wiki
#python
def evaluation(label,pred):
eval = {"TP":0,"TN":0,"FP":0,"FN":0}
for i in range(len(label)):
if label[i] == pred[i]:
if pred[i] == 1:
eval["TP"] += 1
else:
eval["TN"] += 1
else:
if pred[i] == 1:
eval["FP"] += 1
else:
eval["FN"] += 1
precision = eval["TP"] / (eval["TP"] + eval["FP"])
recall = eval["TP"] / (eval["TP"] + eval["FN"])
return precision,recall
true = [1,1,1,1,1,0,0,0,0,0] #10个就诊的人中5人患病,(1表示患病,0表示健康)
predict = [1,1,1,0,0,1,1,1,6,0] #就诊结果中,有6人患病,3人是真正的患者
prec,recl = evaluation(true,predict)
print(prec,recl)
precision = 50%就证明该家医院的对于患病的诊断结果的"准确率"只有50%,那诊断结果为健康的"准确率"呢?没有涉及到,与我们relevant的是患病(不论是真的患病还是诊断为患病)。
recall=60%就说明10个患者,6个患者会被正确找出来,注意,不代表诊断为患病的人为6个,因为在recall中被误诊的人我们不care,只care10个患者有没有找全。
precision和recall两个是互相矛盾的,如果为了提高precision,我们只对我们百分之白肯定的人下诊断结果,比如
true = [1,1,1,1,1,0,0,0,0,0] #10个就诊的人中5人患病,(1表示患病,0表示健康)
predict = [1,0,0,0,0,0,0,0,0,0] #就诊结果中,有1人患病,1人是真正的患者
p
r
e
c
i
s
i
o
n
=
1
1
=
100
%
precision = \frac{ \ 1\ }{\quad 1\quad } = 100\%
precision=1 1 =100%
r
e
c
a
l
l
=
1
5
=
20
%
recall\quad = \frac{ \ 1\ }{ \quad5\quad } = 20\%
recall=5 1 =20%
为了提高recall,只要有点迹象我们就认为患病:
true = [1,1,1,1,1,0,0,0,0,0] #10个就诊的人中5人患病,(1表示患病,0表示健康)
predict = [1,1,1,1,0,1,1,1,1,1] #就诊结果中,有9人患病,4人是真正的患者
p
r
e
c
i
s
i
o
n
=
4
9
=
44
%
precision = \frac{ \ 4\ }{\quad 9\quad } = 44\%
precision=9 4 =44%
r
e
c
a
l
l
=
5
5
=
100
%
recall\quad = \frac{ \ 5\ }{ \quad5\quad } = 100\%
recall=5 5 =100%
当然全部诊断为患者:
p
r
e
c
i
s
i
o
n
=
5
10
=
50
%
precision = \frac{ \ 5\ }{\quad 10\quad } = 50\%
precision=10 5 =50%
一般情况下,precision与recall,一个增加会导致另一个下降。我们可以综合考虑precision与recall,简单来说就是连着相加,有更好就是基于Rijsbergen’s effectiveness measure导出的F-measure
F
β
=
(
1
+
β
2
)
⋅
p
r
e
c
i
s
i
o
n
⋅
r
e
c
a
l
l
β
2
⋅
p
r
e
c
i
s
i
o
n
+
r
e
c
a
l
l
{\ F_{\beta }=(1+\beta ^{2})\cdot {\frac {\mathrm {precision} \cdot \mathrm {recall} }{\beta ^{2}\cdot \mathrm {precision} +\mathrm {recall} }}}
Fβ=(1+β2)⋅β2⋅precision+recallprecision⋅recall,
{ β > 1 注 重 r e c a l l β < 1 注 重 p r e c i s i o n β = 1 就 是 我 们 常 说 的 F 1 − m e s u r e \left\{ \begin{aligned} \beta>1 & & 注重recall \\ \beta < 1 & &注重precision \\ \beta = 1 & & 就是我们常说的F1-mesure \end{aligned} \right. ⎩⎪⎨⎪⎧β>1β<1β=1注重recall注重precision就是我们常说的F1−mesure
其中第一类错误率(Type I )就是误诊,将诊断为患者的人,实际上为健康的人。而将真正的患者诊断为健康的人为第二类错误( type II errors)。第一类错误多了,容易引起社会恐慌,医闹等,第二类错误使得病人不能及时就医,一般情况下,Type I 带来的情况更糟糕,比如正常邮件被当成垃圾邮件拦截而丢失工作,正常细胞被误诊为癌细胞被切除等等。
1.2 TP,TN,FP,FN
我们刚才反复强调与我们relevant的是:患病(不论是真的患病还是诊断为患病)
P是positive表示正类,我们与我们relevant的,也就是患病,
N是negative表示负类,我们与我们irrelevant的,也就是没患病,健康
TP, TN, FP, FN, where TP = True Positive, TN = True Negative, FP = False Positive, and FN = False Negative.
TP = 诊断为患者 且是真正的患者
TN = 诊断为健康 且是健康的
FP = 诊断为患者 实际是健康的
FN = 诊断为健康 实际是患者
A c c u r a c y = T P + T N T P + T N + F P + F N Accuracy = {\displaystyle \frac {\mathrm{TP + TN}}{ \mathrm {TP+TN+FP+FN} }} Accuracy=TP+TN+FP+FNTP+TN : 衡量所有的预测结果是否准确;
P r e c i s i o n = T P T P + F P Precision = {\displaystyle \frac {\mathrm{TP }}{ \mathrm {TP+FP} }} Precision=TP+FPTP 相关预测(relevant predictions )能力,或者查准率(exactness)。也就是预测出来的患者真的患病的精确度。
R e c a l l = T P T P + F N Recall = {\displaystyle \frac {\mathrm{TP }}{ \mathrm {TP+FN} }} Recall=TP+FNTP 描述查全能力(completeness)
precision is "how useful the search results are", and recall is "how complete the results are". --wiki
1.3 evaluation in recommender systems
以求推荐的前3个(top3)items的Precision and recall为例, 假设推荐系统(recommender systems)使用的是评分机制(rating system),1-5星,1星为不喜欢,5星为特别喜欢。以3.5分为分界线,大于等于3.5分为positive(good),小于3.5分为negative(bad),
我们共有10个items
item | rating | item | rating |
---|---|---|---|
item1 | 4 | item6 | ? |
item2 | 2 | item7 | 2 |
item3 | 3 | item8 | ? |
item4 | ? | item9 | ? |
item5 | 5 | item10 | 4 |
在一次预测中
item | user(true/predict) |
---|---|
item7 | 2 / 4.9 |
item5 | 5 / 4.5 |
item10 | 4 / 4.3 |
item3 | 2 / 3.6 |
item2 | 3 / 3.4 |
item1 | 4 / 2.3 |
… | … |
Relevant items:
Relevant items: item5, item10 and item1 total # of relevant items = 3 |
Recommended items @3
Recommended items : item7, item5 and item10 # of recommended items@3 = 3 |
Recommended and Relevant items @ 3
Recommended@3 intersection Relevant: item5 and item10#of recommended items that are relevant @3= 2 |
Precision @ 3:
Precision@3= 2/3 = 66.67%
Recall @ 3:
Recall@3 = 2/3 = 66.67%
1.4 evaluation in code suggestion
如果在code suggestion的时候,model最后一层是softmax ,出来的是probability,我们取top5,
Relevant items:
我们希望的suggestion只有一项就是我们挖掉的,也就是relevant items = 1
Recommended items @5
如果我们设置一个阈值(threshold),规定probability高于threshold为positive,否则为negative
Precision @ 5:
P r e c i s i o n @ 5 = 分 子 分 母 Precision@5 = {\displaystyle \frac {分子}{ \quad分母 \quad}} \quad Precision@5=分母分子 分子 ∈ \in ∈{0,1},分母 ∈ \in ∈{0,1,2,3,4,5}
Recall @ 5:
R e c a l l @ 5 = 分 子 分 母 = 1 Recall @ 5 = {\displaystyle \frac {分子}{ 分母= 1}} \quad Recall@5=分母=1分子 分子 ∈ \in ∈{0,1}
1.5 Question
code suggestion ,正确的suggestion只有一个,按这种理解,这里的recall非0即1。换用precision,其中受一方面受threshold影响,另一方面分子也是非零即1,衡量标准是否是这样用在code-suggestion?
1.6 RMSE(RMSD)
root-mean-square deviation (RMSD) or root-mean-square error (RMSE)
公式:
RMSD
=
∑
i
=
1
N
(
y
^
i
−
y
i
)
2
N
.
{\displaystyle \operatorname {RMSD} ={\sqrt {\frac { \sum_{i=1}^{N}({\hat {y}}_{i}-y_{i})^{2}}{N}}}.}
RMSD=N∑i=1N(y^i−yi)2.
推荐系统(recommender system)中的RMSE:
User A rated itemX with Ystar,
Y
∈
1
,
2
,
3
,
4
,
5
\quad \quad Y\in{1,2,3,4,5}
Y∈1,2,3,4,5
collect 一大笔这样的training data,train出一个回归模型(regression model),便可以预测某user对某items的评分为
y
^
\hat y
y^,对validation set求RMSE为:
RMSD
=
∑
i
=
1
N
(
y
^
i
−
y
i
)
2
N
.
{\displaystyle \operatorname {RMSD} ={\sqrt {\frac { \sum_{i=1}^{N}({\hat {y}}_{i}-y_{i})^{2}}{N}}}.}
RMSD=N∑i=1N(y^i−yi)2.
RMSE越小,预测越准确,model越好。
所以这个指标对应的是 回归模型。
参考:
- wiki Precision and recall
- wiki Type_I_and_type_II_errors
- Precision and recall in recommender systems. And some metrics stuff.
- Recall and Precision at k for Recommender Systems
- https://developers.google.com/machine-learning/crash-course/classification/precision-and-recall
- Python自然语言处理学习笔记之评价(evaluationd)
- Evaluating Recommender Systems: Choosing the best one for your business
2. plan
Period of time | plan |
---|---|
4.14 - 4.17 | 学习RNN与LSTM ,就为什么用RNN,LSTM,优缺点以及long dependence等方面进行了学习,已完成 |
4.18 - 4.20 | 1. evaluate机制 |
4.20 - 4.21 | 了解dataset,从dataset的AST的可视化到AST还原source code |
4.20 - 4.27 | 清楚attention mechanism,实现lstm+attention的code -suggestion,得到evaluate (预期,程序可能会出现loss不降等问题) |
4.27- 5.4 | 清楚pointer NN,实现lstm+attention+pointer的code -suggestion,得到evaluate的结果,对比之前的解结果进行分析 |
Period of time | plan |
---|---|
5.5- 5.19 | 论文初稿 |
5.20- 5.26 | 论文修改版 |
5.27- 5.31 | 论文最终版 |
3. plan in detail
Period of time | plan |
---|---|
周一 5.13 | basic structure, 5个部分 |
5.14 | 第二部分 理论基础 |
5.15 | 第三,四部分model和基本实现条件与讨论 |
5.16 周四 | 第五部分结论与展望 与第一部分绪论 |
5.17 周五 | abstract 交给mentor |