Length of label vector does not match # of instances.

6 views (last 30 days)
Length of label vector does not match # of instances. Error using reshape To RESHAPE the number of elements must not change.
Error in linear_kernel (line 48) ypred = reshape(ypred,nx,ny);
% load training set and testing set
clear all;
train_set = loadMNISTImages('train-images.idx3-ubyte')';
train_label = loadMNISTLabels('train-labels.idx1-ubyte');
test_set = loadMNISTImages('t10k-images.idx3-ubyte')';
test_label = loadMNISTLabels('t10k-labels.idx1-ubyte');
%training
tic;
model = svmtrain(train_label, train_set, '-s 0 -t 0');
t1 = toc;
% classification
tic;
[predicted_label, accuracy, decision_values]=svmpredict(test_label, test_set, model);
t2 = toc;
disp(num2str(t1));
disp(num2str(t2));
%%%%%%%%%%%%%%%%%%plot%%%%%%%%%%%%%
train_set1=train_set;
test_set1=test_set;
nt=220.6940;
m=521.0442;
[Pa,V,me1,l1] = pcaproj(train_set1,2);
[n,p]=size(test_set1);
Pt=(test_set1-ones(n,1)*me1)*V;
min(Pa);
max(Pa);
min(Pt);
max(Pt);
m1 = -1;
M1 = 2.5;
m2 = -1.5;
M2 = 1;
ind1 = 0.25;
ind2 = 0.25;
[xg yg] = meshgrid([m1:ind1:M1],[m2:ind2:M2]);
[nx,ny] = size(xg);
X = [reshape(xg ,nx*ny,1) reshape(yg ,nx*ny,1)];
[n,p] = size(X);
Xg = X*V' + ones(n,1)*me1;
LabelS1=[1*ones(1,3334) 2*ones(1,3333) 3*ones(1,3333)];
[ypred,accuracy1,decision_values1] = svmpredict(LabelS1',Xg,model);
decision_values1=decision_values1';
maxi=max(decision_values1);
ypred = reshape(ypred,nx,ny);
contourf(xg,yg,ypred);shading flat;hold on;
[cs,h]=contour(xg,yg,ypred,[1 2 3]);
clabel(cs,h);
style= ['^+*'];
color= ['kkk'];
hold on;
nbclass=max(LS);
for i=0:nbclass-1
h=plot(Pt(i*nt+1:(i+1)*nt,1),Pt(i*nt+1:(i+1)*nt,2),[style(i+1) color(i+1)]);hold on;
end;

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!