# demonstrate k-means with iris data
# keep the relevant columns
iris2 <- iris[, c("Sepal.Length", "Petal.Length")]
# initialise the cluster centres
clust <- init_rand_centers(iris2, n = 3, seed = 2435)
# plot the data with the cluster centres
library(ggplot2)
ggplot(iris2, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(data = clust$centers, aes(colour = factor(1:3)),
shape = 18, size = 6) +
geom_point() +
scale_colour_brewer(palette = "Set1")
# assign clusters and compute new centres
clust_new <- cluster_with_centers(iris2, clust$centers)
# plot the data with clustering
clust$cluster <- clust_new$cluster
voronoi_diagram(clust, x = "Sepal.Length", y = "Petal.Length",
data = iris2)
# plot the data with new cluster centres
clust$centers <- clust_new$centers
voronoi_diagram(clust, x = "Sepal.Length", y = "Petal.Length",
data = iris2, colour_data = FALSE)
# this procedure may be repeated until the algorithm converges
Run the code above in your browser using DataLab