I have to classify Iris data using k nearest neighbor, (k=1:30) I have divided the data into sample and training involving the Leave-one-out cross validation, so I have the following script:
load fisheriris
group=[ones(1,50), 2*ones(1,50), 3*ones(1,50)]';
for k=(1:30);
for i=(1:150);
sample=meas(i,:);
training1=meas;
training1(i,:)=[];
group_sample=group(i);
group_training=group;
group_training(i)=[];
c(i,k)=knnclassify(sample,training1,group_training,k);
A=confusionmat(group, c(i,k));
mean_error(k)=mean(A(:));
std_error(k)=std(A(:));
end
end
The problem is that I can not make confusion matrix, since c returns me back only one value (for first sample), where is the problem, can anybody help?? Thanks!
I think you're probably after this:
for k=1:30
for i=1:150
sample=meas(i,:);
training1=meas;
training1(i,:)=[];
group_sample=group(i);
group_training=group;
group_training(i)=[];
c(i,k)=knnclassify(sample,training1,group_training,k);
end
A=confusionmat(group, c(:,k));
mean_error(k)=mean(A(:));
std_error(k)=std(A(:));
end
So in other words only find the confusion matrix after the cross-validation loop.