Concepts / Linkage Criteria in Agglomerative Clustering

Linkage Criteria in Agglomerative Clustering

Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two most similar clusters.

  • Programming

Starting From a Pile of Unlabeled Points

Suppose you have a pile of unlabeled data points and no idea how many natural groups they form. Clustering methods answer this kind of question by grouping points that resemble each other. Agglomerative clustering is one family of these methods, and its strategy is unusual. Instead of splitting one big group into smaller ones, it starts at the opposite extreme and builds larger groups up from the bottom.

Every algorithm in the agglomerative family starts in the same place: each data point is treated as a cluster containing only itself. From that starting arrangement, the algorithm repeatedly looks for the two clusters that are most similar to each other and merges them into one. After each merge there is one fewer cluster, and the process continues.

It helps to notice what the procedure never does. It does not start with one big cluster and cut it apart, and it does not need a guess about the final groups before it begins. The final grouping emerges from the sequence of merges themselves. This is why the word agglomerative, meaning gathering together, fits the method well.

Tracing the Merges From Single Points

The algorithm begins in a state where every point is a cluster of its own. In each step it finds the two clusters that are closest and merges them. The source traces this process on a small two-dimensional dataset. In the first four steps, two single-point clusters are picked and joined into two-point clusters. In step five, one of those two-point clusters picks up a third point. The process continues until nine steps have passed and only three clusters remain. Because three clusters were requested, the algorithm stops there.

Start: every pointaloneeach point is one clusterMerge 1two single points joinedMerge 2two more single pointsjoinedMerge 3 and 4still two-point clustersMerge 5a two-point cluster picksup a third pointMerges 6 to 8same rule applied to thecurrent clustersMerge 9last merge in the sourcetraceStop: three clustersrequested count reachedclosest pair mergedrepeatrepeatrepeatrepeatrepeatcount equals 3
After each merge, which clusters remain, and which pair is merged next, from every point as a singleton down to the final three groups?

Notice what the diagram leaves open. It says the two closest clusters are merged, but it does not say what closest means. The word closest is a placeholder until a linkage criterion gives it a precise meaning. Each merge depends on one question: which two clusters are close enough to join? The algorithm itself does not answer that question. A linkage criterion does, and that single choice changes which groups come out at the end.

Rule

The Stopping Criterion and the Number of Clusters

Merging cannot go on forever, so the algorithm needs a rule for when to halt. That rule is called the stopping criterion, and it is what ends the loop. The general idea is to keep merging the most similar pair of clusters until the stopping criterion is satisfied.

In scikit-learn, the implementation discussed in the source uses the number of clusters as its stopping criterion. You tell the algorithm how many clusters you want to end up with, and it keeps merging the most similar clusters until only that number is left. The number is therefore an input you supply, not something the algorithm discovers on its own.

Point 1starts aloneCluster Apoints 1 and 2 mergedAbove the cutmerges not performedPoint 2starts aloneCluster Bpoints 3 and 4 mergedPoint 3starts aloneCluster Cpoint 5 unmergedPoint 4starts alonePoint 5starts alonemergemergemergemergewould merge furtherwould merge furtherwould merge further
When n_clusters is set to 3, which merges happen before the cut, and how does the cut separate the final groups from the rest of the merge history?

The tree above is an illustration rather than output from the source. It shows the idea that the stopping criterion is a cut through the merge history. Merges below the cut are performed, and the clusters that exist at the cut are the final groups. Setting n_clusters to 3 places the cut where three clusters remain.

What a Linkage Criterion Compares

Definition

A linkage criterion is the rule that defines the distance, or similarity, between two clusters. It specifies exactly how the phrase most similar cluster is measured. The source stresses that several linkage criteria exist, and the meaning of most similar depends on which one you select.

An important detail is that a linkage criterion always compares two clusters that already exist. It is not a rule for comparing a single point to a single point in isolation at the moment of merging. Early in the process, clusters may still be single points, but the linkage rule still operates on pairs of current clusters. Each time a merge happens, the set of clusters changes, and the comparisons are made again on the new set.

Ward, Average, and Complete Side by Side

scikit-learn implements three choices of linkage criterion. The three options differ in how they define similarity between clusters, so the same data can produce different merge sequences depending on the one you pick. You select the criterion as a setting when you configure the model.

Ward minimizes the increase in variance that a merge causes. Average linkage uses the average distance between all points in the two clusters. Complete linkage uses the maximum distance between points in the two clusters. Each one looks at the points inside the two candidate clusters in a different way, so each can pick a different pair at the same step.

Wardminimizes the increase invarianceAverage linkageaverage distance betweenall pointsComplete linkagemaximum distance betweenpoints
Given two clusters, which summary of their point-to-point relationships does each criterion use, and how do those choices differ?

