Skip to content

FIX HDBSCAN crash with cluster_selection_epsilon on tied distances - #33276

Closed
Mr-Neutr0n wants to merge 1 commit into
scikit-learn:mainfrom
Mr-Neutr0n:fix/hdbscan-epsilon-tied-distances
Closed

Mr-Neutr0n wants to merge 1 commit into
scikit-learn:mainfrom
Mr-Neutr0n:fix/hdbscan-epsilon-tied-distances

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Fixes #33219

What does this implement/fix?

HDBSCAN.fit_predict() raises TypeError: only 0-dimensional arrays can be converted to Python scalars when cluster_selection_epsilon is set and the input data contains tied distances.

The root cause is in traverse_upwards in _tree.pyx. Tied distances produce duplicate entries in the condensed tree, so filtering by child id can return multiple rows instead of one. The code assumed a scalar result from these lookups, which blows up when it gets an array instead.

The fix indexes [0] on both the parent lookup and the parent_eps computation. All duplicate entries for a given child share the same parent and lambda value, so taking the first match is correct.

How has this been tested?

Added a non-regression test test_hdbscan_cluster_selection_epsilon_tied_distances using both:

  • The exact precomputed distance matrix from the issue report
  • The synthetic Euclidean reproducer from the issue

When the condensed tree contains duplicate child entries (caused by tied
distances in the input data), filtering by child id can return multiple
rows instead of a single row. The traverse_upwards function assumed a
scalar result from these lookups, which raised a TypeError when an array
was returned.

Index into the first match with [0] for both the parent lookup and the
parent_eps computation, since all duplicate entries share the same parent
and lambda value.

Adds a non-regression test with the exact data from the issue report.

Closes #33219
@github-actions

Copy link
Copy Markdown

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff check

ruff detected issues. Please run ruff check --fix --output-format=full locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

sklearn/cluster/tests/test_hdbscan.py:620:89: E501 Line too long (122 > 88)
    |
