MATLAB Answers

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

25 views (last 30 days)
Balaji M. Sontakke
Balaji M. Sontakke on 2 Apr 2018
Commented: Walter Roberson on 12 Apr 2019
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

  0 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 2 Apr 2018
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 .

  5 Comments

Show 2 older comments
Balaji M. Sontakke
Balaji M. Sontakke on 3 Apr 2018
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);
Atheer Alkubeyyer
Atheer Alkubeyyer on 12 Apr 2019
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?
Walter Roberson
Walter Roberson on 12 Apr 2019
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!