Skip to content

FIX HDBSCAN TypeError in traverse_upwards when cluster_selection_epsilon >= merge distance (#35473) - #35499

Open
prashik24 wants to merge 5 commits into
scikit-learn:mainfrom
prashik24:fix-hdbscan-traverse-upwards-scalar
Open

prashik24 wants to merge 5 commits into
scikit-learn:mainfrom
prashik24:fix-hdbscan-traverse-upwards-scalar

Conversation

@prashik24

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #35473

What does this implement/fix? Explain your changes.

In sklearn/cluster/_hdbscan/_tree.pyx, traverse_upwards assigned 1-D array slices (cluster_tree[cluster_tree['child'] == leaf]['parent'] and cluster_tree[cluster_tree['child'] == parent]['value']) directly to typed C scalars (cnp.intp_t and cnp.float64_t).

In NumPy >= 2.4, converting a non-zero-dimensional array to a scalar raises TypeError: only 0-dimensional arrays can be converted to Python scalars, causing HDBSCAN.fit_predict to crash whenever cluster_selection_epsilon >= top-level merge distance.

This PR explicitly extracts the scalar element with [0] (matching epsilon_search), preventing the TypeError.

Any other comments?

  • Added regression test test_hdbscan_cluster_selection_epsilon_large in sklearn/cluster/tests/test_hdbscan.py testing both eom and leaf with allow_single_cluster true and false.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

Comment thread sklearn/cluster/tests/test_hdbscan.py Resolved
@prashik24

Copy link
Copy Markdown
Author

Thanks for the review and suggestions, @betatim!

I have updated:

  1. The test assertion to verify the expected cluster labels ([0, 0, 0, 0] vs [0, 0, 1, 1]) and updated the test docstring.
  2. The release note in whats_new with your suggested wording.

All CI checks are green

@betatim betatim added the Waiting for Second Reviewer First reviewer is done, need a second one! label Nov 27, 2026

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: TypeError from traverse_upwards when cluster_selection_epsilon >= the top-level merge distance (both eom and leaf)

2 participants