618 |     X_precomputed = np.array(
619 |         [
620 |             [0.0, 0.0144234, 0.014608, 0.018432, 0.2028331, 1e-06, 0.0146122, 0.0108745, 0.0238127, 0.0289057, 0.0292668],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
621 |             [0.0144234, 0.0, 0.030085, 0.0341493, 0.2174494, 0.0185183, 0.0300855, 0.0261329, 0.0444496, 0.0499432, 0.0506157],
622 |             [0.014608, 0.030085, 0.0, 0.0224902, 0.2284748, 0.0188262, 0.022492, 0.0147419, 0.029195, 0.0344591, 0.0349043],
    |

sklearn/cluster/tests/test_hdbscan.py:621:89: E501 Line too long (127 > 88)
    |
619 |         [
620 |             [0.0, 0.0144234, 0.014608, 0.018432, 0.2028331, 1e-06, 0.0146122, 0.0108745, 0.0238127, 0.0289057, 0.0292668],
621 |             [0.0144234, 0.0, 0.030085, 0.0341493, 0.2174494, 0.0185183, 0.0300855, 0.0261329, 0.0444496, 0.0499432, 0.0506157],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
622 |             [0.014608, 0.030085, 0.0, 0.0224902, 0.2284748, 0.0188262, 0.022492, 0.0147419, 0.029195, 0.0344591, 0.0349043],
623 |             [0.018432, 0.0341493, 0.0224902, 0.0, 0.2288503, 0.0238127, 0.0035258, 0.0147674, 1e-06, 0.0045079, 0.0045573],
    |

sklearn/cluster/tests/test_hdbscan.py:622:89: E501 Line too long (124 > 88)
    |
620 |             [0.0, 0.0144234, 0.014608, 0.018432, 0.2028331, 1e-06, 0.0146122, 0.0108745, 0.0238127, 0.0289057, 0.0292668],
621 |             [0.0144234, 0.0, 0.030085, 0.0341493, 0.2174494, 0.0185183, 0.0300855, 0.0261329, 0.0444496, 0.0499432, 0.0506157],
622 |             [0.014608, 0.030085, 0.0, 0.0224902, 0.2284748, 0.0188262, 0.022492, 0.0147419, 0.029195, 0.0344591, 0.0349043],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
623 |             [0.018432, 0.0341493, 0.0224902, 0.0, 0.2288503, 0.0238127, 0.0035258, 0.0147674, 1e-06, 0.0045079, 0.0045573],
624 |             [0.2028331, 0.2174494, 0.2284748, 0.2288503, 0.0, 0.2061707, 0.220841, 0.2293228, 0.2397258, 0.2299638, 0.2552726],
    |

sklearn/cluster/tests/test_hdbscan.py:623:89: E501 Line too long (123 > 88)
    |
621 |             [0.0144234, 0.0, 0.030085, 0.0341493, 0.2174494, 0.0185183, 0.0300855, 0.0261329, 0.0444496, 0.0499432, 0.0506157],
622 |             [0.014608, 0.030085, 0.0, 0.0224902, 0.2284748, 0.0188262, 0.022492, 0.0147419, 0.029195, 0.0344591, 0.0349043],
623 |             [0.018432, 0.0341493, 0.0224902, 0.0, 0.2288503, 0.0238127, 0.0035258, 0.0147674, 1e-06, 0.0045079, 0.0045573],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
624 |             [0.2028331, 0.2174494, 0.2284748, 0.2288503, 0.0, 0.2061707, 0.220841, 0.2293228, 0.2397258, 0.2299638, 0.2552726],
625 |             [1e-06, 0.0185183, 0.0188262, 0.0238127, 0.2061707, 0.0, 0.0188313, 0.013986, 0.0238127, 0.0289057, 0.0292668],
    |

sklearn/cluster/tests/test_hdbscan.py:624:89: E501 Line too long (127 > 88)
    |
622 |             [0.014608, 0.030085, 0.0, 0.0224902, 0.2284748, 0.0188262, 0.022492, 0.0147419, 0.029195, 0.0344591, 0.0349043],
623 |             [0.018432, 0.0341493, 0.0224902, 0.0, 0.2288503, 0.0238127, 0.0035258, 0.0147674, 1e-06, 0.0045079, 0.0045573],
624 |             [0.2028331, 0.2174494, 0.2284748, 0.2288503, 0.0, 0.2061707, 0.220841, 0.2293228, 0.2397258, 0.2299638, 0.2552726],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
625 |             [1e-06, 0.0185183, 0.0188262, 0.0238127, 0.2061707, 0.0, 0.0188313, 0.013986, 0.0238127, 0.0289057, 0.0292668],
626 |             [0.0146122, 0.0300855, 0.022492, 0.0035258, 0.220841, 0.0188313, 0.0, 0.0186034, 0.0044997, 0.0091034, 0.0092055],
    |

sklearn/cluster/tests/test_hdbscan.py:625:89: E501 Line too long (123 > 88)
    |
623 |             [0.018432, 0.0341493, 0.0224902, 0.0, 0.2288503, 0.0238127, 0.0035258, 0.0147674, 1e-06, 0.0045079, 0.0045573],
624 |             [0.2028331, 0.2174494, 0.2284748, 0.2288503, 0.0, 0.2061707, 0.220841, 0.2293228, 0.2397258, 0.2299638, 0.2552726],
625 |             [1e-06, 0.0185183, 0.0188262, 0.0238127, 0.2061707, 0.0, 0.0188313, 0.013986, 0.0238127, 0.0289057, 0.0292668],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
626 |             [0.0146122, 0.0300855, 0.022492, 0.0035258, 0.220841, 0.0188313, 0.0, 0.0186034, 0.0044997, 0.0091034, 0.0092055],
627 |             [0.0108745, 0.0261329, 0.0147419, 0.0147674, 0.2293228, 0.013986, 0.0186034, 0.0, 0.0190924, 0.0241502, 0.0244496],
    |

sklearn/cluster/tests/test_hdbscan.py:626:89: E501 Line too long (126 > 88)
    |
624 |             [0.2028331, 0.2174494, 0.2284748, 0.2288503, 0.0, 0.2061707, 0.220841, 0.2293228, 0.2397258, 0.2299638, 0.2552726],
625 |             [1e-06, 0.0185183, 0.0188262, 0.0238127, 0.2061707, 0.0, 0.0188313, 0.013986, 0.0238127, 0.0289057, 0.0292668],
626 |             [0.0146122, 0.0300855, 0.022492, 0.0035258, 0.220841, 0.0188313, 0.0, 0.0186034, 0.0044997, 0.0091034, 0.0092055],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
627 |             [0.0108745, 0.0261329, 0.0147419, 0.0147674, 0.2293228, 0.013986, 0.0186034, 0.0, 0.0190924, 0.0241502, 0.0244496],
628 |             [0.0238127, 0.0444496, 0.029195, 1e-06, 0.2397258, 0.0238127, 0.0044997, 0.0190924, 0.0, 0.0045079, 0.0045573],
    |

sklearn/cluster/tests/test_hdbscan.py:627:89: E501 Line too long (127 > 88)
    |
625 |             [1e-06, 0.0185183, 0.0188262, 0.0238127, 0.2061707, 0.0, 0.0188313, 0.013986, 0.0238127, 0.0289057, 0.0292668],
626 |             [0.0146122, 0.0300855, 0.022492, 0.0035258, 0.220841, 0.0188313, 0.0, 0.0186034, 0.0044997, 0.0091034, 0.0092055],
627 |             [0.0108745, 0.0261329, 0.0147419, 0.0147674, 0.2293228, 0.013986, 0.0186034, 0.0, 0.0190924, 0.0241502, 0.0244496],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
628 |             [0.0238127, 0.0444496, 0.029195, 1e-06, 0.2397258, 0.0238127, 0.0044997, 0.0190924, 0.0, 0.0045079, 0.0045573],
629 |             [0.0289057, 0.0499432, 0.0344591, 0.0045079, 0.2299638, 0.0289057, 0.0091034, 0.0241502, 0.0045079, 0.0, 0.0092227],
    |

sklearn/cluster/tests/test_hdbscan.py:628:89: E501 Line too long (123 > 88)
    |
626 |             [0.0146122, 0.0300855, 0.022492, 0.0035258, 0.220841, 0.0188313, 0.0, 0.0186034, 0.0044997, 0.0091034, 0.0092055],
627 |             [0.0108745, 0.0261329, 0.0147419, 0.0147674, 0.2293228, 0.013986, 0.0186034, 0.0, 0.0190924, 0.0241502, 0.0244496],
628 |             [0.0238127, 0.0444496, 0.029195, 1e-06, 0.2397258, 0.0238127, 0.0044997, 0.0190924, 0.0, 0.0045079, 0.0045573],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
629 |             [0.0289057, 0.0499432, 0.0344591, 0.0045079, 0.2299638, 0.0289057, 0.0091034, 0.0241502, 0.0045079, 0.0, 0.0092227],
630 |             [0.0292668, 0.0506157, 0.0349043, 0.0045573, 0.2552726, 0.0292668, 0.0092055, 0.0244496, 0.0045573, 0.0092227, 0.0],
    |

sklearn/cluster/tests/test_hdbscan.py:629:89: E501 Line too long (128 > 88)
    |
627 |             [0.0108745, 0.0261329, 0.0147419, 0.0147674, 0.2293228, 0.013986, 0.0186034, 0.0, 0.0190924, 0.0241502, 0.0244496],
628 |             [0.0238127, 0.0444496, 0.029195, 1e-06, 0.2397258, 0.0238127, 0.0044997, 0.0190924, 0.0, 0.0045079, 0.0045573],
629 |             [0.0289057, 0.0499432, 0.0344591, 0.0045079, 0.2299638, 0.0289057, 0.0091034, 0.0241502, 0.0045079, 0.0, 0.0092227],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
630 |             [0.0292668, 0.0506157, 0.0349043, 0.0045573, 0.2552726, 0.0292668, 0.0092055, 0.0244496, 0.0045573, 0.0092227, 0.0],
631 |         ]
    |

sklearn/cluster/tests/test_hdbscan.py:630:89: E501 Line too long (128 > 88)
    |
628 |             [0.0238127, 0.0444496, 0.029195, 1e-06, 0.2397258, 0.0238127, 0.0044997, 0.0190924, 0.0, 0.0045079, 0.0045573],
629 |             [0.0289057, 0.0499432, 0.0344591, 0.0045079, 0.2299638, 0.0289057, 0.0091034, 0.0241502, 0.0045079, 0.0, 0.0092227],
630 |             [0.0292668, 0.0506157, 0.0349043, 0.0045573, 0.2552726, 0.0292668, 0.0092055, 0.0244496, 0.0045573, 0.0092227, 0.0],
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
631 |         ]
632 |     )
    |

