Explainable HDBSCAN

Machine Learning
Introduced by
McInnes, L.; Healy, J. (HDBSCAN); Lundberg & Lee (SHAP-based explanation)
Year
2017–2020
Type
Explainable clustering
Family
Machine learning

What does it do?

Explainable HDBSCAN pairs the density-based clustering algorithm HDBSCAN with post-hoc feature attribution, usually SHAP, so that each cluster assignment can be accompanied by a per-feature account of what drove it.

The clustering itself is unchanged: HDBSCAN can still find clusters of varying density and shape and label low-density points as noise. The explanation is a separate, approximate layer fitted on top of the clustering result.

The intuition behind the method

HDBSCAN looks for dense regions separated by sparser ones and summarises them as a hierarchy of nested clusters, the condensed tree. Because HDBSCAN produces labels rather than a prediction function, the explanation layer needs a model to explain. In practice a surrogate model is trained to reproduce the cluster labels from the input features, and SHAP values then divide each of the surrogate's outputs among the features using Shapley's game-theoretic allocation. For a given point, the result is a list of feature contributions relative to the surrogate's average output, showing which variables pushed the point toward its assigned cluster. These attributions describe the surrogate's behaviour, so they are only as faithful as the surrogate is to the original clustering.

History

HDBSCAN is implemented in the Python library described by McInnes, Healy and Astels (2017), which is the software reference for this page. SHAP values come from the unified framework of Lundberg and Lee (2017). Combining the two into one explainable clustering workflow is an applied practice rather than a single published method.

When to use it

Choose this approach when the data are tabular or otherwise have clearly defined features, the clusters are expected to be irregular in shape or of uneven density, and cluster assignments must be justified feature by feature to reviewers or stakeholders. It suits exploratory work in which the number of groups is not known in advance. Do not use it when clusters are expected to be roughly spherical and k-means or a Gaussian mixture model would serve, when the sample is small enough that density estimates are unreliable, or when every point must receive a cluster label. There is no firm minimum sample size for this method, so judge whether a sample is large enough through the resampling checks described under pitfalls. The SHAP step adds computation, so it is a poor fit for latency-sensitive scoring.

Limitations and considerations

  • SHAP values explain a surrogate fitted to the cluster labels, not HDBSCAN's internal density computation, so the attributions are approximate to the extent that the surrogate reproduces the labels.
  • Labels are discrete and assignment depends on the tree structure, so points near cluster boundaries may be poorly fitted by the surrogate and their attributions are less reliable.
  • KernelSHAP is slow on large datasets because its cost rises quickly with the number of features and observations; TreeSHAP is faster but requires a tree-based surrogate.
  • The min_cluster_size and min_samples settings strongly shape the clustering, and different settings can produce different clusters and therefore different explanations.
  • Noise points are not assigned to a stable cluster, so there is no cluster-level reference against which their attributions can be read.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • HDBSCAN does not require the number of clusters to be fixed in advance and can recover groups of differing shape and density, so the explanations attach to a clustering that is not forced into spherical groups.
  • Points that fit no stable cluster are labelled -1 rather than assigned, which keeps noise separate from the cluster-level attribution analysis.
  • The condensed tree gives a view of the hierarchical structure before any SHAP analysis is run, which can help decide whether a split is worth explaining.
  • Shapley values satisfy efficiency, symmetry, dummy and additivity properties; in particular, a feature that never changes the surrogate's output receives zero attribution.
  • Feature rankings give domain experts something concrete to check against prior knowledge.

Applications

  • Customer segmentation where cluster definitions must be explained to managers or auditors.
  • Biomedical subgroup discovery in which the features driving group membership need to be identified for follow-up.
  • Triage of noise points in outlier-detection pipelines, using feature attributions to see which variables make a point atypical.
  • Geospatial or environmental data where clusters are irregular in shape and uneven in density.
  • Clustering of embeddings after dimensionality reduction, where attributions must be read in terms of the reduced axes or mapped back to the original features.

How it works

How it works

1

For each pair of points, HDBSCAN computes the mutual reachability distance, the largest of the two points' core distances and their direct distance. Core distances summarise local density, so distances involving points in sparse regions are raised and isolated points are less likely to link clusters together. Because every distance enters through d(a,b), the choice of distance metric and any feature scaling change the core distances, the hierarchy and therefore the clusters and their explanations. Features measured in very different units should be standardised first, and the distance metric should be reported with the results.

