1

I keep getting the same error during knn analysis of a 1-dimensional data set. The function is presented below:

def KNN(data,legend):

    importr('class') #package for knn object

    print data #both DataFrame objects
    print legend

    KNN_result=robjects.r('knn')(data,data,legend,1) #k=1

The output as follows (firstly: data):

    attribute
1               0
2               0
3               1

...

1635            0
1636            1

And now the legend:

   [1] A A A A B B B B  

...

[1633] A A A A
Levels: A B

The error:

Error in function (train, test, cl, k = 1, l = 0, prob = FALSE, use.all = TRUE)  : 
  too many ties in knn

Can anybody please help ? Importantly -if the data are not binary, but range from 0 to 1 as float numbers the error does not appear and i can analyze the data correctly.

maciek
  • 1,807
  • 2
  • 18
  • 30
  • 1
    Well, since for float numbers from 0 to 1 it works correctly, I suppose that the problem is with discrete attribute - it has only two values. When algorithm looks for neighbours of `0` it gets all `0`, but there are too many of them. Try to change `use.all` argument to `FALSE`, see manual for explanation https://stat.ethz.ch/R-manual/R-devel/library/class/html/knn.html Also - quick google search shows this - https://stat.ethz.ch/pipermail/r-help/2011-September/289517.html – BartekCh Apr 07 '14 at 08:12
  • 1
    Thank you very much. I have seen these links before, but I did not understood the idea of use.all argument. I had to reinstall class package manually for R and increase MAX_TIES (as described). Works as charm! :) – maciek Apr 09 '14 at 11:50

0 Answers0