X <- matrix(nrow = 10, ncol = 2, byrow = TRUE, c(
2.33, 4.66,
2.96, 4.10,
3.71, 1.77,
1.29, 3.20,
1.73, 1.14,
1.37, 4.32,
2.32, 1.38,
2.38, 2.52,
2.74, 2.18,
2.84, 2.66
))
plot(X, pch = 16)
From An Introduction to Statistical Learning with Applications in R by Drs. Gareth James, Daniela Witten, Trevor Hastie, and Rob Tibshirani:
The book is freely available at https://www.statlearning.com/
Also, check out the following videos:
k-Means Clustering
Clustering Methods
Recall that classification aims to assign an observation to known category or class. On the other hand, clustering aims to find smaller groups of similar observations.
The notion of similarity is largely defined by the practitioners rather than known labels. Thus, clustering is said to be unsupervised.


Recall that the Euclidean distance is a popular distance measure between two numerical observations \((x_1, x_2, \dots, x_p)\) and \((y_1, y_2, \dots, y_p)\)
\[\sqrt{ (x_1 - y_1)^2 + (x_2 - y_2)^2 + \dots + (x_p - y_p)^2 }\]
\(k\)-means clustering is done as follows:
Consider the following data set that we want to find groups of similar observations with \(k\)-means clustering.












X <- matrix(nrow = 10, ncol = 2, byrow = TRUE, c(
2.33, 4.66,
2.96, 4.10,
3.71, 1.77,
1.29, 3.20,
1.73, 1.14,
1.37, 4.32,
2.32, 1.38,
2.38, 2.52,
2.74, 2.18,
2.84, 2.66
))
plot(X, pch = 16)
kmeans() can be used for clustering. Note that since \(k\)-means clustering involves random initialization, to make the solution more stable, we can specify a higher number of random sets to start.km <- kmeans(X, centers = 3, nstart = 5)
plot(X, pch = 16, col = km$cluster)
faithful data set that records waiting time between eruptions and the duration of the eruption for the Old Faithful geyser in Yellowstone National Park, Wyoming, USA.plot(faithful, pch = 16)
library(cluster)
km2 <- kmeans(faithful, centers = 2, nstart = 5)
plot(faithful, pch = 16, col = km2$cluster)
km3 <- kmeans(faithful, centers = 3, nstart = 5)
plot(faithful, pch = 16, col = km3$cluster)
km4 <- kmeans(faithful, centers = 4, nstart = 5)
plot(faithful, pch = 16, col = km4$cluster)
km5 <- kmeans(faithful, centers = 5, nstart = 5)
plot(faithful, pch = 16, col = km5$cluster)
D <- dist(faithful)
sw2 <- silhouette(km2$cluster, D)
sw3 <- silhouette(km3$cluster, D)
sw4 <- silhouette(km4$cluster, D)
sw5 <- silhouette(km5$cluster, D)
plot(sw2)
plot(sw3)
plot(sw4)
plot(sw5)
### Because with k = 2, the average Silhouette width is the highest (0.72), k = 2 is the optimal number of clusters.💻 Hands-On
Perform \(k\)-means on the Violent Crime Rates by US State in 1973 (USArrests).
library(cluster)
km2 <- kmeans(USArrests, centers = 2, nstart = 5)
km3 <- kmeans(USArrests, centers = 3, nstart = 5)
km4 <- kmeans(USArrests, centers = 4, nstart = 5)
km5 <- kmeans(USArrests, centers = 5, nstart = 5)
D <- dist(USArrests)
sw2 <- silhouette(km2$cluster, D)
sw3 <- silhouette(km3$cluster, D)
sw4 <- silhouette(km4$cluster, D)
sw5 <- silhouette(km5$cluster, D)
plot(sw2)
plot(sw3)
plot(sw4)
plot(sw5)
# k = 2 seems to be optimal according to the average Silhouette width