Formula
d_{\text{mreach}}(a,b) = \max(\text{core}_k(a), \text{core}_k(b), d(a,b))
2

A minimum spanning tree is built over the graph of mutual reachability distances. It connects all points with the smallest total distance and encodes the hierarchical density structure of the dataset in one structure, which the later steps read.

3

The full hierarchy is condensed by walking down it and treating a split as a true division only when both resulting branches contain at least min_cluster_size points. Smaller branches are recorded as points leaving their parent cluster. The result is a compact tree in which each node is a cluster that exists over a range of density levels.

4

Flat clusters are selected from the condensed tree by choosing the set of nodes with the highest total stability, a measure of how persistently a cluster holds together as the density threshold changes. Points that belong to no selected cluster receive label -1 and are reported as noise rather than being forced into a group.

5

A SHAP explainer, such as KernelSHAP, or TreeSHAP applied to a tree-based surrogate fitted to the cluster labels, estimates Shapley values for each observation. The values sum to the difference between the surrogate's output for that point and its expected output over the data. In the Shapley formula, v(S) is the surrogate's expected output when only the features in S are known, and the sum runs over all subsets of the other features. Each value is one feature's contribution, which supports both global feature rankings and local per-point explanations.

Formula
\phi_i = \sum_{S \subseteq F \setminus \{i\}} \frac{|S|!(|F|-|S|-1)!}{|F|!}[v(S \cup \{i\}) - v(S)]

Common pitfalls

  • Reading SHAP values as causal effects. They quantify contributions to the surrogate's prediction within this clustering, not what would change if a feature were altered.
  • Ignoring the condensed tree and relying only on flat labels, which discards the hierarchy that often carries the most information about nested structure.
  • Running HDBSCAN on very small samples, where core distances are unstable and attributions can vary considerably between resamples.
  • Comparing SHAP values across clusters without a common reference. Each attribution is relative to the surrogate's expected output, so any cross-cluster comparison needs that reference stated explicitly.
  • Presenting a single run as definitive. Check stability by resampling the data and comparing cluster agreement, and check whether the features that drive the clusters make sense to domain experts.

Frequently asked questions

Does HDBSCAN require specifying the number of clusters?

No. HDBSCAN infers the number of clusters from the density structure of the data. The main size setting is min_cluster_size, the smallest group you would consider meaningful, rather than a target cluster count.

Which SHAP explainer should I use with HDBSCAN?

HDBSCAN does not expose a prediction function that SHAP can explain directly, so a surrogate classifier, such as a random forest fitted to the cluster labels, is usually used. KernelSHAP can be applied to it but is slow on large data. If the surrogate is tree-based, TreeExplainer is generally much faster than KernelSHAP.

How do I handle noise points in the explanation?

Noise points (label -1) do not belong to a stable cluster, so their SHAP values do not describe membership in a cluster. It is good practice to treat them separately, reporting which features contribute most to their being classed as noise rather than combining them with clustered observations.

How do I validate that my clusters are real and not artifacts?

Check stability by resampling and compare the cluster persistence values reported by the library with quality scores such as silhouette or DBCV. Check whether the features that SHAP highlights make sense to domain experts. Replicating the clusters on an independently collected sample gives the strongest evidence.

Can Explainable HDBSCAN be used with high-dimensional data like text embeddings?

Yes, with caution. In very high-dimensional spaces distances can become less informative, so a dimensionality reduction step is a common choice before clustering. If you cluster reduced embeddings, the clusters reflect the reduced space; the surrogate can still be fitted on the original features, but its attributions then describe agreement with a clustering made in that reduced space.

Related methods

Sources

  1. McInnes, L., Healy, J., & Astels, S. (2017). hdbscan: Hierarchical density based clustering. Journal of Open Source Software, 2(11), 205.

    Open DOI reference
  2. Lundberg, S. M., & Lee, S.-I. (2017). A unified approach to interpreting model predictions. Advances in Neural Information Processing Systems, 30, 4765–4774.

    Open publication reference