In pattern recognition or grouping, the k-nearest neighbors (k-NN) algorithm is a non-parametric method implemented for classification and regression. For those two cases, the input consists of the k-closest training examples in the feature space. The following four lines of R code tries to separate plants into k-groups by using a dataset called iris:
library(ggvis) x<-ggvis y<-layer_points iris %>% x(~Petal.Length,~Petal.Width,fill=~Species) %>% y()
The graph is shown here:
The following diagram shows the five nearest neighbors:
The code generated by the five nearest neighbors is given here. The code is ...