Precision/Recall和ROC曲线

源地址 https://blog.csdn.net/adminabcd/article/details/46475361

【Precision/Recall的基本概念】

转载自http://www.zhizhihu.com/html/y2010/2137.html

查准率和查全率是信息检索效率评价的两个定量指标,不仅可以用来评价每次检索的准确性和全面性,也是在信息检索系统评价中衡量系统检索性能的重要方面。

(1)查准率(Precision ratio,简称为P),是指检出的相关文献数占检出文献总数的百分比。查准率反映检索准确性,其补数就是误检率。

查准率=(检索出的相关信息量/检索出的信息总量)*100%

(2)查全率(Recall ratio,简称为R),是指检出的相关文献数占系统中相关文献总数的百分比。查全率反映检索全面性,其补数就是漏检率。

查全率=(检索出的相关信息量/系统中的相关信息总量)*100%

【Precision/Recall曲线的来源】

要评价信息检索系统的性能水平,就必须在一个检索系统中进行多次检索。每进行一次检索,都计算其查准率和查全率,并以此作为坐标值,在平面坐标图上标示出来。通过大量的检索,就可以得到检索系统的性能曲线。 
Precision/Recall曲线一般是以每一次计算的查全率为横坐标,每一次计算的查准率为纵坐标。如下图所示: 
这里写图片描述 
该图是由100次检索得到的,由图可知:在查全率和查准率之间存在着相反的相互依赖关系–如果提高输出的查全率,就会降低其查准率

【ROC曲线】

ROC曲线的横坐标为false positive rate,纵坐标为true positive rate。 
其中

false positive rate=搜索出的不相关信息量/系统中的不相关信息量

true positive rate=搜索到的相关信息量/系统中的相关信息量

如下图所示: 
这里写图片描述 
该曲线也是由100次检索得到的,由图可知,false positive rate与true positive rate之间是正相关的。

【Precision/Recall和ROC曲线的一个小实例】

功能函数代码:

function [prec, tpr, fpr, thresh] = prec_rec(score, target, varargin)
% PREC_REC - Compute and plot precision/recall and ROC curves.
%
%   PREC_REC(SCORE,TARGET), where SCORE and TARGET are equal-sized vectors,
%   and TARGET is binary, plots the corresponding precision-recall graph
%   and the ROC curve.
%
%   Several options of the form PREC_REC(...,'OPTION_NAME', OPTION_VALUE)
%   can be used to modify the default behavior.
%      - 'instanceCount': Usually it is assumed that one line in the input
%                         data corresponds to a single sample. However, it
%                         might be the case that there are a total of N
%                         instances with the same SCORE, out of which
%                         TARGET are classified as positive, and (N -
%                         TARGET) are classified as negative. Instead of
%                         using repeated samples with the same SCORE, we
%                         can summarize these observations by means of this
%                         option. Thus it requires a vector of the same
%                         size as TARGET.
%      - 'numThresh'    : Specify the (maximum) number of score intervals.
%                         Generally, splits are made such that each
%                         interval contains about the same number of sample
%                         lines.
%      - 'holdFigure'   : [0,1] draw into the current figure, instead of
%                         creating a new one.
%      - 'style'        : Style specification for plot command.
%      - 'plotROC'      : [0,1] Explicitly specify if ROC curve should be
%                         plotted.
%      - 'plotPR'       : [0,1] Explicitly specify if precision-recall curve
%                         should be plotted.
%      - 'plotBaseline' : [0,1] Plot a baseline of the random classifier.
%
%   By default, when output arguments are specified, as in
%         [PREC, TPR, FPR, THRESH] = PREC_REC(...),
%   no plot is generated. The arguments are the score thresholds, along
%   with the respective precisions, true-positive, and false-positive
%   rates.


optargin = size(varargin, 2);
stdargin = nargin - optargin;  

if stdargin < 2
 error('at least 2 arguments required');
end

%%  parse optional arguments(解析可选参数)
num_thresh = -1;
hold_fig = 0;
% 无输出时,画出Precision/Recall和ROC曲线
plot_roc = (nargout <= 0);  
plot_pr  = (nargout <= 0);
instance_count = -1;
style = '';
plot_baseline = 1;