Choosing a Linkage Criterion by Size Balance

The source gives a practical default. Ward is the default and works on most datasets. Average or complete linkage may work better when cluster sizes differ greatly. The source's guidance is framed around the balance of cluster sizes, so that is the main factor to weigh when you choose. The source does not give further rules about cluster shape, so treat shape-based judgments as something to check on your own data rather than as settled guidance.

Linkage criterionHow closeness is measuredWhen the source suggests it
WardMinimizes the increase in varianceDefault; works on most datasets
AverageAverage distance between all pointsMay work better when cluster sizes differ greatly
CompleteMaximum distance between pointsMay work better when cluster sizes differ greatly

Choosing a linkage criterion according to the source's guidance

Running the Three-Cluster Program

The program below applies agglomerative clustering to a small two-dimensional dataset and asks for three clusters. The data is invented for illustration. The key settings are n_clusters, which is the stopping criterion, and linkage, which is the rule that defines closeness between clusters. The fit_predict call returns a cluster label for each training point.

python
Notes on the code
  • n_clusters=3 is the stopping criterion: merging continues until three clusters remain
  • linkage="ward" selects the Ward rule for measuring closeness between clusters
  • fit_predict runs the merging on the training data and returns one label per point

Changing linkage to average or complete in the same program keeps n_clusters at 3 but may produce a different grouping for the same data, because each criterion defines closeness differently. The labels printed are the final three groups after the merge sequence has run.

Where the Program Breaks

A common beginner error is to treat agglomerative clustering like a model that learns a rule and then labels new data. The source is explicit that agglomerative clustering has no predict method for new points. The correct approach is to use fit_predict on the training data to get cluster memberships.

What do you think happens?

If you add the line labels = model.predict(X_new) after fitting AgglomerativeClustering, which line fails, and what happens?

  • The fit_predict line fails because the model has no fit step

  • The predict line fails because AgglomerativeClustering has no predict method

  • Nothing fails, because predict is inherited from the clustering base class for every model

Reveal answer
Answer:

The predict line fails. AgglomerativeClustering has no predict method for new points, so calling model.predict raises an error at that line.

The source states that agglomerative clustering has no predict method for new points and recommends fit_predict on the training data. The failing line is the call to model.predict, not the fit_predict line. Calling a method that does not exist on an object causes an error at the exact line where that call appears.

  • Common mistake

    Assuming the number of clusters is discovered automatically

    What it looks like

    Expecting the algorithm to find the right number of groups without setting n_clusters

    Why it fails

    The source says the number of clusters is an input you supply as the stopping criterion, not something the algorithm discovers on its own.

    Do this instead

    Set n_clusters to the number of groups you want, or reason about the merge history to choose it.

  • Common mistake

    Thinking the linkage rule compares two single points only

    What it looks like

    Believing the linkage choice only matters for the first merge

    Why it fails

    A linkage criterion always compares two clusters that already exist, and the comparisons are made again after every merge.

    Do this instead

    Recognize that each merge changes the cluster set, so the linkage rule shapes every later step.

  • Common mistake

    Calling predict on the fitted model

    What it looks like
    labels = model.predict(X_new) after fitting an AgglomerativeClustering model
    Why it fails

    AgglomerativeClustering has no predict method for new points.

    Do this instead

    Use fit_predict on the training data to get the cluster memberships.

  • Common mistake

    Using the default linkage without checking whether cluster sizes differ greatly

Practice Predicting the Merge Count

EASY

A dataset has 12 points, and you run agglomerative clustering with n_clusters set to 3. Before you answer, predict how many merges the algorithm performs, and explain how you know.

Hints
  • Start from the state where every point is its own cluster.
  • Each merge reduces the cluster count by exactly one.
MEDIUM

Suppose a dataset has three clusters that are very different in size: one large group and two small ones. Which linkage criterion would you try first according to the source's guidance, and what would you check in the output to decide whether it was a good choice?

Hints
  • Look at the guidance about cluster sizes that differ greatly.
  • Compare the number of points in each final group against what you expect.

Key Takeaways

  • Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two closest clusters, so each merge reduces the cluster count by one.
  • In scikit-learn, the number of clusters you specify is the stopping criterion, so merging stops once that count is reached.
  • A linkage criterion defines closeness between two existing clusters, and it is applied again after every merge.
  • Ward minimizes the increase in variance and is the default; average and complete linkage use mean and maximum point distances and may work better when cluster sizes differ greatly.
  • Agglomerative clustering has no predict method, so use fit_predict on the training data, and a predict call on the model is the line where the program fails.

Related concepts

More concepts starting with L