Clustering

Clustering groups objects so that those in the same cluster are similar to each other and different from those in other clusters. There are no predefined classes, so it is unsupervised learning. Similarity is measured by a distance, usually the Euclidean distance d = √((x1 - y1)² + (x2 - y2)²).

K-Means Algorithm

K-Means is a partitioning method that divides the data into k clusters.

  1. Choose k objects as the initial centroids.
  2. Assign each object to the nearest centroid.
  3. Recalculate each centroid as the mean of its cluster.
  4. Repeat steps 2 and 3 until the clusters no longer change.

It minimises the sum of squared errors (SSE). It is fast, but k must be given, the result depends on the starting centroids, it is affected by outliers and it finds only round clusters.

Hierarchical Clustering

Hierarchical methods build a tree of clusters called a dendrogram.

  • Agglomerative (bottom-up): start with every object as its own cluster and repeatedly merge the two closest clusters.
  • Divisive (top-down): start with one cluster and repeatedly split it.

The closeness of clusters is defined by a linkage: single (minimum distance between members), complete (maximum) or average. Cutting the dendrogram at a chosen height gives the clusters. It needs no k, but it is costly for large data and a merge cannot be undone.

Definition: Centroid. The mean point of all the objects in a cluster, used as its centre in K-Means.

Table 2.4: K-Means and hierarchical clustering

Point K-Means Hierarchical
Type Partitioning Tree of clusters
Number of clusters Given in advance Chosen by cutting the tree
Speed Fast Slow on big data
Shape of clusters Round Depends on linkage
Result Can vary with start Same each time

Example 2.4: K-Means on six points

Problem: Cluster the points 2, 4, 6, 8, 20 and 22 into k = 2 clusters with initial centroids 2 and 8.

Solution:

  1. Iteration 1: 2 and 4 are nearer to 2; 6, 8, 20 and 22 are nearer to 8. New centroids: 3 and 14.
  2. Iteration 2: 2, 4, 6 and 8 are nearer to 3 (8 is 5 away from 3 but 6 from 14); 20 and 22 go to 14. New centroids: 5 and 21.
  3. Iteration 3: the same points are assigned, so the algorithm stops.
  4. SSE = (9 + 1 + 1 + 9) + (1 + 1) = 22.

Result: Clusters {2, 4, 6, 8} with centroid 5 and {20, 22} with centroid 21; SSE = 22.

pts = [2, 4, 6, 8, 20, 22]
cent = [2, 8]
while True:
    groups = [[], []]
    for p in pts:
        j = min(range(2), key=lambda i: abs(p - cent[i]))
        groups[j].append(p)
    new = [sum(g) / len(g) for g in groups]
    print("Clusters:", groups, "Centroids:", new)
    if new == cent:
        break
    cent = new
sse = sum((p - c) ** 2 for g, c in zip(groups, cent) for p in g)
print("SSE:", sse)
clusters = [[p] for p in pts]
merges = []
while len(clusters) > 1:
    best = min(((a, b) for a in range(len(clusters))
                for b in range(a + 1, len(clusters))),
               key=lambda ab: min(abs(x - y) for x in clusters[ab[0]]
                                  for y in clusters[ab[1]]))
    a, b = best
    d = min(abs(x - y) for x in clusters[a] for y in clusters[b])
    merges.append(d)
    clusters[a] += clusters.pop(b)
print("Single-link merge distances:", merges)

Output

Clusters: [[2, 4], [6, 8, 20, 22]] Centroids: [3.0, 14.0]
Clusters: [[2, 4, 6, 8], [20, 22]] Centroids: [5.0, 21.0]
Clusters: [[2, 4, 6, 8], [20, 22]] Centroids: [5.0, 21.0]
SSE: 22.0
Single-link merge distances: [2, 2, 2, 2, 12]

Common mistakes

  • Running K-Means on unscaled attributes, so one attribute dominates the distance.
  • Taking the result of one random start as final.
  • Using K-Means on data with many outliers or non-round clusters.
  • Believing that agglomerative clustering can undo a wrong merge.

Key takeaways

  • K-Means: choose centroids, assign, update, repeat.
  • It minimises SSE and needs k in advance.
  • Agglomerative clustering merges bottom-up; divisive splits top-down.
  • Linkage can be single, complete or average.

Practice questions

  1. What does K-Means minimise?
  2. What is a dendrogram?
  3. Give one drawback of K-Means.

Answers to the practice questions

  1. The sum of squared distances of points from their cluster centroids.
  2. A tree showing how clusters are merged or split.
  3. The number of clusters k must be known in advance.

Sources

  • Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit II