0
$\begingroup$

I have a knn classifier that finds the k nearest neighbors of the given data. While classification I am not able to handle ties. I want to handle ties in the following way:

If there is only one class and it is correct class accuracy is 1

If there is only one class and it is incorrect class accuracy is 0

If there is more than one class tied for best and the correct class is one of those then accuracy = 1/no_of_classes_tied_for_best.

How do I do this in Matlab?

Code till now: test has my knnclassify results for 5 neighbors

classes_test has my verification class.

for i = 1:size(test,1)
   set = 0;
   for j = 1:size(test,2)
      if test(i,j) == classes_test(i,1) && set~=1
          set = 1;
          accuracy = accuracy + 1;
      end
   end
end
$\endgroup$
  • $\begingroup$ I don't see how your plan handles ties. Maybe you can edit the post and make it more clear. $\endgroup$ – Michael Chernick Apr 14 '17 at 18:05
  • $\begingroup$ That is what I am not able to figure out $\endgroup$ – Kobe00992 Apr 14 '17 at 18:35
1
$\begingroup$

When using kNN, $k$ should always be set to odd numbers in order to avoid ties (3,5,7,9,11...). This is standard knowledge in machine learning.

$\endgroup$
  • $\begingroup$ Even when using k = 3, I get ties with data. I have instances where the 3 nearest neighbors are from 3 distinct classes. and when k = 5, I got neighbors with 2 distinct classes. Ex: [1 2 3] and [1 2 2 3 3] are the classes of 3 and 5 nearest neighbors respectively. $\endgroup$ – Kobe00992 Apr 17 '17 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.