Found 11 errors.

ruff format

ruff detected issues. Please run ruff format locally and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

--- sklearn/cluster/tests/test_hdbscan.py
+++ sklearn/cluster/tests/test_hdbscan.py
@@ -617,17 +617,149 @@
     # Minimal reproducer from the issue (precomputed distance matrix).
     X_precomputed = np.array(
         [
-            [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],
+            [
+                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,
+            ],
         ]
     )
 
@@ -645,10 +777,12 @@
 
     # Reproducer with synthetic Euclidean data from the issue.
     rng = np.random.default_rng(0)
-    X_euclidean = np.vstack([
-        rng.normal(0, 0.2, size=(20, 2)),
-        rng.normal(1, 0.2, size=(21, 2)),
-    ])
+    X_euclidean = np.vstack(
+        [
+            rng.normal(0, 0.2, size=(20, 2)),
+            rng.normal(1, 0.2, size=(21, 2)),
+        ]
+    )
     hdb2 = HDBSCAN(
         min_cluster_size=5,
         min_samples=1,

1 file would be reformatted, 931 files already formatted

Generated for commit: f94cf67. Link to the linter CI: here

@Mr-Neutr0n

Copy link
Copy Markdown
Author

closing. check comment in parent issue!

@Mr-Neutr0n Mr-Neutr0n closed this Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Linter failure The linter CI is failing on this PR cython module:cluster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HDBSCAN fails when using cluster_selection_epsilon

1 participant