Skip to content

HDBSCAN fails when using cluster_selection_epsilon #33219

Open

Description

@Thomas1664

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.0

Expected 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: None

Interest 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

  1. added
    Needs Triage
    on Feb 6, 2026
  2. changed the title [-]HDBSCAN fails when using cluster_selection_epsilon with precomputed distances[/-] [+]HDBSCAN fails when using cluster_selection_epsilon[/+] on Feb 6, 2026
  3. rohandhar6824-debug commented on Feb 7, 2026

    @rohandhar6824-debug
  4. FrancoisPgm commented on Feb 11, 2026

    @FrancoisPgm
    Member

    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 while parent is defined as an int. Simply using parent = 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 which parent_eps = 1 / cluster_tree[cluster_tree['child'] == parent]['value'][0] should be used.

    Also I noticed while testing that the traverse_upwards function is not covered by the tests.

  5. Mr-Neutr0n commented on Feb 13, 2026

    @Mr-Neutr0n

    Opened a fix in #33276.

    The problem is that traverse_upwards in _tree.pyx assumes 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.

  6. FrancoisPgm commented on Feb 13, 2026

    @FrancoisPgm
    Member

    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.

  7. Mr-Neutr0n commented on Feb 13, 2026

    @Mr-Neutr0n

    Understood. Closing!

  8. repeating commented on Mar 4, 2026

    @repeating

    hey, i ran into this too while looking at the codebase — the issue is in traverse_upwards where 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.

  9. Br1an67 commented on Mar 9, 2026

    @Br1an67
  10. Thomas1664 commented on Mar 9, 2026

    @Thomas1664
    Author

    Looks like these AI models have a hard time reading the comments on this issue:

    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.

    However, I'd also like to see this issue triaged and fixed.

  11. Mr-Neutr0n commented on Mar 9, 2026

    @Mr-Neutr0n
  12. Mr-Neutr0n commented on Mar 9, 2026

    @Mr-Neutr0n
  13. Jeremiah-England commented on Mar 13, 2026

    @Jeremiah-England

    This appears to have been introduced in Numpy 2.4.0. We have pined to 2.3.5 as workaround for now. See apple/coremltools#2633 for a similar issue.

  14. rein5 commented on Mar 19, 2026

    @rein5
  15. betatim commented on Mar 23, 2026

    @betatim
    Member

    This looks like a mistake was made/difference introduced when porting HDBSCAN to scikit-learn from the HDBSCAN package. The fix is to introduce a [0] to explicitly index the numpy array

    diff --git a/sklearn/cluster/_hdbscan/_tree.pyx b/sklearn/cluster/_hdbscan/_tree.pyx
    index 3c8e93abaa..3c8617f9ce 100644
    --- a/sklearn/cluster/_hdbscan/_tree.pyx
    +++ b/sklearn/cluster/_hdbscan/_tree.pyx
    @@ -585,14 +585,14 @@ cdef cnp.intp_t traverse_upwards(
         cdef cnp.float64_t parent_eps
     
         root = cluster_tree['parent'].min()
    -    parent = cluster_tree[cluster_tree['child'] == leaf]['parent']
    +    parent = cluster_tree[cluster_tree['child'] == leaf]['parent'][0]
         if parent == root:
             if allow_single_cluster:
                 return parent
             else:
                 return leaf  # return node closest to root
     
    -    parent_eps = 1 / cluster_tree[cluster_tree['child'] == parent]['value']
    +    parent_eps = 1 / cluster_tree[cluster_tree['child'] == parent]['value'][0]
         if parent_eps > cluster_selection_epsilon:
             return parent
         else:

    In 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.

  16. removed
    Needs Triage
    on Mar 23, 2026
  17. added a commit that references this issue on Mar 25, 2026
    58669f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions