Search

Search papers and lessons

Accelerated Hierarchical Density Clustering

Leland McInnes, John Healy · 2017

McInnes and Healy made HDBSCAN fast enough to rival DBSCAN by using tree search to avoid most distance computations.

On this page

  • Colour = topic mix
  • Algorithms
  • Discrete math
  • Learning basics
  • Calculus
  • Linear algebra
  • Probability
  • ReadReady to readNot yet

TL;DR

HDBSCAN is a clustering method that finds groups of points that are dense, without asking you to choose a distance threshold or a fixed number of clusters. The original version was correct but slow, taking time that grows with the square of the number of points. This paper shows how to build the same answer with tree-based nearest-neighbour search and a spanning-tree algorithm, so the run time grows close to proportionally to N log N on many datasets. In its tests it matched DBSCAN's speed while keeping HDBSCAN's better behaviour on clusters of different densities.

Builds on: Hierarchical Clustering, Single-Linkage Clustering, Minimum Spanning Tree, K-Nearest Neighbors, DBSCAN · Leads to: the open-source HDBSCAN library and the density-based clustering that followed

Why it mattered

Clustering is often the first step of exploring a new dataset, when nobody knows how many groups to expect. Methods such as K-Means Clustering need that number in advance and assume round clusters. DBSCAN needs no cluster count and tolerates noise, but needs a distance scale and struggles when clusters have different densities. HDBSCAN removes the need for the distance scale and handles mixed densities. Before this paper, it was too slow to use on large data. The paper's claim is that it becomes a practical default: its speed is comparable to DBSCAN's, and the authors released a Python library built on scikit-learn's tree-based neighbour search, so other people can use it directly. Tree search here is exact, not the approximate kind covered in Approximate Nearest Neighbor Search.

The problem

Three things went wrong with the earlier HDBSCAN* method (Campello and colleagues, 2013 and 2015).

  1. Run time. The method takes time proportional to N squared for N points overall. Most of that cost sits in two stages: computing each point's core distance (below) and building the minimum spanning tree.
  2. Exact pairwise work. Every pair of points had to be compared, because the mutual reachability distance depends on the two points' local densities.
  3. Scaling. The paper compares it with the other popular algorithms and sets the goal as sub-quadratic growth, with N log N strongly preferred.

The paper keeps the method's answers the same as before, up to a small, named approximation, and attacks the cost of the three steps one at a time.

How it works

The algorithm runs in stages. Each stage's output is the next stage's input.

flowchart LR
  A[Points] --> B[Core distance of each point]
  B --> C[Mutual reachability distance]
  C --> D[Minimum spanning tree]
  D --> E[Single-linkage tree]
  E --> F[Condensed tree]
  F --> G[Stable clusters]

Here is one tiny instance traced through. Six points on a line at positions 0, 1, 2, 10, 11 and 12, and k = 2. The core distance of a point is the distance to its second-nearest other point (the point itself does not count).

  1. Core distances. For each point, find its second-nearest other point. Point 0 has neighbours 1 (distance 1) and 2 (distance 2), so its core distance is 2. Point 1 has neighbours 0 and 2, both at distance 1, so its core distance is 1. Point 2 has neighbours 1 (distance 1) and 0 (distance 2), so its core distance is 2. The right group works the same way: 10 has core distance 2, 11 has 1, 12 has 2. Check: the core distance of 1 is 1, because both of its neighbours sit at distance 1.
  2. Mutual reachability. For two different points a and b, define the mutual reachability distance as the largest of their two core distances and the plain distance between them. Between 0 and 1 it is max(2, 1, 1) = 2. Between 2 and 10 it is max(2, 2, 8) = 8. Density now enters the distance: a point in a sparse region has a large core distance, so every edge to it is long. Check: a pair of close points inside a crowd keeps its ordinary distance, but a pair that includes a loner is pushed apart.
  3. Minimum spanning tree. Sort every pair by mutual reachability distance and add edges while they do not form a loop. The three pairs inside the left group all have weight 2, and so do the three inside the right group. Taking two of each group's three gives four edges of weight 2 (ties can be broken either way), and the cheapest edge that joins the groups is 2 to 10 with weight 8. Total weight: 4 × 2 + 8 = 16. Check: a spanning tree of six points has exactly five edges. Here there are 4 + 1 = 5.
  4. Single-linkage tree. Process the edges from cheapest to dearest and record each merge. The left three points merge at height 2, the right three merge at height 2, and all six merge at height 8. Check: the root of the tree sits at the largest edge weight, 8.
  5. Condense the tree. Choose a minimum cluster size m = 3. At height 8 the root splits into two parts, each with three points, which is enough, so this is a true split and both parts persist as clusters. Splits that break a cluster into pieces smaller than m are not counted as splits; those points are said to fall out of their parent. Check: with m = 4 the same split would not count, and the six points would form one cluster.
  6. Score each cluster. Each cluster's stability adds up, over its points, how long each point stayed in it on the scale λ = 1/distance. Each left point joins at λ = 1/8 and falls out at λ = 1/2, so the left cluster scores 3 × (1/2 − 1/8) = 9/8. The right cluster scores the same. The root exists from λ=0\lambda = 0 until the split at λ=1/8\lambda = 1/8, so it scores 6 × (1/8 − 0) = 3/4. Check: 9/8 + 9/8 = 9/4 is larger than 3/4, so keep the two children.
  7. Output. The flat clustering is {0, 1, 2} and {10, 11, 12}. No cluster count was given.

