Search This Blog

Thursday, 15 June 2017

(R language) k-means clustering

#k-means clustering
library(cluster)

#Read the dataset iris
iris2 <- iris

#Remove Species column for cluster
iris2$Species <- NULL



#Apply k-means, vectors, components
(kmeans.result<- kmeans(iris2, 3))

#table species separation result
table(iris$Species, kmeans.result$cluster)

#Apply the k-means clustering
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster)

# plot cluster centers
points(kmeans.result$centers[,c("Sepal.Length","Sepal.Width")],col=1:3,pch=8,cex=2)



No comments:

Post a Comment