-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy path分类算法中的ROC与PR指标.html
209 lines (206 loc) · 12 KB
/
分类算法中的ROC与PR指标.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="../stylesheets/Github.css" type="text/css" />
<title>分类算法中的ROC与PR指标</title>
</head>
<body>
<div id="header"><center>
<p class="header_titleline">
<a href="../index.html" target="_self" title="主页">主页 </a><a href="../Search.html" target="_self" title="站内搜索">站内搜索 </a><a href="../Projects.html" target="_self" title="项目研究">项目研究 </a><a href="../Archives.html" target="_self" title="文章存档">文章存档 </a><a href="../README.html" target="_self" title="分类目录">分类目录 </a><a href="../AboutMe.html" target="_self" title="关于我">关于我 </a>
</p>
</center></div>
<h1>分类算法中的ROC与PR指标</h1>
<h4>2015-01-26 / xiahouzuoxin</h4>
<h4>Tags: ROC,PR</h4>
转载请注明出处: <a href="http://xiahouzuoxin.github.io/notes/">http://xiahouzuoxin.github.io/notes/</a>
<div id="TOC">
<ul>
<li><a href="#基本概念">基本概念</a></li>
<li><a href="#roc曲线和pr曲线">ROC曲线和PR曲线</a></li>
</ul>
</div>
<!---title:分类算法中的ROC与PR指标-->
<!---keywords:ROC,PR-->
<!---date:2015-01-26-->
<p>做过图像识别、机器学习或者信息检索相关研究的人都知道,论文的实验部分都要和别人的算法比一比。可怎么比,人多嘴杂,我说我的方法好,你说你的方法好,各做各的总是不行——没规矩不成方圆。于是慢慢的大家就形成了一种约定,用ROC曲线和PR曲线来衡量算法的优劣。关于ROC曲线和PR曲线的详细介绍可参考资料:</p>
<ol style="list-style-type: decimal">
<li><a href="http://home.comcast.net/~tom.fawcett/public_html/ROCCH/index.html">ROC Analysis and the ROC Convex Hull</a></li>
<li>Tom Fawcett,<a href="https://cours.etsmtl.ca/sys828/REFS/A1/Fawcett_PRL2006.pdf">An introduction to ROC analysis</a></li>
<li>Jesse Davis,Mark Goadrich. <a href="https://www.biostat.wisc.edu/~page/rocpr.pdf">The Relationship Between Precision-Recall and ROC Curves.</a>,还有一份与这篇文章对应的<a href="http://www.ke.tu-darmstadt.de/lehre/archiv/ws0708/ml-sem/Folien/Wen_Zhang.pdf">PPT讲稿</a></li>
</ol>
<p>有这3份资料足以,应用分析和理论分析都讲得很不错。</p>
<h2 id="基本概念">基本概念</h2>
<ol style="list-style-type: decimal">
<li>True Positives,TP:预测为正样本,实际也为正样本的特征数</li>
<li>False Positives,FP:预测为正样本,实际为负样本的特征数(错预测为正样本了,所以叫False)</li>
<li>True Negatives,TN:预测为负样本,实际也为负样本的特征数</li>
<li>False Negatives,FN:预测为负样本,实际为正样本的特征数(错预测为负样本了,所以叫False)</li>
</ol>
<p>接着往下做做小学的计算题:</p>
<ul>
<li>TP+FP+FN+FN:特征总数(样本总数)</li>
<li>TP+FN:实际正样本数</li>
<li>FP+TN:实际负样本数</li>
<li>TP+FP:预测结果为正样本的总数</li>
<li>TN+FN:预测结果为负样本的总数</li>
</ul>
<p>有些绕,为做区分,可以这样记:相同的后缀(P或N)之和表示__预测__正样本/负样本总数,前缀加入T和F;实际样本总数的4个字母完全不同,含TP(正正得正)表示实际正样本,含FP(负正得负)表示实际负样本。</p>
<h2 id="roc曲线和pr曲线">ROC曲线和PR曲线</h2>
<p>True Positive Rate(TPR)和False Positive Rate(FPR)分别构成ROC曲线的y轴和x轴。</p>
<ol style="list-style-type: decimal">
<li>TPR=TP/(TP+FN),实际正样本中被预测正确的概率</li>
<li>FPR=FP/(FP+TN),实际负样本中被错误预测为正样本的概率</li>
</ol>
<p>实际学习算法中,预测率100%的话,TPR=100%和FPR=0,所以TPR越大而FPR越小越好。仅用其中一个作为衡量指标可以吗?考虑这么一种情况,一幅图片假如600x480个像素,其中目标(正样本)仅有100个像素,假如有某种算法,预测的目标为包含所有像素600x480,这种情况下TPR的结果是TPR=100%,但FPR却也接近于100%。明显,TPR满足要求但结果却不是我们想要的,因为FPR太高了。</p>
<p>Precision和Recall(有人中文翻译成召回率)则分别构成了PR曲线的y轴和x轴。</p>
<ol style="list-style-type: decimal">
<li>Precision=TP/(TP+FP),预测结果为有多少正样本是预测正确了的</li>
<li>Recall=TP/(TP+FN),召回率很有意思,这个其实就=TPR,相对于Precision只不过参考样本从预测总正样本数结果变成了实际总正样本数。</li>
</ol>
<p>同理,Precision和Recall同时考虑才能确定算法好坏。好了,原来一切尽在尽在下图中,</p>
<div class="figure">
<img src="../images/分类算法中的ROC与PR指标/ConfusionMatrix.png" alt="图:Confusion Matrix" /><p class="caption">图:Confusion Matrix</p>
</div>
<p>既然ROC和PR都是同时要考虑两个指标,一个我好一个你好,到底谁好?画到ROC空间一看便知,如下图,将TPR和FPR分别画在两个坐标轴上,则沿着对角线的方向,离右上角越近,算法效果越好。(由于ROC和PR类似,以下仅讨论ROC空间和ROC曲线。)</p>
<div class="figure">
<img src="../images/分类算法中的ROC与PR指标/ROC-space.png" alt="图:ROC空间" /><p class="caption">图:ROC空间</p>
</div>
<p>一个分类算法,找个最优的分类效果,对应到ROC空间中的一个点。通常分类器的输出都是Score,比如SVM、神经网络,有如下的预测结果:</p>
<table>
<caption><strong>TABLE</strong> 一般分类器的结果都是Score表</caption>
<thead>
<tr class="header">
<th align="left">no.</th>
<th align="left">True</th>
<th align="left">Hyp</th>
<th align="left">Score</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">1</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99999</td>
</tr>
<tr class="even">
<td align="left">2</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99999</td>
</tr>
<tr class="odd">
<td align="left">3</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99993</td>
</tr>
<tr class="even">
<td align="left">4</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99986</td>
</tr>
<tr class="odd">
<td align="left">5</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99964</td>
</tr>
<tr class="even">
<td align="left">6</td>
<td align="left">p</td>
<td align="left">Y</td>
<td align="left">0.99955</td>
</tr>
<tr class="odd">
<td align="left">7</td>
<td align="left">n</td>
<td align="left">Y</td>
<td align="left">0.68139</td>
</tr>
<tr class="even">
<td align="left">8</td>
<td align="left">n</td>
<td align="left">Y</td>
<td align="left">0.50961</td>
</tr>
<tr class="odd">
<td align="left">9</td>
<td align="left">n</td>
<td align="left">N</td>
<td align="left">0.48880</td>
</tr>
<tr class="even">
<td align="left">10</td>
<td align="left">n</td>
<td align="left">N</td>
<td align="left">0.44951</td>
</tr>
</tbody>
</table>
<p>True表示实际样本属性,Hyp表示预测结果样本属性,第4列即是Score,Hyp的结果通常是设定一个阈值,比如上表就是0.5,Score>0.5为正样本,小于0.5为负样本,这样只能算出一个ROC值,为更综合的评价算法的效果,通过取不同的阈值,得到多个ROC空间的值,将这些值描绘出ROC空间的曲线,即为ROC曲线。</p>
<div class="figure">
<img src="../images/分类算法中的ROC与PR指标/ROC曲线绘制.png" alt="图:ROC曲线绘制" /><p class="caption">图:ROC曲线绘制</p>
</div>
<p>我们只要明白这个基本的点,详细的ROC曲线绘制已经有很多代码了,资料1就提供了Prel直接根据Score绘制ROC曲线的代码,Matlab也有,下载链接:</p>
<ol style="list-style-type: decimal">
<li><a href="../codes/分类算法中的ROC与PR指标/prec_rec.zip">Local: prec_rec.m</a></li>
<li><a href="http://www.mathworks.com/matlabcentral/fileexchange/21528-precision-recall-and-roc-curves/content//prec_rec/prec_rec.m">Mathworks: prec_rec.m</a></li>
</ol>
<p>有了ROC曲线,更加具有参考意义的评价指标就有了,在ROC空间,算法绘制的ROC曲线越凸向西北方向效果越好,有时不同分类算法的ROC曲线存在交叉,因此很多文章里用AUC(即Area Under Curve曲线下的面积)值作为算法好坏的评判标准。关于这里的凸理论可参考文章开头的[资料2]。</p>
<p>与ROC曲线左上凸不同的是,PR曲线是右上凸效果越好,下面是两种曲线凸向的简单比较:</p>
<div class="figure">
<img src="../images/分类算法中的ROC与PR指标/算法在ROC空间与PR空间的不同比较.png" alt="图:算法在ROC空间与PR空间的不同比较" /><p class="caption">图:算法在ROC空间与PR空间的不同比较</p>
</div>
<p>作为衡量指标,选择ROC或PR都是可以的。但是资料3显示,ROC和PR虽然具有相同的出发点,但并不一定能得到相同的结论,在写论文的时候也只能参考着别人已有的进行选择了。</p>
<div class="ds-thread" data-thread-key="分类算法中的ROC与PR指标" data-title="分类算法中的ROC与PR指标" data-url="xiahouzuoxin.github.io/notes/html/分类算法中的ROC与PR指标.html"></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"5","bdPos":"right","bdTop":"300"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"xiahouzuoxin"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<!-- 多说公共JS代码 end -->
<div id="footer">
<p class="footer_subline">联系邮箱: [email protected]</p>
<p class="footer_subline">声明: 本站所有文章如非特别说明均为原创,转载请注明出处!
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253219218'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1253219218%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
</p>
</div>
<!-- 回到顶部 -->
<script>
lastScrollY=0;
function heartBeat(){
var diffY;
if (document.documentElement && document.documentElement.scrollTop)
diffY = document.documentElement.scrollTop;
else if (document.body)
diffY = document.body.scrollTop
else
{/*Netscape stuff*/}
percent=.1*(diffY-lastScrollY);
if(percent>0)percent=Math.ceil(percent);
else percent=Math.floor(percent);
document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px";
lastScrollY=lastScrollY+percent;
}
suspendcode="<div id=\"full\" style='right:1px;POSITION:absolute;TOP:600px;z-index:100'><a onclick='window.scrollTo(0,0);'><img src='../images/top.png'></a><br></div>"
document.write(suspendcode);
window.setInterval("heartBeat()",1);
</script>
</body>
</html>