I am working on a Pattern Recognition project on MATLAB. I am working on fisher iris data set. I have written some code that works for k-NN classification and after done the classification I want to calculate the confusion matrix. All my attempts have failed. So, I am asking for your help to find a way to calculate the confusion matrix C.
The related part of code is presented below:
fold_number = 5;
indices = crossvalind('Kfold',species, fold_number);
val = 1:2:5; % for these small k values there will not be an important difference
% regarding the cp ErrorRates. The difference is going to be
% observable for val = 1:2:100, for example!!! But the
% exercise asks only for k = 1,3,5.
err_arr = [];
for k=val
cp = classperf(species); % Reinitialize the cp-structure!
for i = 1:fold_number
test = (indices == i);
train = ~test;
class = knnclassify(meas(test,:),meas(train,:),species(train), k);
%class = knnclassify(meas(test,2),meas(train,2),species(train), k); % To experiment only with the 2nd feature
classperf(cp,class,test);
end
err_arr = [err_arr; cp.ErrorRate];
fprintf('The k-NN classification error rate for k = %d is: %f\n', k,cp.ErrorRate);
end
fprintf('\n The error array is: \n');
disp(err_arr);
I am looking forward to reading your answers!
If you have access to the statistics and machine learning toolbox you can simply use the confusionmat function. It returns the confusion matrix C
if you give as input the known classification (knownGroups
) and your k-nn oputput (predicted classification given by class
in your example):
[C] = confusionmat(knownGroups,class)