HDBSCAN#

Hierarchical Density-Based Spatial Clustering of Applications with Noise (HDBSCAN) is a clustering algorithm proposed in [Campello2013]. It extends DBSCAN by converting it into a hierarchical clustering algorithm, then extracting a flat clustering based on the stability of clusters. Unlike DBSCAN, HDBSCAN does not require the user to choose an epsilon parameter and can find clusters of varying densities.

Operation

Computational methods

Programming Interface

Compute

Bf, kd tree, ball tree

compute(…)

compute_input

compute_result

Mathematical formulation#

Computation#

Given the set \(X = \{x_1 = (x_{11}, \ldots, x_{1p}), \ldots, x_n = (x_{n1}, \ldots, x_{np})\}\) of \(n\) \(p\)-dimensional feature vectors, a metric for pairwise distance computation, and positive integers min_cluster_size and min_samples, the problem is to compute cluster assignments for each observation.

Supported distance metrics#

The pairwise distance used for core-distance and mutual-reachability computation is selected via the metric parameter. HDBSCAN reuses the Minkowski distances family with configurable degree \(p > 0\): degree = 1 yields the Manhattan (\(L_1\)) distance, degree = 2 yields the Euclidean (\(L_2\), default) distance, and the limit \(p \to \infty\) corresponds to the Chebyshev distance (\(L_\infty\)); the Chebyshev distance is also exposed as its own metric value so callers do not have to pass a very large degree. In addition, HDBSCAN supports the Cosine distance for angular data.

The kd_tree and ball_tree methods support the Minkowski (Manhattan, Euclidean, general \(L_p\)) and Chebyshev distances – tree pruning relies on the triangle inequality of an \(L_p\) metric. The Cosine distance is only supported with the brute_force method; requesting Cosine with kd_tree or ball_tree raises invalid_argument.

HDBSCAN consists of the following steps:

(1) Core distance computation: For each point \(x_i\), the core distance is the distance to its \(k\)-th nearest neighbor, where \(k\) = min_samples:

\[d_{\text{core}}(x_i) = d(x_i, \text{NN}_k(x_i))\]

(2) Mutual Reachability Distance (MRD): The mutual reachability distance between any two points \(x_i\) and \(x_j\) is defined as:

\[d_{\text{mrd}}(x_i, x_j) = \max\{d_{\text{core}}(x_i),\; d_{\text{core}}(x_j),\; d(x_i, x_j)\}\]

This metric upweights sparse regions, making the algorithm robust to varying densities.

(3) Minimum Spanning Tree (MST): Construct the minimum spanning tree of the complete graph where each observation is a vertex and edge weights are mutual reachability distances.

(4) Condensed cluster tree: Convert the MST into a dendrogram (hierarchical clustering), then condense it by removing splits where one of the child clusters has fewer than min_cluster_size points. Points that fall out of clusters during condensation are potential noise points.

(5) Cluster extraction (Excess of Mass): Using the Excess of Mass (EOM) method [Campello2013], compute the stability of each cluster in the condensed tree, then select the set of clusters that maximizes total stability. Selected clusters provide the flat clustering output.

With cluster_selection_method = leaf the leaves of the condensed cluster tree are selected instead. The root is not a leaf of that tree, so a condensed tree that never splits into two clusters of min_cluster_size observations selects nothing and the whole dataset is noise.

The root cluster is a candidate for EOM only when allow_single_cluster is set. When it wins and is the only selected cluster, the flat clustering has no sibling to separate noise from signal, so an observation is kept only if the \(\lambda\) at which it fell out of the tree reaches a threshold: \(1 / \texttt{cluster\_selection\_epsilon}\) when an epsilon is set, and the root’s own death \(\lambda\) otherwise, which keeps only the observations that persist to the very end.

Each cluster gets a unique identifier, an integer from \(0\) to \(\text{cluster\_count} - 1\). Observations not belonging to any cluster are assigned \(-1\) (noise).

(6) Membership probabilities (optional): When the probabilities result option is requested, each observation also gets a membership strength in its cluster. With \(\lambda_i = 1 / d_i\), where \(d_i\) is the mutual reachability distance at which observation \(x_i\) fell out of the condensed tree, and \(\lambda_C^{\max}\) the largest such value over the selected cluster \(C\) the observation was assigned to,

\[p_i = \frac{\min(\lambda_i, \lambda_C^{\max})}{\lambda_C^{\max}}\]

