pip install hdbscan★The standalone library (needs a C compiler).conda install -c conda-forge hdbscanPrebuilt — no compiler needed.import hdbscan★Thenhdbscan.HDBSCAN(...).from sklearn.cluster import HDBSCANIn sklearn ≥ 1.3 — fewer extras than standalone. To match results, sklearn'smin_samplesmust be 1 greater than this package's.
c = hdbscan.HDBSCAN(min_cluster_size=15)★Create the clusterer.c.fit(X)★Run the clustering.labels = c.labels_★One label per point; −1 = noise.labels = c.fit_predict(X)★Fit + return labels in one call.n = labels.max() + 1Number of clusters found (noise excluded).plt.scatter(*X.T, c=labels, s=5)Color points by cluster.
min_cluster_size=5★Smallest group that counts as a cluster.min_samples=None★How conservative; defaults to min_cluster_size.cluster_selection_method="eom""eom"few big ·"leaf"many small.metric="euclidean"Distance in the input space.cluster_selection_epsilon=0.0Merge clusters closer than this (DBSCAN-ish floor).
min_cluster_size=5★Default; small groups still count.min_cluster_size=50..100Fewer, larger, core clusters.# set to the smallest group you care aboutIts meaning is intuitive — pick by domain.# too small ⇒ micro-clustersLarger values are more robust to noise.
min_samples=None★Defaults tomin_cluster_size.min_samples=1Little noise; clusters reach further out.min_samples=25Dense cores; more points called noise.# set it apart from min_cluster_sizeDecouple to tune granularity & noise separately.
cluster_selection_method="eom"★Excess of Mass — a few most-persistent clusters.cluster_selection_method="leaf"Many small, homogeneous, fine clusters.allow_single_cluster=TruePermit one big cluster (off by default).cluster_selection_epsilon=0.5Stop splitting below a distance — merges micro-clusters.max_cluster_size=0Cap eom cluster size (0 = no limit).
metric="euclidean"★Default; general numeric data.metric="manhattan"Other tree-supported metrics.metric="minkowski", p=3Pass extra args as kwargs.metric="haversine"Lat/lon in radians.metric="precomputed"X is a square distance matrix.# cosine not in the fast treesNormalize rows + euclidean, or precompute.
c.labels_★Cluster id per point;−1is noise.c.probabilities_★Membership strength 0–1 (0 for noise).c.cluster_persistence_Stability score per cluster (1 = rock-solid).c.outlier_scores_GLOSH outlier score per point.c.exemplars_Representative points (needsprediction_data).
hdbscan.HDBSCAN(prediction_data=True)★Cache what's needed to predict later.c.generate_prediction_data()Or enable it after fitting.labels, probs = hdbscan.approximate_predict(c, X_new)★Assign new points to existing clusters.hdbscan.approximate_predict_scores(c, X_new)Outlier score for new points.# not a full re-clusteringRe-fit if the data shifts a lot.
hdbscan.all_points_membership_vectors(c)★(n × k) — each point's membership to every cluster.hdbscan.membership_vector(c, X_new)Soft-assign new points across clusters.vecs.argmax(axis=1)Hard label = strongest membership.# requires prediction_data=TrueSame cached data as approximate_predict.
c.condensed_tree_.plot(select_clusters=True)★Icicle plot — see which clusters were chosen.c.single_linkage_tree_.plot()Full dendrogram of the merge hierarchy.c.condensed_tree_.to_pandas()parent · child · lambda_val · child_size.c.minimum_spanning_tree_.plot()Needsgen_min_span_tree=Trueat init.
c.outlier_scores_★Higher = more outlier-like (GLOSH).np.quantile(c.outlier_scores_, 0.9)Threshold to flag the top outliers.c.relative_validity_Fast DBCV — compare across hyper-params.hdbscan.validity.validity_index(X, labels)Full DBCV score (−1 … 1, higher better).
c.dbscan_clustering(cut_distance=0.3)Extract a DBSCAN* clustering for free.core_dist_n_jobs=-1★Parallelize core-distance computation.algorithm="best"Auto-pick; orboruvka_kdtreefor big data.gen_min_span_tree=TrueEnable the MST plot &relative_validity_.memory="./cache"Cache the hard computation across re-runs.
min_cluster_size = smallest meaningful groupPick by domain, then adjust.min_samples=None → lower itDrop it to reduce noise if too much is −1.cluster_selection_method="eom""leaf"when you want many fine clusters.PCA / UMAP → HDBSCANReduce high-dim first, cluster the result.condensed_tree_.plot(select_clusters=True)Always eyeball the chosen clusters.prediction_data=TrueIf you'll assign new points later.
noise is a feature (−1)by designHDBSCAN won't force outliers into clusters.min_samples = min_cluster_sizedefaultSet it explicitly to control noise.raw high-dim ⇒ poor clusterscareReduce dimensions first.all-noise result?careLowermin_cluster_size/ tryallow_single_cluster.probabilities_ ≠ confidencecareIt's persistence-based membership strength.