-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_roc.m
More file actions
41 lines (39 loc) · 1.37 KB
/
plot_roc.m
File metadata and controls
41 lines (39 loc) · 1.37 KB
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
function auc = plot_roc( predict, ground_truth ,p ,n)
% predict - 分类器对测试集的分类结果
% ground_truth - 测试集的正确标签,这里只考虑二分类
% auc - 返回ROC曲线的曲线下的面积
%初始点为(1.0, 1.0)
x = 1.0;
y = 1.0;
%计算出ground_truth中正样本的数目pos_num和负样本的数目neg_num
% pos_num = sum(ground_truth==1);
% neg_num = sum(ground_truth==0);
pos_num = numel(find(strcmp(ground_truth, p)));
neg_num = numel(find(strcmp(ground_truth, n)));
%根据该数目可以计算出沿x轴或者y轴的步长
x_step = 1.0/neg_num;
y_step = 1.0/pos_num;
%首先对predict中的分类器输出值按照从小到大排列
[predict,index] = sort(predict);
ground_truth = ground_truth(index);
%对predict中的每个样本分别判断他们是FP或者是TP
%遍历ground_truth的元素,
%若ground_truth[i]=1,则TP减少了1,往y轴方向下降y_step
%若ground_truth[i]=0,则FP减少了1,往x轴方向下降x_step
for i=1:length(ground_truth)
if strcmp(ground_truth{i}, p)==1
y = y - y_step;
else
x = x - x_step;
end
X(i)=x;
Y(i)=y;
end
%画出图像
plot(X,Y,'-ro','LineWidth',2,'MarkerSize',3);
xlabel('FPR');
ylabel('TPR');
title('ROC曲线图');
%计算小矩形的面积,返回auc
auc = -trapz(X,Y);
end