Concepts / Choosing the Number of Clusters

Choosing the Number of Clusters

A linkage criterion is the rule that turns the similarity between two clusters into a single measure. It is applied at every merge step, and it is what decides which pair of clusters gets merged next.

  • Programming

Grouping Data Without a Target Count

Imagine you are handed a pile of data points and asked to sort them into groups, but nobody has told you how many groups to expect or what each group should look like. A natural first move is to ask which points belong together, and then to keep combining the most similar groups until the result is manageable. Agglomerative clustering is a family of algorithms built on exactly this idea. The word agglomerative refers to the act of gathering things together, and that is the direction the whole procedure moves in.

What do you think happens?

If there are no groups yet, how could a clustering procedure reasonably begin?

Reveal answer
Answer:

Every point is declared to be its own cluster. The procedure starts from a state with no grouping at all, and then merges.

Each algorithm in the agglomerative family begins with every point as its own cluster. So twenty data points give twenty clusters, each containing exactly one point. The merging comes only after that starting arrangement.

Every Point Starts Alone

Definition

Agglomerative clustering is a family of algorithms that starts with each point as its own cluster. Every algorithm in the family shares the same principles and the same starting arrangement.

Because the starting arrangement puts each point in its own cluster, nothing has been grouped yet. The number of clusters at the start equals the number of points. This is the baseline from which every later step departs. The procedure does not begin with a guess about the final groups; it begins with the finest possible arrangement and coarsens it one step at a time.

Tracing the Repeated Merge

From the starting arrangement, the algorithm repeats one operation. It finds the two most similar clusters that currently exist and merges them into a single cluster. Because a merge always reduces the number of clusters by exactly one, the count shrinks step by step. The procedure moves through a sequence of intermediate arrangements, each one slightly coarser than the one before. The diagram below shows the cluster count falling through repeated merges.

6 clustersstart: one per point5 clustersafter merge 14 clustersafter merge 23 clustersafter merge 32 clustersafter merge 4merge two most similarmerge two most similarmerge two most similarmerge two most similar
After starting with every point in its own cluster, which two clusters get merged at each step, and how does the cluster count change with each merge? Each merge removes exactly one cluster from the count.
Worked example

Six Points Merged Down to Two Clusters

Six points A, B, C, D, E, and F are clustered agglomeratively, with the target set to two clusters. Trace the cluster count through the merges. The pairs chosen at each step are assumed here for illustration; they are not measured values.

  1. 1
    Starting state

    Each point is its own cluster: {A}, {B}, {C}, {D}, {E}, {F}. That is 6 clusters.

  2. 2
    Merge 1

    The two most similar clusters are {A} and {B}. They merge into {A, B}. The count drops from 6 to 5.

  3. 3
    Merge 2

    The next most similar pair is {C} and {D}. They merge into {C, D}. The count drops from 5 to 4.

  4. 4
    Merge 3

    Now {A, B} and {E} are the most similar existing pair, so they merge into {A, B, E}. The count drops from 4 to 3. This comparison uses a cluster that was formed earlier, not an original point.

  5. 5
    Merge 4 and stop

    {C, D} and {F} merge into {C, D, F}. The count is now 2, which equals the target, so merging ends.

Result

The final clustering is {A, B, E} and {C, D, F}. Four merges took the count from 6 down to the target of 2.

Why Linkage Decides Which Pair Merges

The phrase two most similar clusters hides a real choice. Points have a natural similarity to one another, but a cluster is a group, so it is not immediately obvious how to say how similar a group is to another group. The source states that there are several linkage criteria, and that each one specifies exactly how the most similar cluster is measured.

Key point

A linkage criterion is the rule that turns the similarity between two clusters into a single measure. It is applied at every merge step, and it is what decides which pair of clusters gets merged next.

