wine_data;
%In this case, we want to normalize the data so that the window width can be the same
%for different dimensions
normalize_flag = 1;
if normalize_flag ~= 0,
   for ccol=2:size(wine_ins,2),
      minV=min(wine_ins(:,ccol));
      maxV=max(wine_ins(:,ccol));
      varCol=var(wine_ins(:,ccol));
      wine_ins(:,ccol) = (wine_ins(:,ccol)-minV)/sqrt(varCol);
   end
end
K_nn = 21;
l_nn = 18;
fprintf(1,'Now classifying the Wine dataset with %d instances using (%d, %d)-nearest neighbor rule.\n', ...
   size(wine_ins,1), K_nn, l_nn);
%In order to use PNN, we need to normalize each instance to have unit length
total_ins = 0;
total_correct = 0;
total_wrong = 0;
total_reject = 0;
for ind=1:size(wine_ins,1),
   wine_ins_rest=wine_ins([1:(ind-1) (ind+1):size(wine_ins,1)],:);
   %First we compute the distance to the one that is left out
   x=wine_ins(ind,2:end);
   dist_to_rest = wine_ins_rest(:,2:end)-repmat(x,[size(wine_ins_rest,1) 1]);
   dist_to_rest = dist_to_rest.^2;
   dist_to_rest = sum(dist_to_rest,2);
   [sorted_dist, sorted_ind]=sort(dist_to_rest);
   labels_class=zeros([1 3]);
   for i=1:K_nn,
      c_class = wine_ins_rest(sorted_ind(i),1);
      labels_class(c_class)=labels_class(c_class)+1;
   end
   fprintf(1,'For instance %d (from class %d), the number of nearest neighbors are %d %d %d.\n',...
      ind, wine_ins(ind,1), labels_class(1), labels_class(2), labels_class(3));
   [max_val classified_ind]=max(labels_class);
   if max_val > l_nn,
      fprintf(1,'   using %d-NN classifier, it is classified as class %d, which is ', K_nn, classified_ind);
      if classified_ind==wine_ins(ind,1),
         fprintf(1,'correct.\n');
         total_correct = total_correct + 1;
      else
         fprintf(1,'wrong.\n');
         total_wrong = total_wrong + 1;
      end
   else
      total_reject = total_reject + 1;
      fprintf(1,'It is rejected to be classified.\n');
   end
   total_ins = total_ins + 1;
   %pause;
end
fprintf(1,'For (%d,%d)-NN classification, there are %d correct and %d rejected (out of %d), which is %8.6f %%.\n',...
   K_nn, l_nn, total_correct, total_reject, total_ins, (100.0*total_correct/(total_ins-total_reject)));

