Reputation: 41
I have to plot ROC using Matlab but my data set including 3 classes and most of examples are for 2 classes. How can I plot ROC for 3 classes (e.g. the fisher iris data set)?
Upvotes: 2
Views: 1330
Reputation: 6015
Here is a sample that plots ROCs following 1-against-others method:
%% loading data
load fisheriris
X = meas(:, 1:1); % more features -> higher AUC
Y = species;
%% dividing data to test and train sets
r = randperm(150); trn = r(1:100); tst = r(101:150);
%% train classifier
model = fitcdiscr(X(trn, :),Y(trn));
%% predict labels
% score store likelihood of each sample
% being of each class: nSample by nClass
[Y2, scores] = predict(model, X(tst, :));
%% plot ROCs
hold on
for i=1:length(model.ClassNames)
[xr, yr, ~, auc] = perfcurve(Y(tst),scores(:, i), model.ClassNames(i));
plot(xr, yr, 'linewidth', 1)
legends{i} = sprintf('AUC for %s class: %.3f', model.ClassNames{i}, auc);
end
legend(legends, 'location', 'southeast')
line([0 1], [0 1], 'linestyle', ':', 'color', 'k');
xlabel('FPR'), ylabel('TPR')
title('ROC for Iris Classification (1 vs Others)')
axis square
Upvotes: 1