The paper's speed-ups sit in stages 1 and 3: finding nearest neighbours with trees rather than by comparing every pair, and computing the spanning tree with a method suited to points in space. Condensing is a single pass over the tree.

The math

The paper gives three equivalent descriptions of HDBSCAN*. The one used by the algorithm is the one in the walkthrough.

Definition: Core distance

For a point xx and an integer kk, the core distance κ(x)\kappa(x) is the distance from xx to its kk-th nearest other point.

Step 1 in symbols

κ(x)=d(x, x(k))\kappa(x)=d\big(x,\,x_{(k)}\big)

Here x(k)x_{(k)} is the kk-th nearest point to xx, and dd is the distance of the underlying metric space. The paper uses the reciprocal of this value as a local density estimate, so a small core distance means a dense neighbourhood. The k-nearest-neighbour query that finds it is where the tree search helps most.

Step 2 in symbols

dmreach(a,b)=max⁡{κ(a), κ(b), d(a,b)}a≠bd_{\text{mreach}}(a,b)=\max\{\kappa(a),\,\kappa(b),\,d(a,b)\}\qquad a\neq b

and dmreach(a,a)=0d_{\text{mreach}}(a,a)=0. The paper states that this is a metric on the points. Its key property: dmreach(a,b)d_{\text{mreach}}(a,b) is never less than either core distance, so points in sparse regions cannot be chained together through short edges.

Step 3 in symbols

Let GG be the complete graph on the points, with each edge weighted by dmreachd_{\text{mreach}}. A minimum spanning tree TT is a subset of N−1N-1 edges that connects all the points with the smallest total weight ∑e∈Tw(e)\sum_{e\in T} w(e). Single-linkage clustering is the sequence of merges obtained by adding the edges of TT in order of weight, which is what single linkage computes from the same distances.

Step 5 and 6 in symbols

The paper writes the stability of a cluster CC in the condensed tree as

σ(C)=∑x∈C(λmax⁡,C(x)−λmin⁡,C(x)),λ=1ε\sigma(C)=\sum_{x\in C}\big(\lambda_{\max,C}(x)-\lambda_{\min,C}(x)\big),\qquad \lambda=\frac{1}{\varepsilon}

where λmin⁡,C(x)\lambda_{\min,C}(x) is the value at which xx joins CC and λmax⁡,C(x)\lambda_{\max,C}(x) the value at which it leaves. The flat clustering then chooses a set of clusters with no overlap that maximises the total stability. Choosing the children over the parent whenever their stabilities sum to more is the rule that gives the answer in the walkthrough.

Cost in symbols

The reference method takes time of order N2N^2. The accelerated method's stages each have sub-quadratic cost for many datasets: core distances by tree search, the spanning tree by a Borůvka variant whose rounds are nearest-neighbour searches (the paper cites a bound of order Nlog⁡NN\log N times an inverse-Ackermann factor from March and colleagues for cover trees), and condensing in a single pass over the tree. The paper says the overall cost is data-dependent and that Nlog⁡NN\log N is an expectation for many datasets, not a proof.

Results

