How to plot ROC and calculate AUC form these data?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi everyone
If I have targets and outputs in attach files. How can I plot ROC and calculate AUC by using these data ?
Thanks in advance
댓글 수: 2
Hasan shovon
2018년 4월 12일
use this:
% code
function [auc,fpr,tpr] = fastAUC(labels,scores,plot_flag)
% function [auc,fpr,tpr] = myauc(labels,scores,plot_flag)
%
% This function calculates m AUC values for m ranked lists.
% n is the number of ranked items.
% m is the number of different rankings.
%
% Input: labels is nXm binary logical.
% scores is nXm real. For a high AUC the higher scores should have
% labels==1.
% plot_flag: binary flag, if TRUE then m ROC curves will be plotted
% (default FALSE).
%
% Output: auc is mX1 real, the Area Under the ROC curves.
% fpr is nXm real, the false positive rates.
% tpr is nXm real, the true positive rates.
if ~exist('plot_flag','var')
plot_flag = 0;
end
if ~islogical(labels)
error('labels input should be logical');
end
if ~isequal(size(labels),size(scores))
error('labels and scores should have the same size');
end
[n,m] = size(labels);
num_pos = sum(labels);
if any(num_pos==0)
error('no positive labels entered');
end
if any(num_pos==n)
error('no negative labels entered');
end
[~,scores_si] = sort(scores,'descend');
clear scores
scores_si_reindex = scores_si+ones(n,1)*(0:m-1)*n;
l = labels(scores_si_reindex);
clear scores_si 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
if plot_flag==1
plot(fpr,tpr);
xlabel('False Positive');
ylabel('True Positive');
end
auc = sum(tpr.*[(diff(fp)==1); zeros(1,m)])./num_neg;
답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 ROC - AUC에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!