Hierarchical Clustering and Dendrograms

Merge the two closest clusters, repeat, and keep the whole tree. Choose how many clusters you want afterwards.

Hierarchical Clustering and Dendrograms

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Start with every point as its own cluster. Repeatedly merge the two closest, until one remains.
The result is a tree. Cutting it at a height gives a clustering — and every height gives a different k.
Linkage defines the distance between two clusters, and changes the answer more than any other choice.
Single linkage chains through bridges; complete linkage insists on tight clusters; average sits between them.

Overview

Begin with each point as its own cluster. At each step, merge the two clusters considered closest by the chosen linkage rule. Continue until all points form one cluster. A dendrogram draws each merge at its distance; branches join higher when the merge required a larger distance.

In this guide
  1. How agglomerative clustering builds a tree
  2. Linkage defines “closest”
  3. Worked example: read a cut
  4. Limits and practical choices
  5. Sources and next steps

Unlike k-means, the method does not require a cluster count before building the hierarchy. Afterward, a horizontal cut through the dendrogram determines the number of groups. A large vertical gap can suggest a useful cut, but it is evidence to inspect, not proof of a natural number of clusters.

Linkage defines “closest”

  • Single: minimum distance between any pair across the two clusters. It can connect elongated groups, but a chain of nearby points may join otherwise distinct groups.
  • Complete: maximum distance between cross-cluster pairs. It tends to form compact groups, but one distant point can delay a merge.
  • Average: mean cross-cluster distance. It is a compromise between the two definitions.
  • Ward: merges the pair that produces the smallest increase in within-cluster squared error. It favors compact groups and is available only with compatible Euclidean distances.

Try each available choice in Linkage. Then move Cut height and watch the flat cluster count change while the merge tree stays the same. The visualizer offers Single, Complete, and Average; Ward is described here for comparison.

Worked example: read a cut

Suppose two tight groups are far apart. Merges within each group occur at small heights; the final merge between groups occurs much higher. A cut between those heights returns two clusters. If instead the dendrogram has no clear gap, the data may not support a compelling cluster count, or the chosen distance and linkage may not match the structure.

The left-to-right order of leaves is arranged for drawing and does not rank similarity. Read branch joins and heights, not which leaves happen to appear next to each other.

Limits and practical choices

Agglomerative merges are greedy and irreversible: an early merge remains in every higher level of the tree. Distance-based results are sensitive to feature scale, so scale features when units differ. Many implementations require pairwise-distance storage that grows quadratically with the number of observations, limiting the method on large datasets.

Choose linkage and distance based on the shape and meaning of the data, and validate any selected clusters against domain knowledge or a downstream task. Hierarchical clustering can show structure at several resolutions; it does not prove that the data contains discrete natural groups.

Sources and next steps

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is the advantage of building the whole tree?

  2. What is chaining, and which linkage causes it?

  3. Why is hierarchical clustering impractical for large datasets?

Cheat sheet

Hierarchical Clustering and Dendrograms

Build a clustering tree from pairwise distances and read a dendrogram to choose the number of clusters after the algorithm finishes.

MACHINE LEARNING · vizlearn.in/machine_learning/hierarchical_clustering.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.