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)?
Asked
Active
Viewed 1,108 times
2
-
You may have to do it for each class separately ([1](https://stackoverflow.com/q/56227246/8239061), [2](https://stats.stackexchange.com/q/2151/177387), [3](https://www.researchgate.net/post/Is-it-possible-to-plot-a-ROC-curve-for-a-multiclass-classification-algorithm-to-study-its-performance-or-is-it-better-to-analyze-by-confusion-matrix)). – SecretAgentMan Nov 25 '20 at 21:00
-
Related from CrossValidated: [How to plot ROC curves in multiclass classification?](https://stats.stackexchange.com/q/2151/177387) – SecretAgentMan Nov 25 '20 at 21:01
1 Answers
1
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

saastn
- 5,717
- 8
- 47
- 78