[R] Question about knn

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 11 10:43:12 CEST 2008


Try reading the help page (and crediting the authors of package class).

Description:

      k-nearest neighbour classification for test set from training set.
      For each row of the test set, the 'k' nearest (in Euclidean
      distance) training set vectors are found, and the classification
      is decided by majority vote, with ties broken at random. If there
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
      are ties for the 'k'th nearest vector, all candidates are included
      in the vote.

So the result is random (as documente), not 'unstable'

What you are trying is nonsensical: you have all the points of different 
classes, so the 4 nearest must be of 4 different classes, and furthermore 
there are ties on distances.

On Mon, 11 Aug 2008, rami batal wrote:

> Hello all,
>
> am a newby in R,
>
> am trying the knn function, and am doing just a stupid test :
>
> knn(c(1,2,3,4,5,6), c(3), k=4 ,prob=TRUE,factor(c(1:6)))
>
> the result is unstable !! i have each time different result :
>
> [1] 5
> attr(,"prob")
> [1] 0.1666667
> Levels: 1 2 3 4 5 6
>
> [1] 4
> attr(,"prob")
> [1] 0.1666667
> Levels: 1 2 3 4 5 6
>
> [1] 1
> attr(,"prob")
> [1] 0.1666667
> Levels: 1 2 3 4 5 6
>
> ect...
>
> I would like to know why ?
>
> the thing that am searching for is to have the 4 NN and sort them according
> to there
> distance to the test point.
>
> Tank you
>
> -- 
> Rami
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list