Hierarchical clustering builds a tree of clusters and does not need k to be fixed beforehand. The tree is drawn as a dendrogram.

Agglomerative Method

The common agglomerative (bottom-up) approach starts with every instance in a cluster of its own. At each step it finds the two closest clusters and merges them, until only one cluster remains. The opposite, divisive (top-down) method starts with one cluster and splits it. A distance matrix between points is computed first and updated after each merge.

Linkage: Distance Between Clusters

How is the distance between two clusters defined? That is the linkage rule.

  1. Single linkage: the smallest distance between any two points of the two clusters. It can chain clusters together.
  2. Complete linkage: the largest distance between any two points. It gives compact clusters.
  3. Average linkage: the mean of all pairwise distances.
  4. Ward's method: merges the pair that increases the within-cluster variance the least.

Reading the Dendrogram

The leaves are the instances and the height of each join is the distance at which the clusters were merged. Cutting the dendrogram with a horizontal line gives a flat clustering: the number of branches crossed is the number of clusters. A long vertical stretch without merges suggests a natural place to cut. Hierarchical clustering costs at least O(n²) time and memory, so it suits small and medium data.

Definition: Dendrogram. A tree diagram that shows the order and distances at which clusters are merged in hierarchical clustering; cutting it at a chosen height gives a set of clusters.

Table 4.3: Linkage methods

Linkage Cluster distance Tendency
Single Minimum pairwise distance Long, chained clusters
Complete Maximum pairwise distance Compact clusters
Average Mean pairwise distance Between the two
Ward Smallest rise in variance Even-sized clusters

Example 4.3: Single-linkage merging

Problem: Cluster the points 1, 2, 6, 8, 15 by agglomerative clustering with single linkage and list the merges.

Solution:

  1. The closest pair is (1, 2) at distance 1; merge them.
  2. The next closest pair is (6, 8) at distance 2; merge them.
  3. The distance between {1, 2} and {6, 8} is the smallest pairwise distance, 6 − 2 = 4; the distance from 15 to {6, 8} is 15 − 8 = 7. Merge {1, 2} with {6, 8} at 4.
  4. Finally 15 joins the group at distance 7.

Result: Merges at distances 1, 2, 4 and 7. Cutting between 4 and 7 gives two clusters, {1, 2, 6, 8} and {15}.

from scipy.cluster.hierarchy import linkage, fcluster
P = [[1], [2], [6], [8], [15]]
for method in ("single", "complete"):
    Z = linkage(P, method)
    print(method, "merge heights:", [float(v) for v in Z[:, 2]])
Z = linkage(P, "single")
print("cut at 5:", fcluster(Z, t=5, criterion="distance").tolist())

Output

single merge heights: [1.0, 2.0, 4.0, 7.0]
complete merge heights: [1.0, 2.0, 7.0, 14.0]
cut at 5: [1, 1, 1, 1, 2]

Common mistakes

  • Believing the number of clusters must be fixed before the algorithm starts.
  • Using single linkage and being surprised by long chains.
  • Forgetting that the height of a join is the merge distance.
  • Using hierarchical clustering on very large datasets.

Key takeaways

  • Agglomerative: start with singletons and merge the closest pair repeatedly.
  • Linkage defines cluster distance: single, complete, average, Ward.
  • Cut the dendrogram to get a chosen number of clusters.
  • Cost is at least O(n²), so it suits small data.

Practice questions

  1. What is a dendrogram?
  2. How does single linkage define cluster distance?
  3. How do you obtain k clusters from a dendrogram?

Answers to the practice questions

  1. A tree showing the order and distances of cluster merges.
  2. As the minimum distance between points of the two clusters.
  3. Cut it at a height that crosses k branches.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit IV