i = 1;
while (i <= optargin)
 if (strcmp(varargin{i}, 'numThresh'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    num_thresh = varargin{i+1};
    i = i + 2; 
    end
 elseif (strcmp(varargin{i}, 'style'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    style = varargin{i+1};
    i = i + 2;
    end
 elseif (strcmp(varargin{i}, 'instanceCount'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    instance_count = varargin{i+1};
    i = i + 2;
    end
 elseif (strcmp(varargin{i}, 'holdFigure'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
       if ~isempty(get(0,'CurrentFigure'))
       hold_fig = varargin{i+1};
       end
    i = i + 2;
    end
 elseif (strcmp(varargin{i}, 'plotROC'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    plot_roc = varargin{i+1};
    i = i + 2;
    end
 elseif (strcmp(varargin{i}, 'plotPR'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    plot_pr = varargin{i+1};
    i = i + 2;
    end
 elseif (strcmp(varargin{i}, 'plotBaseline'))
    if (i >= optargin)
    error('argument required for %s', varargin{i});
    else
    plot_baseline = varargin{i+1};
    i = i + 2;
    end
 elseif (~ischar(varargin{i}))
    error('only two numeric arguments required');
 else
 error('unknown option: %s', varargin{i});
 end
end


%% 数据处理
[nx,ny]=size(score);
if (nx~=1 && ny~=1)
 error('first argument must be a vector');
end

[mx,my]=size(target);
if (mx~=1 && my~=1)
 error('second argument must be a vector');
end
 %convert to colume vector
score  =  score(:);
target = target(:);

if (length(target) ~= length(score))
 error('score and target must have same length');
end

if (instance_count == -1)
 % set default for total instances
 instance_count = ones(length(score),1);
 target = max(min(target(:),1),0); % ensure binary target
else
 if numel(instance_count)==1
 % scalar
 instance_count = instance_count * ones(length(target), 1);
 end
 [px,py] = size(instance_count);
 if (px~=1 && py~=1)
 error('instance count must be a vector');
 end
 instance_count = instance_count(:);
 if (length(target) ~= length(instance_count))
 error('instance count must have same length as target');
 end
 target = min(instance_count, target);
end

 % set default for number of thresholds 
if num_thresh < 0
 score_uniq = unique(score);
 num_thresh = min(length(score_uniq), 100);
end
% set thresholds 
qvals = (1:(num_thresh-1))/num_thresh;
thresh = [min(score) quantile(score,qvals)];
% remove identical bins
thresh = sort(unique(thresh),2,'descend');
%计算总的target中1的个数和0的个数
total_target = sum(target);
total_neg = sum(instance_count - target);

prec = zeros(length(thresh),1);
tpr  = zeros(length(thresh),1);
fpr  = zeros(length(thresh),1);
for i = 1:length(thresh)    //进行多次检索
  %找出score值大于阈值的那部分数据target
 idx     = (score >= thresh(i));
 fpr(i)  = sum(instance_count(idx) - target(idx)); %计算这部分target中等于0的数目
 tpr(i)  = sum(target(idx));  %计算这部分target中等于1的数目
  % 查准率=搜索到的相关信息量/搜索到的所有信息量
 prec(i) = sum(target(idx)) / sum(instance_count(idx));  
end
%  查全率=搜索到的相关信息量/系统中的相关信息量
tpr=tpr / total_target;  
% 代价=搜索到的不相关信息量/系统中的不相关信息量
fpr = fpr / total_neg;    

if (plot_pr || plot_roc)
 % draw
 if (~hold_fig)
 figure
 if (plot_pr)
 if (plot_roc)
 subplot(1,2,1);
 end

 if (plot_baseline)
 target_ratio = total_target / (total_target + total_neg);  
 plot([0 1], [target_ratio target_ratio], 'k');
 end

 hold on
 hold all

 plot([0; tpr], [1 ; prec], style); % add pseudo point to complete curve
 xlabel('recall');
 ylabel('precision');
 title('precision-recall graph');
 end
 if (plot_roc)
 if (plot_pr)
 subplot(1,2,2);
 end

 if (plot_baseline)
 plot([0 1], [0 1], 'k');
 end

 hold on;
 hold all;

 plot([0; fpr], [0; tpr], style); % add pseudo point to complete curve

 xlabel('false positive rate');
 ylabel('true positive rate');
 title('roc curve');
 %axis([0 1 0 1]);
 if (plot_roc && plot_pr)
 % double the width
 rect = get(gcf,'pos');
 rect(3) = 2 * rect(3);
 set(gcf,'pos',rect);
 end
 end

 else
 if (plot_pr)
 if (plot_roc)
 subplot(1,2,1);
 end
 plot([0; tpr],[1 ; prec], style); % add pseudo point to complete curve
 end

 if (plot_roc)
 if (plot_pr)
 subplot(1,2,2);
 end
 plot([0; fpr], [0; tpr], style);
 end
 end
end
  • 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
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249

创建具体实例:

clear;
clc;
x1 = rand(1000, 1);
y1 = round(x1 + 0.5*(rand(1000,1) - 0.5));  %取整
prec_rec(x1, y1);  % 向量y1代表系统中的信息量,y1的值只能为01,y1取值为1代表相关信息量,否则为不相关信息量
% x2 = rand(1000,1);
% y2 = round(x2 + 0.75 * (rand(1000,1)-0.5));
% prec_rec(x2, y2, 'holdFigure', 1);
% legend('baseline','x1/y1','x2/y2','Location','SouthEast');

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值