There is some error while calling function fitcknn. I passed parameters like fitcknn(P_​train,trai​n_label,'D​istance','​euclidean'​,'NumNeigh​bors',5) here size of P_train is 176 X 180 and train_label is 180 1

Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 201)
X and Y do not have the same number of observations.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 487)
classreg.learning.FullClassificationRegressionModel.prepareDataCR(...
Error in ClassificationKNN.prepareData (line 868)
prepareData@classreg.learning.classif.FullClassificationModel(X,Y,varargin{:},'OrdinalIsCategorical',true);
Error in classreg.learning.FitTemplate/fit (line 213)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in ClassificationKNN.fit (line 853)
this = fit(temp,X,Y);
Error in fitcknn (line 315)
this = ClassificationKNN.fit(X,Y,RemainingArgs{:});
Error in main_new (line 621)
mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',5);
-----
%%Code%%%%%%%%%%%%%%%
%%Dorsal hand vein recognition using SVM
clc
clear all;
c=[];
addpath train;
addpath test;
mapping=getmapping(8,'u2'); %LBP
% W=[2,1,1,1,1,1,2; ...
% 2,4,4,1,4,4,2; ...
% 1,1,1,0,1,1,1; ...
% 0,1,1,0,1,1,0; ...
% 0,1,1,1,1,1,0; ...
% 0,1,1,2,1,1,0; ...
% 0,1,1,1,1,1,0];
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];
for i=1:9
B=imread(strcat('train\','1\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','2\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','3\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','4\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','5\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','6\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','7\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','8\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','9\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','10\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','11\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','12\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','13\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','14\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','15\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','16\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','17\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','18\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','19\',num2str(i),'.bmp'));
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
for i=1:9
B=imread(strcat('train\','20\',num2str(i),'.bmp'));
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
d=[];
for i=1:3
B=imread(strcat('test\','1\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','2\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','3\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','4\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','5\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','6\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','7\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','8\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','9\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','10\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','11\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','12\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','13\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','14\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','15\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','16\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','17\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','18\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','19\',num2str(i),'.bmp'));
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
for i=1:3
B=imread(strcat('test\','20\',num2str(i),'.bmp'));
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
P_train=c;
P_test=d;
% %%PCA low dimension reduction
%
P_train = P_train';
model = perform_pca(P_train,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
% results = nn_classification_PhD(P_train,train_label, P_test, test_label, size(P_test,1), 'euc');
mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',5);
predict_label = predict(mdl,P_test);

 Accepted Answer

"Predictor data, specified as numeric matrix.
Each row corresponds to one observation (also known as an instance or example), and each column corresponds to one predictor variable (also known as a feature).
The length of Y and the number of rows of X must be equal."
So, you are passing in X data that has 176 samples, each with 180 features, but you are passing in 180 feature labels.
Chances are that you want to pass in X.'

7 Comments

By the way:

for folder_idx = 1 : 20
 for i = 1 : 3
   thisfile = fullfile('test', num2str(folder_idx), [num2str(i) '.bmp']);
   B = imread(thisfile);
   ...
 end
end

You do not need to repeat your code with different hard-coded folder names.

Sir, am confused, according to ur suggestion I reduced train label by 1 now train_label is 179 X 1 and p_train is 179 X 180 now rows of x and length of y is equal, but still there are some errors Error using ExhaustiveSearcher/knnsearch (line 144) Y must be a matrix with 180 columns.
Error in ClassificationKNN/score (line 425) [CIDX,dist] = knnsearch(this.NS, X,'k',this.ModelParams.NumNeighbors,...
Error in ClassificationKNN/predict (line 703) posteriors = score(this,X);
Error in main_new (line 104) predict_label = predict(mdl,P_test);
You should simply have changed
mdl = fitcknn(P_train,train_label,'Distance','euclidean','NumNeighbors',5);
to
mdl = fitcknn(P_train.', train_label, 'Distance', 'euclidean', 'NumNeighbors', 5);
yes sir I did it but still error is remained i.e.
Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 201)
X and Y do not have the same number of observations.
%%Dorsal hand vein recognition using SVM
clc
clear all;
c=[];
addpath train;
addpath test;
mapping=getmapping(8,'u2'); %LBP
% W=[2,1,1,1,1,1,2; ...
% 2,4,4,1,4,4,2; ...
% 1,1,1,0,1,1,1; ...
% 0,1,1,0,1,1,0; ...
% 0,1,1,1,1,1,0; ...
% 0,1,1,2,1,1,0; ...
% 0,1,1,1,1,1,0];
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
for i = 1 : 9
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
for i = 1 : 3
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
% results = nn_classification_PhD(P_train,train_label, P_test, test_label, size(P_test,1), 'euc');
mdl = fitcknn(P_train',train_label,'Distance','euclidean','NumNeighbors',5);
predict_label = predict(mdl,P_test);
It is confusing that you have
P_train = P_train';
P_test = P_test';
and also pass P_train' to fitcknn, but you pass P_test without transpose to predict()
You should make sure that your arrays are arranged so that each row is independent of the other rows and that your label array has the same length as the number of rows, and it is that arrangement of data that you should be passing in to both fitcknn() and predict()
Sir, In this function [predict_label,score,cost] = predict(mdl,P_test); the score matrix contains o and 1 values but how to see a matrix contains distances.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!