###################################################
###################################################
set.seed(256)
data(iris)
str(iris)
#Remove the initial label of Species from original dataset
iris2 <- iris[,-5]
iris2
?kmeans
kmeans.result <- kmeans(iris2, centers = 3, nstart = 20)
kmeans.result
kmeans.result$cluster
table(iris$Species, kmeans.result$cluster)
plot(iris2[c("Sepal.Length", "Sepal.Width")])
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster)
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster, main = "K-Means with 3 clusters")
kmeans.result$centers
kmeans.result$centers[,c("Sepal.Length", "Sepal.Width")]
points(kmeans.result$centers[,c("Sepal.Length", "Sepal.Width")], col = 1:3,pch = 8, cex=3)#cex = font size, pch = symbol
library(cluster)
y_kmeans <- kmeans.result$cluster
?clusplot()
clusplot(iris2[,c("Sepal.Length", "Sepal.Width")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
main = paste('Clusters of iris'),
xlab = 'Sepal.Length',
ylab = 'Sepal.Width')
kmeans.result$tot.withinss
set.seed(6)
wcss = vector()
for (i in 1:10) wcss[i] = sum(kmeans(iris2, centers = i,nstart = 20)$withinss)
plot(1:10,
wcss,
type = "b",
main = paste('The Elbow Method'),
xlab = "Number of Clusters",
ylab = "WCSS")
k <-3
set.seed(29)
kmeans.result <- kmeans(iris2, centers = 3, nstart = 20)
y_kmeans = kmeans.result$cluster