0

I have some trouble on predicting KNN classifier without using built-in function. I got stuck here and had no idea how to go to next step. Here is my code:

 % calculate Euclidean distance
 dist = pdist2(test, train, 'euclidean');
 for k = [1 3 5 7]
    [~, nearest] = sort(dist, 2);
    nearst = nearest(:, 1:k);
end % for loop

Where test is a 297x64 matrix, and train is a 1500x64 matrix. The dist matrix is 297x1500. Any help will be thankful!

BigD
  • 571
  • 7
  • 24

1 Answers1

1

So you managed to get sorted indices in terms of distances in your nearst, all you have to do is to refer to your labels of the original data. So you have somewhere a variable labels which holds a true label for each point. Use indices stored in nearst to read them out and simply report the most common value.

lejlot
  • 64,777
  • 8
  • 131
  • 164