我正在尝试使用Matlab从头开始构建ROC曲线。当我运行该文件 auc是
auc_area =
-2.8129e-006
我认为这会削减截止步骤,但我无法理解。有什么功能可以执行吗? 这是我的代码:
num_pos = sum(all_labels);
tp = cumsum(l==1,1);
fp = repmat((1:n)',[1 m])-tp;
num_neg = n-num_pos;
fpr = bsxfun(@rdivide,fp,num_neg); %False Positive Rate
tpr = bsxfun(@rdivide,tp,num_pos); %True Positive Rate
%Plot the ROC curve
plot(fpr,tpr);
xlabel('False Positive');
ylabel('True Positive');
auc_area = sum(tpr.*[(diff(fp)==1); zeros(1,m)])./num_neg;
答案 0 :(得分:0)
这是一个执行此操作的功能。我3年前写的。
function auc = areaundercurve(FPR,TPR)
% given true positive rate and false positive rate calculates the area under the curve
% true positive are on the y-axis and false positives on the x-axis
% sum rectangular area between all points
% example: auc=areaundercurve(FPR,TPR);
[x2,inds]=sort(FPR);
x2=[x2,1]; % the trick is in inventing a last point 1,1
y2=TPR(inds);
y2=[y2,1];
xdiff=diff(x2);
xdiff=[x2(1),xdiff];
auc1=sum(y2.*xdiff); % upper point area
auc2=sum([0,y2([1:end-1])].*xdiff); % lower point area
auc=mean([auc1,auc2]);
end