# Visualizing k-nearest neighbour?

Using R plot() and plotcp() methods, we can visualize linear regression model (lm) as an equation and decision tree model (rpart) as a tree. We can develop k-nearest neighbour model using R kknn() method, but I don't know how to present this model. Please suggest me some R methods that produce nice graphs for knn model visualization.

• Take a look here: asa.1gb.ru/kmeans/1.html – Alex Jul 26 '11 at 22:27
• @Alex This is k-means clustering, not kNN. – user88 Jul 26 '11 at 22:37
• How many variables/features/predictors do you have? If you have two predictors, then you can just sample a grid and do predictions from your model on the grid points, then you can plot these points in different colors based on the predictions. If you have more variables, then there is not an easy way to do this. – Gumeo Jul 30 '15 at 10:57

More to the point, the package you mentioned, kknn, has built-in functionality for plot() for many of its functions, and you should browse the vignette, which contains several examples.
You may do this by counting the distances between train objects the way you did it in kknn, then use cmdscale to cast this on 2D, finally plot directly or using some smoothed scatterplot using colours to show classes or values (the smoothed regression version would require probably some hacking with hue and intensity). However, as I wrote, this would be probably a totally useless plot.