sir, following is the error in my code while calculating ROC curve

Error using main_new (line 131)
You must pass scores as a vector of floating-point values.
%%Dorsal hand vein recognition using SVM
clc
clear all;
c=[];
addpath train;
addpath test;
mapping=getmapping(8,'u2'); %LBP
W=[0,0,0,0,0,0,0; ...
0,1,1,1,1,1,0; ...
0,1,2,4,2,1,0; ...
0,1,4,4,4,1,0; ...
0,1,2,4,2,1,0; ...
0,1,1,1,1,1,0; ...
0,0,0,0,0,0,0];
%%Training images
for folder_idx = 1 : 20 % no of classes
for i = 1 : 9 % no of images per class
thisfile = fullfile('train', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
% imshow(lpqhist);
a=[H2,lpqhist];
c=[c;a];
disp(sprintf('Done',i));
end
end
%%Testing images
d=[];
for folder_idx = 1 : 20 %no of classes
for i = 1 : 3 % no of images per class
thisfile = fullfile('test', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
a=[H2,lpqhist];
d=[d;a];
disp(sprintf('Done',i));
end
end
P_train=c;
P_test=d;
% %%PCA low dimension reduction
%
P_train = P_train';
% if classes are 20 then eiganvectors not exceed then 179
model = perform_pca(P_train,179); %rank(P_train)-1
test_features= linear_subspace_projection(P_test, model, 1);
P_train=model.train';
P_test=test_features';
%%Normalisation
P_train=P_train/256;
P_test=P_test/256;
% %%%%%%%%load label %%%%%%%%%%%%
train_label=load('train_label.txt');
test_label=load('test_label.txt');
% P_train = P_train';
% P_test = P_test';
%%classification K Nearest Neighour
% fit a kNN classification model
mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',1); %euclidean
% model=svmtrain(train_label,P_train);
% [predicted_label, accuracy, decision_values]=predict(test_label,P_test,model);
% apply the model to test data and compute test error
[predictlabel,score,cost] = predict(mdl,P_test);
%[predictlabel, score] = resubPredict(mdl,P_test);
% testError1 = sum(test_label ~= predict_label)/numel(test_label);
% Fit probabilities for scores
%groundTruthNumericalLable = [ones(20,1); zeros(20,1); -1.*ones(20,1)];
%[FPR, TPR, Thr, AUC, OPTROCPT] = perfcurve(predictlabel(:,1), score(:,1),'Good');
% [~, Accuracy, Thr] = perfcurve(predictlabel(:,1), score(:,1), 1,'yCrit','accu');
% figure,
% plot(Thr,Accuracy,'r-');
% hold on;
% plot(Thr,Accuracy,'bo');
% xlabel('Threshold for ''good'' Returns');
% ylabel('Classification Accuracy');
% grid on;
%
% disp('Recognition rate');
% [maxInd] = max(Accuracy) %display max index
% disp('threshold value');
% Thr(maxInd) %displays threshold value
score = double(score);
[tpr, fpr, thresh] = roc(predictlabel,score); % The ROC function
[fpr2, tpr2] = perfcurve(predictlabel,score,1); % The perfcurve funtion
plotroc(predictlabel,score); % calculating and plotting altogether
figure(); plot(fpr, tpr); title('ROC'); xlabel('False Positive Rate'); ylabel('True Positive Rate'); % Plotting the ROC results
figure(); plot(fpr2, tpr2); title('perfcurve'); xlabel('False Positive Rate'); ylabel('True Positive Rate'); % Plotting the perfcurve results

Risposte (1)

score returned by predict is an samples x classes array giving probabilities for each class. https://www.mathworks.com/help/stats/classificationknn.predict.html
perfcurve requires a vector of values for the scores https://www.mathworks.com/help/stats/perfcurve.html#bunsogv-scores .

6 Commenti

sir, I got a vector, score from this functions output [predictlabel,score,cost] = predict(mdl,P_test); but that score vector contains only 0 and 1 of size 60 X 20. I provide this score into roc function but this score and score from predict is same or not? how I can obtained score vector which I want to provide in roc function
sir, I read a link and did accordingly, but results are like recognition rate 100 at threshold 1 and curve attach here, is it correct?
%% Dorsal hand vein recognition using SVM
clc clear all; c=[]; addpath train; addpath test; mapping=getmapping(8,'u2'); %LBP
W=[0,0,0,0,0,0,0; ...
0,1,1,1,1,1,0; ...
0,1,2,4,2,1,0; ...
0,1,4,4,4,1,0; ...
0,1,2,4,2,1,0; ...
0,1,1,1,1,1,0; ...
0,0,0,0,0,0,0];
%%Training images
for folder_idx = 1 : 20 % no of classes
for i = 1 : 9 % no of images per class
thisfile = fullfile('train', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
% imshow(lpqhist);
a=[H2,lpqhist];
c=[c;a];
disp(sprintf('Done',i));
end
end
%% Testing images
d=[];
for folder_idx = 1 : 20 %no of classes for i = 1 : 3 % no of images per class
thisfile = fullfile('test', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
a=[H2,lpqhist];
d=[d;a];
disp(sprintf('Done',i));
end
end
%% P_train=c; P_test=d;
% %% PCA low dimension reduction % P_train = P_train';
% if classes are 20 then eiganvectors not exceed then 179 model = perform_pca(P_train,179); %rank(P_train)-1
test_features= linear_subspace_projection(P_test, model, 1); P_train=model.train'; P_test=test_features';
%% Normalisation
P_train=P_train/256;
P_test=P_test/256;
%% % P_train=mapminmax(P_train,0,1); % P_test=mapminmax(P_test,0,1);
% %%%%%%%% load label %%%%%%%%%%%% train_label=load('train_label.txt'); test_label=load('test_label.txt');
% P_train = P_train'; % P_test = P_test';
%% classification K Nearest Neighour
% fit a kNN classification model %mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',1); %euclidean
Model = fitctree(P_train(:,1:20),train_label,'ClassNames',{'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20'});
[predictlabel,score] = resubPredict(Model);
%% Dorsal hand vein recognition using SVM
clc clear all; c=[]; addpath train; addpath test; mapping=getmapping(8,'u2'); %LBP
W=[0,0,0,0,0,0,0; ...
0,1,1,1,1,1,0; ...
0,1,2,4,2,1,0; ...
0,1,4,4,4,1,0; ...
0,1,2,4,2,1,0; ...
0,1,1,1,1,1,0; ...
0,0,0,0,0,0,0];
%%Training images
for folder_idx = 1 : 20 % no of classes
for i = 1 : 9 % no of images per class
thisfile = fullfile('train', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
% imshow(lpqhist);
a=[H2,lpqhist];
c=[c;a];
disp(sprintf('Done',i));
end
end
%% Testing images
d=[];
for folder_idx = 1 : 20 %no of classes for i = 1 : 3 % no of images per class
thisfile = fullfile('test', num2str(folder_idx), [num2str(i) '.bmp ']);
B = imread(thisfile );
X = double(B);
X = imresize(X,[60 60],'bilinear');
H2=DSLBP(X,mapping,W);
Gray=X;
Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+60;
lpqhist=lpq(Gray,3,1,1,'nh');
a=[H2,lpqhist];
d=[d;a];
disp(sprintf('Done',i));
end
end
%% P_train=c; P_test=d;
% %% PCA low dimension reduction % P_train = P_train';
% if classes are 20 then eiganvectors not exceed then 179 model = perform_pca(P_train,179); %rank(P_train)-1
test_features= linear_subspace_projection(P_test, model, 1); P_train=model.train'; P_test=test_features';
%% Normalisation
P_train=P_train/256;
P_test=P_test/256;
%% % P_train=mapminmax(P_train,0,1); % P_test=mapminmax(P_test,0,1);
% %%%%%%%% load label %%%%%%%%%%%% train_label=load('train_label.txt'); test_label=load('test_label.txt');
% P_train = P_train'; % P_test = P_test';
%% classification K Nearest Neighour
% fit a kNN classification model %mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',1); %euclidean
Model = fitctree(P_train(:,1:20),train_label,'ClassNames',{'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20'});
[predictlabel,score] = resubPredict(Model);
I have used the fitcknn and perfcurve function to get the FPR and TPR. However, it only returns three values for both FPR and TPR, this is when using NumNeighbors =1, how to get the values of FPR and TPR for all instances?
Unfortunately, Balaji's code is poorly formated in a way that makes it ambiguous about whether some parts of it are commented out or not.
[predictlabel,score,cost] = predict(mdl,P_test);

Accedi per commentare.

Tag

Commentato:

il 12 Apr 2023

Community Treasure Hunt

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

Start Hunting!

Translated by