Repository navigation
Conversation
`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
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 was referenced Oct 23, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
HDBSCANwith a non-zerocluster_selection_epsilonraised on NumPy 2.x:In
sklearn/cluster/_hdbscan/_tree.pyx,traverse_upwardsassigned the resultof a boolean-mask lookup (
cluster_tree[...]['parent']and['value']) directlyto 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, forboth
eomandleafselection 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
hdbscanpackage referenced in the issue.A non-regression test (
test_hdbscan_epsilon_merges_micro_clusters, parametrizedover
eom/leaf) reproduces the crash on hierarchical data (tight micro-clustersnested 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
TypeErrorbefore the fix and passes after.First time contributor introduction
Hi! I use scikit-learn's
HDBSCANto cluster text-embedding vectors (for amisinformation / fact-checking project), reducing with UMAP first. I hit this
crash when enabling
cluster_selection_epsilonto 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:
Any other comments?
The change is minimal and
traverse_upwardsis exercised only on the epsilonpath. Full
sklearn/cluster/tests/test_hdbscan.pypasses (150 tests), andruff/cython-lintare clean.