user5928466
user5928466

Reputation: 81

how to plot KNN clusters boundaries in r

I am using iris data for K- nearest neighbour. I have replaced species type with numerical values in data i.e

setosa = 1
versicolor = 2
virginica = 3 

now I am diving my data into training and testing set . And training this model on the basis of species colmum.

# Clustering
WNew <- iris


# Knn Clustering Technique

library(class)
library(gmodels)
WNew[is.na(WNew)] <- 0
WSmallSet<-WNew[1:100,]
WTestSet<-WNew[100:150,] # testing set
WLabel<-c(WNew[1:100,5]) # training set
wTestLabel<-c(WNew[100:150,5])
kWset1 <- knnSet <- knn(WSmallSet,WTestSet,WLabel,k=3)
CTab<-CrossTable(x = wTestLabel, y = kWset1,prop.chisq=FALSE)

now I want to plot this 3 clusters boundaries on the basis of their boundaries. but i dont know how to do this . Can anyone help me with this.??

Upvotes: 2

Views: 13095

Answers (1)

royr2
royr2

Reputation: 2289

I'll try and answer this as best as I can. The example below works when you'd like to visualize the clusters using a 2D scatter plot. You could extrapolate this to 3D a well but for multidimensional data sets, maybe use pairwise scatter plots?

Note that I didn't use your code as is but I am still using the iris data set. I did this so as to not hard code row indices.

Hope this helps in some way.

library(plyr)
library(ggplot2)
set.seed(123)

# Create training and testing data sets
idx = sample(1:nrow(iris), size = 100)
train.idx = 1:nrow(iris) %in% idx
test.idx =  ! 1:nrow(iris) %in% idx

train = iris[train.idx, 1:4]
test = iris[test.idx, 1:4]

# Get labels
labels = iris[train.idx, 5]

# Do knn
fit = knn(train, test, labels)
fit

# Create a dataframe to simplify charting
plot.df = data.frame(test, predicted = fit)

# Use ggplot
# 2-D plots example only
# Sepal.Length vs Sepal.Width

# First use Convex hull to determine boundary points of each cluster
plot.df1 = data.frame(x = plot.df$Sepal.Length, 
                      y = plot.df$Sepal.Width, 
                      predicted = plot.df$predicted)

find_hull = function(df) df[chull(df$x, df$y), ]
boundary = ddply(plot.df1, .variables = "predicted", .fun = find_hull)

ggplot(plot.df, aes(Sepal.Length, Sepal.Width, color = predicted, fill = predicted)) + 
  geom_point(size = 5) + 
  geom_polygon(data = boundary, aes(x,y), alpha = 0.5)

Upvotes: 3

Related Questions