Audit distance, linkage and cut height before labeling clusters from a hierarchical tree.
Dendrograms: read merge height without overreading leaf order
A tree is built from a distance rule
A dendrogram shows a sequence of cluster merges. Its shape depends first on what distance means: Euclidean distance on raw fields lets large-unit features dominate, while a domain-specific distance may weight errors or missingness differently. Standardize only when that operation fits the question and record the fitted scaling parameters. Then choose a linkage rule. Single linkage merges by the nearest pair of members and can chain points; complete linkage uses the farthest pair; average linkage uses the mean of pair distances. These are different cluster definitions, not styling options. Correlation heatmaps often sit next to a dendrogram, but the correlation-to-distance transformation must be stated if it creates the tree.
Read height, not horizontal spacing
The vertical coordinate of a merge records the linkage distance at which its child clusters join. Horizontal leaf placement is usually a layout choice: sibling branches can be rotated without changing any membership or merge distance. Do not infer that two neighboring leaf labels are the closest original pair merely because they appear adjacent. Put the distance metric and linkage rule in the caption, and label the merge-height axis with units or a dimensionless scale. A flat cut at a chosen height partitions the leaves, but that height is a decision threshold, not a statistical test. The chart question should say whether the reader needs a hierarchy, a fixed number of groups or actual pairwise distances.
Inspect unstable merges
Small changes in records or scaling can reorder merges near the same height. A visually dramatic split is not automatic evidence of a natural population boundary. Refit after reasonable perturbations, compare membership and report how often groups persist; keep the data snapshot fixed while changing one assumption at a time. If a leaf represents a collapsed branch, show how many original members it contains. Truncating a large dendrogram can hide lower merges, so label contracted nodes explicitly. Uncertainty displays are useful when the tree is used to support a decision, especially with few observations or noisy distances. Keep an accessible membership table for every cut shown.
Choose the right comparison view
A tree can hold many leaves but becomes difficult to read at phone width. For a few entities, a labeled distance matrix may answer the exact-pair question faster. For many groups, a cut-level summary and downloadable membership table are more useful than microscopic text. In linked views, selecting a branch should highlight its exact IDs in the associated chart without recomputing the clustering under the cursor. Linked filters should disclose whether they merely highlight a fixed tree or rebuild it from a filtered population. Rebuilding changes the merge history and needs a new analysis version in the export.
Worked contract
from math import isfinite
def average_linkage_merges(values: dict[str, float]):
if len(values) < 2 or any(not label or not isfinite(value)
for label, value in values.items()):
raise ValueError("at least two finite labeled observations are required")
clusters = {label: (label,) for label in values}
merges = []
while len(clusters) > 1:
candidates = []
labels = sorted(clusters)
for left_index, left in enumerate(labels):
for right in labels[left_index + 1:]:
distances = [abs(values[first] - values[second])
for first in clusters[left] for second in clusters[right]]
candidates.append((sum(distances) / len(distances), left, right))
height, left, right = min(candidates)
members = tuple(sorted(clusters.pop(left) + clusters.pop(right)))
cluster_id = "|".join(members)
clusters[cluster_id] = members
merges.append((members, height))
return merges
sensor_offsets = {"sensor-47": 4.0, "sensor-59": 5.0,
"sensor-83": 19.0, "sensor-97": 21.0}
merges = average_linkage_merges(sensor_offsets)
assert merges[0] == (("sensor-47", "sensor-59"), 1.0)
assert merges[1] == (("sensor-83", "sensor-97"), 2.0)
assert merges[2][1] == 15.5Performance and operating cost
This transparent teaching implementation recomputes all candidate average distances at every merge. For N observations it uses O(N³) time in the worst case and O(N²) temporary space: each merge compares pair distances across the current partition; use a tested clustering library for real data. Its scalar absolute-distance rule is deliberately narrow. The output provides memberships and heights, not leaf x-coordinates. Those can be rearranged without changing the hierarchy.
Common Mistakes
- Reading adjacent leaves as the closest original observations.
- Changing the distance metric or scaling without documenting a new tree.
- Treating a chosen cut height as a significance threshold.
- Truncating branches without their member counts or a membership table.
Validation drill
Cluster 47 sensor offsets using two distance definitions. Save both trees, compare cut memberships and explain which merges changed because of the metric.
