Skip to content

Fix HDBSCAN with non-zero cluster_selection_epsilon on NumPy 2.x - #34243

Open
CptK wants to merge 3 commits into
scikit-learn:mainfrom
CptK:fix-hdbscan-epsilon-numpy2-33219
Open

CptK wants to merge 3 commits into
scikit-learn:mainfrom
CptK:fix-hdbscan-epsilon-numpy2-33219

Conversation

@CptK

@CptK CptK commented Jun 10, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #33219.
(#33355 was closed as a duplicate of the same NumPy 2.x issue.)

What does this implement/fix? Explain your changes.

Fitting HDBSCAN with a non-zero cluster_selection_epsilon raised on NumPy 2.x:

  TypeError: only 0-dimensional arrays can be converted to Python scalars

In sklearn/cluster/_hdbscan/_tree.pyx, traverse_upwards assigned the result
of a boolean-mask lookup (cluster_tree[...]['parent'] and ['value']) directly
to a typed scalar. Those lookups return a size-1 array, and NumPy 2.x no longer
permits the implicit size-1-array-to-scalar conversion that earlier versions
silently allowed, so the assignment raised.

This only affects the epsilon code path (epsilon_search → traverse_upwards),
which is why it surfaces solely with a non-zero cluster_selection_epsilon, for
both eom and leaf selection methods.

The fix indexes the size-1 arrays explicitly with [0] to extract the scalar.
This restores the documented epsilon-merging behavior and matches the output of
the standalone hdbscan package referenced in the issue.

A non-regression test (test_hdbscan_epsilon_merges_micro_clusters, parametrized
over eom/leaf) reproduces the crash on hierarchical data (tight micro-clusters
nested inside far-apart super-clusters) and asserts that a large enough epsilon
merges them back into the expected super-clusters. It fails with the original
TypeError before the fix and passes after.

First time contributor introduction

Hi! I use scikit-learn's HDBSCAN to cluster text-embedding vectors (for a
misinformation / fact-checking project), reducing with UMAP first. I hit this
crash when enabling cluster_selection_epsilon to merge nearby micro-clusters,
traced it to the size-1-array conversion removed in NumPy 2.x, and figured a
one-line fix plus a regression test was a good first contribution.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

The change is minimal and traverse_upwards is exercised only on the epsilon
path. Full sklearn/cluster/tests/test_hdbscan.py passes (150 tests), and
ruff/cython-lint are clean.

`traverse_upwards` assigned the result of a boolean-mask lookup
(`cluster_tree[...]['parent']` / `['value']`) directly to a typed scalar.
These lookups return a size-1 array, and NumPy 2.x no longer allows the
implicit size-1-array-to-scalar conversion, so fitting `HDBSCAN` with a
non-zero `cluster_selection_epsilon` raised:

    TypeError: only 0-dimensional arrays can be converted to Python scalars

Index the size-1 arrays explicitly with `[0]` to extract the scalar.

Adds a non-regression test on hierarchical data that exercises the
`epsilon_search`/`traverse_upwards` path for both `eom` and `leaf`
selection methods.

Fixes scikit-learn#33219
@lorentzenchr

Copy link
Copy Markdown
Member

@betatim Could you have a look here?

joaofnds added a commit to joaofnds/dewey that referenced this pull request Sep 21, 2026
scikit-learn 1.9.1's HDBSCAN raises "only 0-dimensional arrays can be
converted to Python scalars" on NumPy 2.x whenever a non-zero
cluster_selection_epsilon actually merges clusters (the epsilon_search
path in sklearn/cluster/_hdbscan/_tree.pyx). The fix is open upstream as
scikit-learn/scikit-learn#34243. Until it lands the only usable value is
0.0, which is also the default, so the setting is removed rather than
exposed as a knob that crashes. On the 2,872-summary dataset the old
0.25 was tuned for a 3-D space this pipeline no longer clusters in.

Clustering.members returns a list of ints so callers index plain Python
values, and copy=True is passed explicitly ahead of scikit-learn 1.10
changing the default.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HDBSCAN fails when using cluster_selection_epsilon

2 participants