Skip to content

Clustering, density estimation, and anomaly detection · Hierarchical clustering · UCL015 · Algorithm

Agglomerative hierarchical clustering

Build nested clusters by repeatedly merging the closest pair. Single, complete, average, and Ward linkage belong to this one algorithm family rather than inflating the catalog with every linkage setting.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.hierarchical. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Build nested clusters by repeatedly merging the closest pair. Single, complete, average, and Ward linkage belong to this one algorithm family rather than inflating the catalog with every linkage setting.

Question / hypotheses

Which nested divisions or merges summarize the specified dissimilarity structure? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

A merge tree built from pairwise cluster dissimilarities; the final cut produces labels.

Assumptions

Ward requires Euclidean geometry for its variance interpretation. Single linkage chains through bridges; complete linkage favors compact diameter. Early merges cannot be reversed and dense distance storage can be expensive.

Formula / computational definition

Single linkage uses min⁡i∈A,j∈Bdij\min_{i\in A,j\in B}d_{ij}; complete uses the maximum; average uses the mean. Ward chooses the smallest increase Δ=∣A∣∣B∣∥xˉA−xˉB∥2/(∣A∣+∣B∣)\Delta=|A||B|\|\bar x_A-\bar x_B\|^2/(|A|+|B|) in within-cluster squared error. These definitions produce different trees rather than interchangeable estimates of one truth.

Parameters and notation

Learned or fitted quantities: Merge ordering/heights and a selected partition. User-set hyperparameters/design choices: Choose distance, linkage, optional connectivity constraints, and a cut height or number of groups. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable. A changed distance or linkage generally requires rebuilding the hierarchy. A cut can be changed without rebuilding the tree; assigning new cases to a fixed tree needs a separately stated rule and is not identical to refitting.

How to use and calibrate

Inspect the dendrogram, merge heights, cophenetic agreement with original distances, and bootstrap stability. Large merge gaps are exploratory evidence, not automatically significance tests.

Example application

Explore nested relationships among ecological samples. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

Use sklearn.cluster.AgglomerativeClustering or SciPy hierarchy; R provides stats::hclust and cluster::agnes. Ensure Ward conventions and distance squaring match across languages before interpreting different heights.

Component: Ward merge cost. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Ward merge cost. · needs numpy

import numpy as np
a=np.array([0.,1.]); b=np.array([4.,5.,6.])
result=len(a)*len(b)/(len(a)+len(b))*(a.mean()-b.mean())**2
print(result)
R

Component demo, not a trained model — Ward merge cost.

a=c(0,1); b=c(4,5,6)
result=length(a)*length(b)/(length(a)+length(b))*(mean(a)-mean(b))^2
print(result)

Sources

From the CodeSplash reference, entry UCL015.