Chapter 4
Clustering with spanning trees
The minimal spanning tree connects all samples with the shortest possible set of edges, and removing its longest edges separates clusters of any shape. This chapter presents Zahn’s classical criteria for inconsistent edges, the cluster validity indices that judge tree partitions, the hybrid MST–Gath–Geva algorithm that cuts chains by a volume criterion and turns the tree clusters into a mixture of Gaussians, and HDBSCAN*, which builds the tree from density-corrected distances and selects clusters by their persistence. The density-based validity index DBCV completes the chapter.
4.1 Problem and idea
Removing edges from a tree leaves subtrees, which are natural candidate clusters: every sample in a subtree can be reached from every other by short steps, and the subtrees are separated by at least the length of the removed edges. Since the tree has only edges, finding a good partition reduces to choosing a few edges among candidates. Zahn [9] called the edges to remove inconsistent and proposed to recognize them by their length relative to the other edges, either all of them or those nearby.
Two difficulties remain (fig. 4.1). Groups of different density have tree edges of different typical length, so a global threshold either cuts the sparse group or leaves the dense groups merged; a local comparison helps. More seriously, a thin chain of samples between two groups keeps every edge short, so no length criterion fires: the chaining effect of single linkage. The hybrid algorithm of Vathy-Fogarassy, Feil, and Abonyi [2] breaks chains with a second criterion based on the volume of the clusters; HDBSCAN* [7] breaks them by lengthening edges in sparse regions before the tree is built.
4.2 Theory
4.2.1 Zahn’s inconsistent edges
Let be the Euclidean MST of the data with edge lengths . Zahn’s first criterion declares an edge inconsistent if it is much longer than the average edge,
|
| (4.1) |
with a user-chosen factor (typically 2 to 3). The second criterion compares the edge with the mean length of the tree edges within steps of its end points (the edge itself excluded) and declares it inconsistent if . The local criterion detects the boundary between a dense and a sparse group, because the edges around the boundary edge belong to the dense group. Both criteria remove edges in the order of decreasing length; the removal of the longest edges without any criterion is single-linkage clustering [38].
4.2.2 Validity of tree partitions
Because the tree can be cut at any number of places, the number of clusters must be decided by an index. Three indices fit tree clustering particularly well.
Dunn-like index with MST diameters. Dunn’s index [53] divides the smallest distance between clusters by the largest cluster diameter. For elongated clusters the ordinary diameter (the largest within-cluster distance) is misleading; Pal and Biswas [54] replaced it by the length of the longest path in the minimal spanning tree of the cluster,
|
| (4.2) |
which measures the extent of a cluster along its own backbone. Large values indicate compact, well-separated clusters.
Fukuyama–Sugeno index. For a fuzzy or crisp partition with centers and memberships , the index [55] subtracts the separation of the centers from the compactness of the clusters; Varma and Simon [56] used it to decide which MST edge to remove.
Fuzzy hypervolume. With the fuzzy covariance matrices
|
| (4.3) |
the hypervolume of cluster is and the total fuzzy hypervolume is [57]. For a crisp partition and , is the ordinary covariance matrix of the cluster. A compact Gaussian group has a small volume; the union of two groups, with or without a chain between them, has a large one. Small FHV indicates a good partition.
4.2.3 The hybrid MST–Gath–Geva algorithm
The hybrid algorithm [2] grows a binary tree of clusters by repeated splitting of the MST (algorithm 4.1). In every step it selects the current cluster with the largest hypervolume and splits it by one of three criteria, tried in order: Zahn’s global criterion, Zahn’s local criterion, and—if neither finds an inconsistent edge—the edge of the cluster’s subtree whose removal yields the smallest total hypervolume. The third criterion evaluates all edges of the selected subtree. With running sums of and accumulated over the subtrees of a rooted tree, the covariance matrices of both sides of every possible cut are obtained in time together, so the exhaustive search is cheap.
Cutting continues until a maximal number of clusters is reached. If the true number is unknown, is chosen somewhat larger and the clusters are merged afterwards, for instance by single linkage of the cluster centers. The tree partition can be used directly, but it is crisp and nonparametric. When a compact description is wanted, it initializes Gath–Geva clustering [57], the fuzzy counterpart of a Gaussian mixture, in which the distance of sample from cluster is inversely proportional to the Gaussian likelihood,
|
| (4.4) |
and memberships and parameters are updated alternately, with
|
| (4.5) |
Gath–Geva converges quickly but is notoriously sensitive to its starting partition; the tree partition, whose clusters are by construction compact and pure, is an excellent start.
In a nutshell: Hybrid MST–Gath–Geva clustering
- Combines the shape freedom of the MST with the compact parametric description of a Gaussian mixture.
- Zahn’s criteria separate well-separated groups; the volume criterion cuts chains and bends that no length criterion detects.
- Parameters: (choose generously and merge), , a minimal cluster size, the fuzziness of Gath–Geva.
- The tree partition solves the initialization problem of Gath–Geva; the Gath–Geva result adds memberships and covariance matrices.
4.2.4 HDBSCAN*: a density-corrected tree
Density-based clustering, in the tradition of DBSCAN [58], defines clusters as regions of high density separated by regions of low density. Campello, Moulavi, and Sander [7, 10] showed that the entire hierarchy of DBSCAN partitions over all density thresholds is encoded in one minimal spanning tree. Let the core distance be the distance of to its -th nearest neighbor (the sample itself counted) and define the mutual reachability distance
|
| (4.6) |
In dense regions equals the Euclidean distance; in sparse regions it is inflated to the local core distance. The MST of the complete graph with weights is the single-linkage hierarchy of this density-adjusted distance. A chain of samples between two groups is sparse, its core distances are large, and its edges become long: exactly what Zahn’s criteria lacked.
The hierarchy is then simplified. Proceeding from the root (all samples) towards the leaves with increasing density level , a split that cuts off fewer than samples (the minimal cluster size) is interpreted as samples leaving the cluster rather than a new cluster appearing. In the resulting condensed tree every cluster is born at some level and loses its samples at levels ; its stability is
|
| (4.7) |
Clusters are selected bottom-up by excess of mass: a cluster is kept if its stability exceeds the summed stability of its selected descendants, otherwise the descendants are kept. Samples that do not belong to any selected cluster are labeled noise. HDBSCAN* thus answers three questions that Zahn’s method leaves open: which edges to cut (by density level), how many clusters (by stability), and which samples are outliers.
In a nutshell: HDBSCAN*
- The MST of mutual reachability distances encodes all density-based partitions at once.
- Clusters are chosen by stability over density levels; the number of clusters follows.
- Parameters: minimal cluster size and the neighbor count of the core distance (default ).
- Returns noise labels and membership strengths; works on any distance and in moderate dimension.
4.2.5 Density-based validity
Indices built on centers and variances reward spherical clusters. DBCV [11] evaluates density-separated clusters of any shape with the same machinery as HDBSCAN*. Within each cluster an all-points core distance is computed (an average of inverse distances to all other members raised to the power of the dimension), the mutual reachability MST of the cluster is built, and its largest internal edge measures the density sparseness of the cluster. The smallest mutual reachability distance between internal nodes of two clusters measures their density separation. The validity of cluster is
|
| (4.8) |
with the separation to the nearest other cluster, and DBCV is the size-weighted average of , with noise samples counted in the total. It ranges from to ; larger is better.
4.3 Using it in graphclust
graphclust.cluster
MSTClustering(n_clusters=None, lambda_=2.0, criterion="both", depth=2, min_cluster_size=1): Zahn’s method; criterion=None with n_clusters gives single linkage. HybridMSTGG(max_clusters, lambda_=2.0, depth=2, min_cluster_size=None, gath_geva=True, m=2.0): attributes tree_labels_, labels_, membership_, cluster_centers_, covariances_, split_history_. HDBSCANStar(min_cluster_size=5, min_samples=None): labels_ (noise ), probabilities_, mst_, condensed_tree_. GathGeva is in graphclust.fuzzy; dunn_mst_index, fukuyama_sugeno, fuzzy_hypervolume, dbcv, clustering_rate in graphclust.validity.
The hybrid algorithm reports which criterion produced each split:
model = HybridMSTGG(max_clusters=4, lambda_=2.5, m=2.0).fit(X) for step in model.split_history_: print(step["criterion"], "cut of cluster", step["cluster"], "volumes", np.round(step["volumes"], 4)) print("clustering rate:", clustering_rate(y, model.labels_))
4.4 Examples
4.4.1 The chaining effect
The ChainLink data (make_chainlink) consist of three compact groups, two of which are connected by a chain of fifteen samples. Figure 4.2 shows the MST and two classical results. Zahn’s criteria with detach the lower group, broken into two pieces, but leave the two chained groups together; increasing only reduces the number of pieces. Removing the three longest edges (single linkage with four clusters) isolates individual samples instead of cutting the chain.
4.4.2 Hybrid MST–Gath–Geva clustering
With and the hybrid algorithm first removes the long edge to the lower group (global criterion) and then cuts the chained cluster twice by the volume criterion, once at each end of the chain (fig. 4.3). Gath–Geva started from this partition describes the two groups, the lower group, and the chain by Gaussians; 74 of the 75 samples are assigned correctly (98.7%).
Open in ColabExample plot_02_hybrid_mst_gg.py in the gallery
4.4.3 Initialization of Gaussian mixtures for curved clusters
The Curves data contain four arcs. Run with , the hybrid algorithm separates the arcs by Zahn’s criteria and continues to split the most voluminous pieces, so that every arc is covered by two or three roughly ellipsoidal clusters. All pieces are pure; after Gath–Geva fine tuning, 98.4% of the samples are in a cluster dominated by their own arc, against 87.0% when Gath–Geva starts from random memberships (fig. 4.4). The single linkage dendrogram of the ten centers shows which pieces belong together.
Open in ColabExample plot_03_curves_initialization.py in the gallery
4.4.4 The Iris data
On the four Iris measurements, the global criterion separates setosa, and the volume criterion divides the remaining cluster into versicolor and virginica. With a minimal cluster size of ten samples, 145 of the 150 flowers are grouped correctly (96.7%), both by the tree partition and after Gath–Geva (fig. 4.5); without the size limit the volume criterion splits off eight border samples instead.
4.4.5 HDBSCAN* and DBCV
Figure 4.6 applies HDBSCAN* to the Variety data. The MST of mutual reachability distances links the outlier and the sparse group by long edges. With a minimal cluster size of eight, three clusters are selected and the outlier is labeled noise; counting the noise label as an error, 98% of the samples are clustered correctly. DBCV rates this partition at 0.42, while Zahn’s partition of the same data, which splits off small fragments, scores 0.04.
4.5 Further reading
Zahn’s paper [9] discusses many more criteria than the two used here, and Gower and Ross [38] establish the link to single linkage. The hybrid algorithm and its fuzzy similarity of clusters are described by Vathy-Fogarassy, Feil, and Abonyi [2]; Gath and Geva [57] introduce the fuzzy maximum likelihood estimates and the hypervolume. Campello et al. [10] give the full theory of HDBSCAN*, including outlier scores; McInnes, Healy, and Astels [59] describe an efficient implementation. DBCV is introduced by Moulavi et al. [11].