The measure of similarity is always defined between two existing clusters. The algorithm never asks how similar a point is to some abstract group; at every merge step it compares a specific pair of clusters that actually exist at that moment. As merges happen, the clusters that exist change, so the comparisons available at later steps can differ from those at the start. This is why the third merge in the example above could compare a cluster containing two points against a single point: both were real clusters at that moment.

Where the Stopping Criterion Ends Merging

Merging cannot go on forever, so the algorithm needs a rule for when to stop. In general terms, the algorithm merges the two most similar clusters until some stopping criterion is satisfied. In scikit-learn, the stopping criterion is the number of clusters. The user specifies a target number of clusters, and similar clusters are merged until only that number of clusters remain.

Current countequals target?checked after each mergeMerge most similarpaircount drops by oneStop mergingtarget number remainsno, count is above targetrecheck new countyes
Starting from n clusters and merging down, at what point does the process stop, and how does the cluster count move from n to the specified number? The stopping condition is checked against the current cluster count after each merge.

Read the loop as a cycle with an exit. The algorithm keeps returning to the search for the most similar pair until the cluster count matches the stopping condition, and only then does it stop. The stopping condition is checked against the current state, not against the original points. The target number of clusters therefore has no effect on which pairs merge; it only controls when the loop ends.

Starting State Versus Final State

Comparing the arrangement before any merging with the arrangement after merging stops shows what the procedure actually produced. In the six-point example, the start has six single-point clusters and the end has two clusters, each holding several points. The diagram marks which points ended up grouped together.

Aits own cluster{A, B, E}one final clusterBits own cluster{C, D, F}one final clusterCits own clusterDits own clusterEits own clusterFits own cluster
How does the set of clusters look before any merging versus after merging stops, and which points ended up grouped together? Points A, B, and E end in one cluster, and C, D, and F end in the other.

Mistakes Learners Make With Cluster Counts

  • Common mistake

    Thinking the target number of clusters decides which points get merged

    What it looks like

    Setting the target to two and expecting the algorithm to aim for particular groups from the start.

    Why it fails

    The algorithm never looks at the target until it is time to decide whether to stop. Which pair merges at each step is decided by the linkage criterion applied to existing clusters.

    Do this instead

    Treat the target as the stopping condition only. The merge choices come from comparing clusters at each step.

  • Common mistake

    Assuming each merge can compare a point against any group

    What it looks like

    Expecting a comparison between a single point and a group that has not been formed yet.

    Why it fails

    The similarity measure is always defined between two clusters that actually exist at that moment.

    Do this instead

    At each step, check which clusters currently exist, then compare only that specific pair.

  • Common mistake

    Expecting a merge to change the count by more than one

    What it looks like

    Believing that merging several clusters at once reduces the count from 6 to 3 in a single step.

    Why it fails

    Each merge combines exactly two clusters into one, so the count drops by exactly one per merge.

    Do this instead

    Count the merges: going from 6 clusters to 2 requires four merges.

  • Common mistake

    Confusing the starting state with an initial guess at the final groups

Predicting the Merge Sequence

EASY

Eight points, P1 through P8, are clustered agglomeratively with the target set to three clusters. Before merging begins, how many clusters exist? After how many merges does the process stop, and why? Explain why the linkage criterion matters even though the final count is fixed.

Hints
  • The starting state puts every point in its own cluster.
  • Each merge reduces the cluster count by exactly one.
  • The stopping criterion is checked against the current cluster count after each merge.
  • Linkage decides which existing pair is merged at each step, which can change the groups even when the count is the same.

Key Takeaways

  • Agglomerative clustering starts with every point as its own cluster, so the initial cluster count equals the number of points.
  • At each step the algorithm merges the two most similar existing clusters, reducing the cluster count by exactly one.
  • A linkage criterion defines how the similarity between two clusters is measured, and it is applied to pairs of existing clusters at every merge.
  • In scikit-learn the stopping criterion is the number of clusters: merging continues until the user-specified target number remains.
  • The target count controls only when the loop ends, while linkage controls which pair merges at each step.

Related concepts

More concepts starting with C