0
votes

I use knn classifier to classify images according to their writers (problem of writer recognition). I worked on a given database that contains 150 images with 100 images for training and 50 images for testing. I use this code to find the accuracy of the classifier( k=1):

load('testdirection.mat') 
load('traindirection.mat')
load('testlabels.mat') 
load('trainlabels.mat') 
class = knnclassify(testdirection,traindirection, trainlabels);
cp = classperf(testlabels,class);
cp.CorrectRate 
fprintf('KNN Classifier Accuracy: %.2f%%\n',100*cp.CorrectRate )

I want to find different accuracy for different value for k [1..25] and save result in matrix matlab. I want also to plot the result to see the variability of accuracy depending on the value of k. Please, help me to change this code and thanks in advance

1

1 Answers

0
votes

knnclassify has an optional fourth argument k which is the number of nearest neighbors. You can just put the knnclassify in a for loop and iterate through all values for k.

load('testdirection.mat') 
load('traindirection.mat')
load('testlabels.mat') 
load('trainlabels.mat') 

for k=25:-1:1
    class = knnclassify(testdirection,traindirection, trainlabels, k);
    cp = classperf(testlabels,class);
    correctRate(k) = cp.CorrectRate;
end

You can plot the result e.g. using stem or plot

stem(1:25,correctRate);

PS: note that according to the MATLAB documentation, knnclassify will be removed in a future release and you should better use fitcknn.