so the most persistent members of a cluster get \(1\) and the ones that detach right after the cluster is born get a value close to \(0\). Noise observations get \(0\). A cluster that never loses an observation has \(\lambda_C^{\max} = 0\); all of its members then get \(1\).

(7) Single linkage tree (optional): When the single_linkage_tree result option is requested, the dendrogram of step (4) is reported as well, before it is condensed: an \((n - 1) \times 4\) table with one row per merge in ascending distance order, holding [left, right, distance, size]. An id below \(n\) is an original observation and id \(n + k\) is the cluster formed by row \(k\), so row \(n - 2\) is the root and spans all \(n\) observations. The table is empty when \(n < 2\). The ids share the table’s floating-point type, so they are exact as long as \(2n\) fits the mantissa: always in double precision, and up to \(n = 2^{23}\) in single precision. A larger \(n\) in single precision raises oneapi::dal::domain_error.

The dendrogram is the full hierarchy the flat clustering was cut out of, so a caller can re-cut it at any distance \(\varepsilon\) and obtain the clustering DBSCAN would produce at that \(\varepsilon\) – keeping the merges below the cut and labelling the resulting components – without running the algorithm again.

Computation method: brute_force#

The brute-force method computes the full \(n \times n\) pairwise distance matrix using the selected metric, then uses this matrix for core distance computation and MST construction via Boruvka’s algorithm. Euclidean and Cosine distances are GEMM-accelerated; other metrics use element-wise computation.

  • Core distances: For each row of the distance matrix, find the \(k\)-th nearest distance using partial sort. Parallelized across rows.

  • MST: Boruvka’s algorithm with \(O(n^2 \log n)\) worst-case complexity, using precomputed MRD matrix. The per-round nearest-neighbor search is parallelized across all points.

  • Complexity: \(O(n^2 p)\) for distances + \(O(n^2 \log n)\) for MST.

  • Memory: \(O(n^2)\) for the distance matrix.

Computation method: kd_tree#

The k-d tree method avoids the \(O(n^2)\) distance matrix by building a k-d tree data structure over the input data and using it for both core distance computation and MST construction via Boruvka’s algorithm. Supports Euclidean, Manhattan, Minkowski, and Chebyshev metrics (Cosine is not supported).

  • Core distances: For each point, perform a k-NN query on the k-d tree. Complexity: \(O(n \cdot k \cdot \log n)\).

  • MST: Boruvka’s algorithm with k-d tree-accelerated nearest-different-component queries, using three-level pruning (component, bounding-box MRD lower bound, near/far child ordering). Complexity: \(O(n \log^2 n)\).

  • Memory: \(O(n \cdot p)\) for k-d tree nodes and bounding boxes.

The k-d tree method is significantly faster for moderate-sized datasets, especially in low-to-moderate dimensions (\(p \leq 20\)).

Computation method: ball_tree#

The ball tree method is similar to the k-d tree method but uses hypersphere (ball) bounding volumes instead of axis-aligned bounding boxes. This provides better pruning in higher-dimensional spaces where k-d tree splitting planes become ineffective.

  • Core distances: For each point, perform a k-NN query on the ball tree. Complexity: \(O(n \cdot k \cdot \log n)\).

  • MST: Boruvka’s algorithm with ball-tree-accelerated nearest-different-component queries, using hypersphere distance bounds for pruning. Complexity: \(O(n \log^2 n)\).

  • Memory: \(O(n \cdot p)\) for ball tree nodes, centers, and radii.

The ball tree method is preferred over k-d tree when the number of features exceeds approximately 20, as it maintains effective pruning regardless of dimensionality.

Computation on GPU#

On GPU, the three methods share one implementation that builds the exact minimum spanning tree; as on CPU, only brute_force also divides the core distances by alpha. Each call takes one of two paths:

  • The \(n \times n\) matrix path described for brute_force, used for the Cosine distance, and for the other metrics when \(p \geq 16\) and the matrix and its work buffers fit in half of the device memory.

  • Otherwise, a bounding-box tree over the k-d ordered observations, searched exactly for the core distances and in every Boruvka round. For min_samples above 256 the core distances come from a blocked brute-force k-NN search instead, whose block size is distance_block_size.

Cosine raises oneapi::dal::domain_error when the matrix does not fit on the device. Cluster extraction runs on the host.

Programming Interface#

Refer to API Reference: HDBSCAN.

Examples#

References#

[Campello2013] (1,2)

Campello, R.J.G.B., Moulavi, D., Sander, J. Density-Based Clustering Based on Hierarchical Density Estimates. Advances in Knowledge Discovery and Data Mining, 2013.