How to get ROC curve using SVM?

11 views (last 30 days)
Imran Riaz
Imran Riaz on 3 Nov 2022
Answered: Rohit on 22 Feb 2023
I have extracted the features of finger knuckles using LBP and now want to classify using SVM. I am using following code and I get values of F1 score , recall, percision, and accuracy. Now I also want to plot ROC curve. Plz help me which changes are required to get ROC for one-vs-all classicification using SVM. Feat file is attached.
clc
clear
close all
%%
Data = load ("Feat13.mat");
Features = Data.Feat (:,1:end-1);
Labels = Data.Feat (:,end);
% Features = Data.Feat (1:200,1:end-1); % for 1st 200 only
% Labels = Data.Feat (1:200,end);
% [m,n] = size(Features);
[m,n] = size(Data.Feat);
P = 0.50;
idx = randperm(m) ;
Training = Data.Feat(idx(1:round(P*m)),:);
Testing = Data.Feat(idx(round(P*m)+1:end),:);
Train_Features = Training(:,1:end-1);
Train_Labels = Training(:,end);
Test_Features = Testing(:,1:end-1);
Test_Labels = Testing(:,end);
rng(1); % For reproducibility
% SVMModel = fitcecoc(Train_Features,Train_Labels);
t = templateSVM('Standardize',true,'KernelFunction','linear');
SVMModel = fitcecoc(Train_Features,Train_Labels,'Learners',t);
error = resubLoss(SVMModel)
[Pred_TrainLabels,Pred_TrainScore] = predict(SVMModel,Train_Features);
[Pred_TestLabels,Pred_TestScore] = predict(SVMModel,Test_Features);
% ROC_data = roc_curve(Test_Labels,Pred_TestLabels)
% [Pred_WholeLabels,Pred_WholeScore] = predict(SVMModel,Features);
% ROC_data = roc_curve(Labels,Pred_wholeLabels)
% ROC_data = roc_curve(Labels,Pred_WholeLabels)
%
% [X,Y,T,AUC] = perfcurve(Labels,Pred_WholeScore,'5')
% plot(X,Y)
%% % For whole labels and scores
% [tpr,fpr,thresholds] = roc(Labels,Pred_WholeLabels);
% plotroc(Labels,Pred_WholeScore())
% %% For Test labels and scores only
% [tpr,fpr,thresholds] = roc(Labels,Pred_TestLabels);
% plotroc(Labels,Pred_TestScore())
% [c_matrixp,Result]= confusion.getMatrix(Test_Labels,Pred_TestLabels);
% [c_matrixp,Result]= confusion.getMatrix(Labels,Pred_WholeLabels);
fig = figure;
cm = confusionchart(Test_Labels,Pred_TestLabels,'RowSummary','row-normalized','ColumnSummary','column-normalized');
% cm = confusionchart(Labels,Pred_WholeLabels,'RowSummary','row-normalized','ColumnSummary','column-normalized');
cm.Title = 'Finger Creases Classification Using SVM';
cm.RowSummary = 'row-normalized';
cm.ColumnSummary = 'column-normalized';
[m, order] = confusionmat(Test_Labels,Pred_TestLabels);
Diagonal=diag(m);
sum_rows=sum(m,2);
Precision=Diagonal./sum_rows;
Overall_Precision=mean(Precision)
sum_col=sum(m,1);
recall=Diagonal./sum_col';
recall(isnan(recall))=0;
overall_recall=mean(recall)
F1_Score=2*((Overall_Precision*overall_recall)/(Overall_Precision+overall_recall))
accuracy = sum(Test_Labels == Pred_TestLabels,'all')/numel(Pred_TestLabels)

Answers (1)

Rohit
Rohit on 22 Feb 2023
You can compute a ROC curve and other performance curves by creating a rocmetrics object which support both binary and multiclass classification.
Refer to the below documentation links for further reference-

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!