我需要使用Matlab绘制ROC曲线,但我的数据集包含三个类别,而大多数示例都是针对两个类别的。我该如何为三个类别绘制ROC曲线(例如,fisher iris数据集)?
回答:
以下是一个使用1对多方法绘制ROC曲线的示例:
%% loading dataload fisheririsX = meas(:, 1:1); % more features -> higher AUCY = species;%% dividing data to test and train setsr = randperm(150); trn = r(1:100); tst = r(101:150);%% train classifiermodel = 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 ROCshold onfor 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);endlegend(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