Skip to content

HDBSCAN: TypeError from traverse_upwards when cluster_selection_epsilon >= the top-level merge distance (both eom and leaf) #35473

In review

Description

@SapereAudeAI

Describe the bug

HDBSCAN raises TypeError: only 0-dimensional arrays can be converted to Python scalars from
sklearn/cluster/_hdbscan/_tree.pyx whenever cluster_selection_epsilon is greater than or equal to the
distance at which the top-level clusters merge. It affects both cluster_selection_method="eom" (the
default) and "leaf", and it reproduces with four points in one dimension.

The failure is data-dependent, which is what makes it easy to miss: the same cluster_selection_epsilon works
on one dataset and raises on another. I hit it first on a real clustering job, where it killed five of the
grid entries I was sweeping, and only afterwards found that it reduces to the tiny case below.

Steps/Code to Reproduce

import numpy as np
from sklearn.cluster import HDBSCAN

X = np.array([[0.0], [0.01], [1.0], [1.01]])

HDBSCAN(
    min_cluster_size=2,
    cluster_selection_epsilon=2.0,   # >= the 1.0 separation between the two pairs
    cluster_selection_method="eom",  # the default; "leaf" fails identically
).fit_predict(X)

The relevant variable is cluster_selection_epsilon relative to the cluster separation. With the six-point
version below, eps=0.5 works and eps=1.0 (equal to the separation) already raises:

X6 = np.array([[0.0], [0.01], [0.02], [1.0], [1.01], [1.02]])
for eps in (0.5, 1.0, 2.0):
    for method in ("eom", "leaf"):
        try:
            labels = HDBSCAN(min_cluster_size=2, cluster_selection_epsilon=eps,
                             cluster_selection_method=method).fit_predict(X6)
            print("OK   ", method, eps, labels)
        except TypeError as e:
            print("CRASH", method, eps, e)
OK    eom  0.5 [0 0 0 1 1 1]
OK    leaf 0.5 [0 0 0 1 1 1]
CRASH eom  1.0 only 0-dimensional arrays can be converted to Python scalars
CRASH leaf 1.0 only 0-dimensional arrays can be converted to Python scalars
CRASH eom  2.0 only 0-dimensional arrays can be converted to Python scalars
CRASH leaf 2.0 only 0-dimensional arrays can be converted to Python scalars

A 2-D version of the same six points fails the same way, so it is not specific to one-dimensional input.

Expected Results

A cluster_selection_epsilon larger than every merge distance in the tree should be accepted and should simply
produce the coarsest selection available — in this example a single cluster, or all points in one group — rather
than raising. At minimum it should raise an informative ValueError naming the parameter, not a TypeError
about array dimensionality from inside a Cython helper.

Actual Results

  File ".../sklearn/cluster/_hdbscan/hdbscan.py", line 920, in fit_predict
    self.fit(X)
  File ".../sklearn/base.py", line 1403, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File ".../sklearn/cluster/_hdbscan/hdbscan.py", line 866, in fit
    self.labels_, self.probabilities_ = tree_to_labels(
  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

_get_clusters enters epsilon_search at line 751 under "eom" and at line 771 under "leaf"; both then fail
in traverse_upwards.

Likely cause

Line 588 of _tree.pyx is

parent = cluster_tree[cluster_tree['child'] == leaf]['parent']

and parent is a typed scalar, so the assignment requires the mask to select exactly one row. Every cluster in
the condensed tree appears once as a 'child' except the root, which only ever appears as a 'parent'. So
when the upward walk in epsilon_search reaches the root, the mask selects zero rows, the result is not
0-dimensional, and the conversion raises.

That matches the observed threshold exactly: the walk only reaches the root when
cluster_selection_epsilon is at least the distance at which the last two clusters merge, which is why
eps < separation is fine and eps >= separation is not. I have not confirmed this against the internals
directly — I could not conveniently drive _condense_tree from outside — so please treat the mechanism as a
hypothesis supported by the threshold behaviour rather than as a verified diagnosis. The reproducer and the
traceback are solid.

A guard in traverse_upwards for "leaf has no parent row, i.e. it is the root" and returning the root (or the
current cluster) looks like it would be sufficient, but I have not built and tested a patch, so I am filing this
as a report rather than a pull request.

Versions

System:
    python: 3.11.15
   machine: Windows-11

Python dependencies:
      sklearn: 1.9.1
        numpy: 2.4.6
        scipy: 1.17.1
       joblib: 1.6.0

Also worth noting for anyone who finds this from a search: cluster_selection_epsilon is the usual remedy
suggested for over-fragmented HDBSCAN output, so the crash tends to hit exactly the people who have gone
looking for it.

Activity

  1. added
    Needs Triage
    on Nov 20, 2026
  2. betatim commented on Nov 23, 2026

    @betatim
    Member

    Thanks for the report and the minimal reproducer. I can reproduce it on main.

    The cause is not the root lookup, though: traverse_upwards already handles parent == root. The problem is that lines 588 and 595 in _tree.pyx assign a one-element 1-D array slice to a typed Cython scalar:

        parent = cluster_tree[cluster_tree['child'] == leaf]['parent']
        ...
        parent_eps = 1 / cluster_tree[cluster_tree['child'] == parent]['value']
    

    NumPy used to allow this with a DeprecationWarning. Since NumPy 2.4 it raises TypeError. The epsilon threshold you saw only decides whether traverse_upwards is called at all.

    This makes cluster_selection_epsilon unusable with NumPy >= 2.4: any value either has no effect or crashes. Our tests don't reach traverse_upwards, which is why CI didn't catch it.

    The fix is to take the scalar explicitly ([0], as epsilon_search already does), plus a regression test that actually reaches traverse_upwards (your 4-point example works). A PR is welcome.

  3. added and removed
    Needs Triage
    on Nov 23, 2026
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