The paper benchmarks run time, not cluster quality. Quality was treated in the earlier HDBSCAN* papers.

  • Against the reference HDBSCAN.* On 2-dimensional and 50-dimensional data, the accelerated Python version was much faster, and its curve on a log-log plot has a clearly smaller slope, which the authors read as sub-quadratic growth. At 200,000 points it was roughly two orders of magnitude faster in absolute run time, in both dimensions.
  • Against other scikit-learn clustering methods, with default settings. Affinity propagation, spectral clustering and mean shift scaled poorly beyond a few thousand points. Ward, complete linkage and Birch did better but still scaled poorly for large sizes. K-means was the fastest and most scalable, roughly linear in N, but needs the number of clusters, assumes Gaussian clusters and does not cope well with noise or outliers. DBSCAN and HDBSCAN* grew at similar rates, close to k-means.
  • Against DBSCAN, with tuned parameters. DBSCAN's default parameters make comparisons meaningless, so the authors searched DBSCAN's two parameters to best match each HDBSCAN* result (using the adjusted Rand index), and then timed DBSCAN at those settings. Run time was comparable at the same asymptotic rate across dimensions and numbers of clusters.

Limits the paper states itself:

  • The tie-breaking shortcut that speeds up the spanning-tree step means the result is an approximation of the exact spanning tree. The authors argue that the differences mostly disappear during condensing and cluster selection.
  • The method is serial. The authors name parallelising it over partitioned space as open work.
  • The DBSCAN comparison used settings found by a search that needed the HDBSCAN* answer first. A user does not have that answer in advance, so the tuned comparison is a best case for DBSCAN.
  • Run times depend on the data. The benchmarks ran on one laptop (3.1 GHz Intel Core i7, 8 GB RAM) with scikit-learn 0.18.1.

Reading guide

The paper has six numbered sections. Section 2.2 and section 3 carry the algorithm. Read them in this order.

  • 1 Introduction uses DBSCAN and K-Means Clustering as the reference points for the problem. Read it for the goals: exploratory use, few parameters, noise handling.
  • 2.1 Statistical view. Needs Probability Density Function (the density function and its level sets) and Hierarchical Clustering (cluster trees). Skip the excess-of-mass formulas on a first read; the stability formula in section 2.2 does the same job for the algorithm.
  • 2.2 Computational view. The most useful section for implementation. Needs DBSCAN, Single-Linkage Clustering, and Minimum Spanning Tree. The walkthrough above follows this section.
  • 2.3 Topological view. Needs topology well beyond this site's current lessons. Skip it on a first read; it is an alternative route to the same algorithm and the authors name the parameter-free extension as future work.
  • 3 Accelerating HDBSCAN.* The main contribution. Needs K-Nearest Neighbors, Minimum Spanning Tree, Metric Space and union-find. The tree search and Borůvka rounds are the parts to read slowly.
  • 4 Performance comparisons. Read the figure captions and the text around them. Needs Graph only for the spanning-tree vocabulary.
  • 5 Future work and 6 Conclusions: short; read them for the limits and the claim.

Check yourself

QuestionWhat does the core distance κ(x)\kappa(x) of a point measure, and what does a small value tell you?Show answer

It is the distance from xx to its kk-th nearest other point. A small value means that xx sits in a dense neighbourhood, because its kk nearest neighbours are all close.

QuestionUsing the six-point example, compute dmreach(1,10)d_{\text{mreach}}(1, 10).Show answer

The core distances are κ(1)=1\kappa(1)=1 and κ(10)=2\kappa(10)=2, and the plain distance is d(1,10)=9d(1,10)=9. So dmreach(1,10)=max⁡{1, 2, 9}=9d_{\text{mreach}}(1,10)=\max\{1,\,2,\,9\}=9.

QuestionWhy does mutual reachability distance keep chains of points in sparse regions from merging clusters early?Show answer

Every edge touching a point is at least that point's core distance. A point in a sparse region has a large core distance, so each edge to it is long, and it cannot link into a dense group through a short edge. This is the fix for the chaining problem of single linkage that the paper describes in section 2.1.

QuestionThe accelerated algorithm may return a spanning tree that is not exactly minimal. Where does this come from, and why does the paper accept it?Show answer

It comes from the shortcut for ties: when many distances are equal, the algorithm reuses the same pruning bounds across repeated searches, which is fast but can skip a few optimal edges. The paper accepts it because the differences are small and are usually removed when the tree is condensed and clusters are selected, and because the run time falls a lot.

Sources

Read the full paper

Leland McInnes, John Healy, 2017. arXiv:1705.07321