Repository navigation
Description
Describe the bug and give evidence about its user-facing impact
When using HDBSCAN with cluster_selection_epsilon set to at least 0.015 (for my dataset), I get a TypeError.
I only get the error when using cluster_selection_epsilon. I don't get this error when using the hdbscan package (version 0.8.41) directly.
Steps/Code to Reproduce
Edit: I finally was able to reproduce the issue with synthetic data by playing around with cluster_selection_epsilon and you don't need precomputed distances:
from sklearn.cluster import HDBSCAN
import numpy as np
rng = np.random.default_rng(0)
X = np.vstack([
rng.normal(0, 0.2, size=(20, 2)),
rng.normal(1, 0.2, size=(21, 2)),
])
hdb = HDBSCAN(
min_cluster_size=5,
min_samples=1,
n_jobs=10,
copy=True,
allow_single_cluster=True,
cluster_selection_epsilon=1
)
print(hdb.fit_predict(X))The issue is only triggered with my specific data - I couldn't reproduce it with synthetic data. Notably, the same data works just fine with the hdbscan package.
from hdbscan import HDBSCAN
import pandas as pd
# Bug only triggered with my specific dataset
df = pd.read_csv('bug.csv', header=None)
hdb = HDBSCAN(
min_cluster_size=5,
min_samples=1,
n_jobs=10,
metric='precomputed',
copy=True,
allow_single_cluster=True,
cluster_selection_epsilon=0.015
)
print(hdb.fit_predict(df.values))bug.csv:
0.0,0.0144234,0.014608,0.018432,0.2028331,1e-06,0.0146122,0.0108745,0.0238127,0.0289057,0.0292668
0.0144234,0.0,0.030085,0.0341493,0.2174494,0.0185183,0.0300855,0.0261329,0.0444496,0.0499432,0.0506157
0.014608,0.030085,0.0,0.0224902,0.2284748,0.0188262,0.022492,0.0147419,0.029195,0.0344591,0.0349043
0.018432,0.0341493,0.0224902,0.0,0.2288503,0.0238127,0.0035258,0.0147674,1e-06,0.0045079,0.0045573
0.2028331,0.2174494,0.2284748,0.2288503,0.0,0.2061707,0.220841,0.2293228,0.2397258,0.2299638,0.2552726
1e-06,0.0185183,0.0188262,0.0238127,0.2061707,0.0,0.0188313,0.013986,0.0238127,0.0289057,0.0292668
0.0146122,0.0300855,0.022492,0.0035258,0.220841,0.0188313,0.0,0.0186034,0.0044997,0.0091034,0.0092055
0.0108745,0.0261329,0.0147419,0.0147674,0.2293228,0.013986,0.0186034,0.0,0.0190924,0.0241502,0.0244496
0.0238127,0.0444496,0.029195,1e-06,0.2397258,0.0238127,0.0044997,0.0190924,0.0,0.0045079,0.0045573
0.0289057,0.0499432,0.0344591,0.0045079,0.2299638,0.0289057,0.0091034,0.0241502,0.0045079,0.0,0.0092227
0.0292668,0.0506157,0.0349043,0.0045573,0.2552726,0.0292668,0.0092055,0.0244496,0.0045573,0.0092227,0.0Expected Results
Using the hdbscan package:
[ 0 0 0 0 -1 0 0 0 0 0 0]
Actual Results
Traceback (most recent call last):
File "bug.py", line 16, in <module>
print(hdb.fit_predict(df.values))
~~~~~~~~~~~~~~~^^^^^^^^^^^
File ".conda/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py", line 920, in fit_predict
self.fit(X)
~~~~~~~~^^^
File ".conda/lib/python3.14/site-packages/sklearn/base.py", line 1336, in wrapper
return fit_method(estimator, *args, **kwargs)
File ".conda/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py", line 866, in fit
self.labels_, self.probabilities_ = tree_to_labels(
~~~~~~~~~~~~~~^
self._single_linkage_tree_,
^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<4 lines>...
self.max_cluster_size,
^^^^^^^^^^^^^^^^^^^^^^
)
^
File "sklearn/cluster/_hdbscan/_tree.pyx", line 61, in sklearn.cluster._hdbscan._tree.tree_to_labels
File "sklearn/cluster/_hdbscan/_tree.pyx", line 75, in sklearn.cluster._hdbscan._tree.tree_to_labels
File "sklearn/cluster/_hdbscan/_tree.pyx", line 751, in sklearn.cluster._hdbscan._tree._get_clusters
File "sklearn/cluster/_hdbscan/_tree.pyx", line 626, in sklearn.cluster._hdbscan._tree.epsilon_search
File "sklearn/cluster/_hdbscan/_tree.pyx", line 588, in sklearn.cluster._hdbscan._tree.traverse_upwards
TypeError: only 0-dimensional arrays can be converted to Python scalars
Versions
System:
python: 3.14.2 | packaged by conda-forge | (main, Jan 26 2026, 19:56:00) [GCC 14.3.0]
executable: .conda/bin/python
machine: Linux-5.15.0-119-generic-x86_64-with-glibc2.35
Python dependencies:
sklearn: 1.8.0
pip: 25.3
setuptools: 80.10.2
numpy: 2.4.2
scipy: 1.17.0
Cython: None
pandas: 3.0.0
matplotlib: 3.10.8
joblib: 1.5.3
threadpoolctl: 3.6.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 64
prefix: libscipy_openblas
filepath: .conda/lib/python3.14/site-packages/numpy.libs/libscipy_openblas64_-096271d3.so
version: 0.3.31.dev
threading_layer: pthreads
architecture: SkylakeX
user_api: blas
internal_api: openblas
num_threads: 64
prefix: libscipy_openblas
filepath: .conda/lib/python3.14/site-packages/scipy.libs/libscipy_openblas-6cdc3b4a.so
version: 0.3.30
threading_layer: pthreads
architecture: SkylakeX
user_api: openmp
internal_api: openmp
num_threads: 96
prefix: libgomp
filepath: .conda/lib/python3.14/site-packages/scikit_learn.libs/libgomp-e985bcbb.so.1.0.0
version: NoneInterest in fixing the bug
I don't want to create a PR because I don't know the codebase at all.
I asked ChatGPT for possible causes and it thinks the bug might be related to tied distances, resulting in competing branches and epsilon intersects a plateau.
Activity
[-]HDBSCAN fails when using cluster_selection_epsilon with precomputed distances[/-][+]HDBSCAN fails when using cluster_selection_epsilon[/+]rohandhar6824-debug commentedon Feb 7, 2026
FrancoisPgm commentedon Feb 11, 2026
Thanks @Thomas1664 for reporting the bug, I am also able to reproduce it.
It seems that the issue is that
cluster_tree[cluster_tree['child'] == leaf]['parent']returns a list whileparentis defined as an int. Simply usingparent = cluster_tree[cluster_tree['child'] == leaf]['parent'][0]instead should do the trick.I believe there is a similar problem here with
parent_eps, for whichparent_eps = 1 / cluster_tree[cluster_tree['child'] == parent]['value'][0]should be used.Also I noticed while testing that the
traverse_upwardsfunction is not covered by the tests.Mr-Neutr0n commentedon Feb 13, 2026
Opened a fix in #33276.
The problem is that
traverse_upwardsin_tree.pyxassumes filtering the condensed tree by child id always returns a single row. With tied distances the condensed tree can have duplicate child entries, so the lookup returns an array instead of a scalar, which then blows up on the implicit conversion to a Python int/float.The fix just indexes
[0]on the parent and parent_eps lookups since all duplicates share the same parent and lambda value.FrancoisPgm commentedon Feb 13, 2026
Hi @Mr-Neutr0n thank you for your interest, but please check Issues tagged “Needs Triage” and refrain from opening a PR until consensus has been reached on how to handle the issue.
Mr-Neutr0n commentedon Feb 13, 2026
Understood. Closing!
repeating commentedon Mar 4, 2026
hey, i ran into this too while looking at the codebase — the issue is in
traverse_upwardswhere boolean indexing returns a shape-(1,) array instead of a scalar, and numpy 2.0 no longer silently coerces that. opening a PR with the fix.Br1an67 commentedon Mar 9, 2026
Thomas1664 commentedon Mar 9, 2026
Looks like these AI models have a hard time reading the comments on this issue:
However, I'd also like to see this issue triaged and fixed.
Mr-Neutr0n commentedon Mar 9, 2026
Mr-Neutr0n commentedon Mar 9, 2026
Jeremiah-England commentedon Mar 13, 2026
This appears to have been introduced in Numpy
2.4.0. We have pined to2.3.5as workaround for now. See apple/coremltools#2633 for a similar issue.rein5 commentedon Mar 19, 2026
betatim commentedon Mar 23, 2026
This looks like a mistake was made/difference introduced when porting
HDBSCANto scikit-learn from theHDBSCANpackage. The fix is to introduce a[0]to explicitly index the numpy arrayIn addition to that it would be nice to find a non regression test, ideally something more focussed than a test that relies on a particular set of hyper-parameters and data. But if we can't come up with one that is better than nothing. The goal of the non-regression test should be to make it very easy for someone from the future to understand what happened if the test fails.
FIX HDBSCAN cluster_selection_epsilon TypeError with tied distances