MATLAB Answers

How to plot ROC and calculate AUC form these data?

187 views (last 30 days)
Pradya Panyainkaew
Pradya Panyainkaew on 12 Apr 2018
Answered: sudhakar shinde on 12 Apr 2018
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


Hasan shovon
Hasan shovon on 12 Apr 2018
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;
if ~islogical(labels)
error('labels input should be logical');
if ~isequal(size(labels),size(scores))
error('labels and scores should have the same size');
[n,m] = size(labels);
num_pos = sum(labels);
if any(num_pos==0)
error('no positive labels entered');
if any(num_pos==n)
error('no negative labels entered');
[~,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
xlabel('False Positive');
ylabel('True Positive');
auc = sum(tpr.*[(diff(fp)==1); zeros(1,m)])./num_neg;
Pradya Panyainkaew
Pradya Panyainkaew on 12 Apr 2018
Due to I am a beginner, I think variables name labels and plot_flag are my outputs and targets, respectively. However, I don't know how to calculate a variable name scores. Do you have any suggestion to find "scores" for me ?

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by