Skip to content

✍ 05: Clustering — Exercise

Data Analytics

Chapter 05: Clustering 3 questions

Home Notes All Exercises Quiz

Tip

Practice — try each question first, then expand the answer to check your reasoning.

Dataset (a "social network" sample, Age & Educational level):

Name Age Educational level
Andrew (A) 55 1
Bernhard (B) 43 2
Carolina (C) 37 5
Dennis (D) 82 3
Eve (E) 23 3.2
Fred (F) 46 5

Run k-means with k = 2, using Andrew and Carolina as the initial centroids.


📈 Q1. Assign each point to its nearest centroid (Euclidean distance) for the first iteration.

Show answer Centroid 1 = A = (55, 1); Centroid 2 = C = (37, 5) | Point | Dist to A | Dist to C | Assigned | | --- | --- | --- | --- | | A (55,1) | 0 | 17.2 | Cluster 1 | | B (43,2) | 12.0 | 6.7 | Cluster 2 | | C (37,5) | 17.2 | 0 | Cluster 2 | | D (82,3) | 27.1 | 45.2 | Cluster 1 | | E (23,3.2) | 32.4 | 14.0 | Cluster 2 | | F (46,5) | 9.85 | 9.0 | Cluster 2 | **Cluster 1** = {A, D}; **Cluster 2** = {B, C, E, F}. Each distance uses d = √((x₁−x₂)² + (y₁−y₂)²).

🔄 Q2. Recompute the centroids of both clusters after this first assignment.

Show answer **Cluster 1** {A(55,1), D(82,3)}: Age = (55+82)/2 = 68.5, Edu = (1+3)/2 = 2 → **New centroid 1 = (68.5, 2)** **Cluster 2** {B(43,2), C(37,5), E(23,3.2), F(46,5)}: Age = (43+37+23+46)/4 = 37.25, Edu = (2+5+3.2+5)/4 = 3.8 → **New centroid 2 = (37.25, 3.8)** Notice both centroids moved away from their original seed points (Andrew, Carolina) toward the center of mass of their assigned members — this is exactly what the "means" in k-means recomputes each iteration.

📊 Q3. In R, how would you compute the distances, assign clusters, and plot the result with the recomputed centroids?

Show answer
table_data <- data.frame(
  Name = c("Andrew","Bernhard","Carolina","Dennis","Eve","Fred"),
  Age  = c(55, 43, 37, 82, 23, 46),
  Edu  = c(1, 2, 5, 3, 3.2, 5))

centroid1 <- c(55, 1); centroid2 <- c(37, 5)
table_data$dist_to_A <- sqrt((table_data$Age-centroid1[1])^2 + (table_data$Edu-centroid1[2])^2)
table_data$dist_to_C <- sqrt((table_data$Age-centroid2[1])^2 + (table_data$Edu-centroid2[2])^2)
table_data$Cluster <- ifelse(table_data$dist_to_A < table_data$dist_to_C, 1, 2)

new_centroids <- aggregate(cbind(Age, Edu) ~ Cluster, data = table_data, FUN = mean)

plot(table_data$Age, table_data$Edu,
     col = ifelse(table_data$Cluster == 1, "blue", "red"), pch = 16,
     xlab = "Age", ylab = "Educational Level", main = "K-means: First Iteration")
text(table_data$Age, table_data$Edu, labels = table_data$Name, pos = 3)
points(new_centroids$Age, new_centroids$Edu, col = c("blue","red"), pch = 8, cex = 1)
A real run would repeat the distance/assign/recompute loop until the assignments stop changing (convergence) — this exercise only walks through iteration 1 by hand.

📚 All Exercises · Next: Chapter 06 — Frequent Pattern Mining ➡️