Skip to content

FIX calinski_harabasz_score returns inf for perfectly separated clusters - #35389

Closed
Alm0stSurely wants to merge 1 commit into
scikit-learn:mainfrom
Alm0stSurely:fix/calinski-harabasz-perfect-separation
Closed

Alm0stSurely wants to merge 1 commit into
scikit-learn:mainfrom
Alm0stSurely:fix/calinski-harabasz-perfect-separation

Conversation

@Alm0stSurely

Copy link
Copy Markdown

Problem

calinski_harabasz_score returns 1.0 whenever within-cluster dispersion is zero, regardless of the between-cluster dispersion value. This conflates two distinct cases:

  1. Perfectly separated clusters (intra_disp == 0, extra_disp > 0): the score should approach +inf as intra_disp → 0⁺, but the current implementation returns 1.0.
  2. All samples identical (intra_disp == 0, extra_disp == 0): a 0/0 indeterminate form where returning 1.0 is a reasonable convention.

This means a clustering with three perfectly separated clusters can receive a lower score than a coarser two-cluster partition of the same data, potentially misleading cluster-count selection procedures.

Reproducer (from #35387):

from sklearn.metrics import calinski_harabasz_score

X = [[0], [0], [10], [10], [20], [20]]
print(calinski_harabasz_score(X, [0, 0, 1, 1, 2, 2]))  # 1.0 (should be inf)
print(calinski_harabasz_score(X, [0, 0, 1, 1, 1, 1]))  # 12.0

Analysis

The Calinski-Harabasz index is:

CH = (extra_disp / (n_labels - 1)) / (intra_disp / (n_samples - n_labels))

When intra_disp = 0 and extra_disp > 0, the numerator is positive and the denominator is zero, so the ratio diverges to +inf. The current implementation short-circuits to 1.0 on intra_disp == 0.0 without inspecting extra_disp, collapsing the two mathematically distinct cases into the same return value.

Solution

Distinguish the two zero-dispersion cases:

  • intra_disp == 0 and extra_disp == 0 → return 1.0 (preserve existing convention for indeterminate 0/0)
  • intra_disp == 0 and extra_disp > 0 → return float("inf") (consistent with the limiting behavior)

Benchmarks

Not a performance change — the additional branch is evaluated only when intra_disp == 0.0, which is a rare edge case.

Behavior verification:

Case Before After
Perfect separation [0,0,1,1,2,2] 1.0 inf
Coarser partition [0,0,1,1,1,1] 12.0 12.0 (unchanged)
All identical points 1.0 1.0 (unchanged)
Near-zero intra_disp (0.001) ~4e8 ~4e8 (unchanged)

Notes

  • The fix uses Python's float("inf") rather than xp.inf since the return value is already cast to Python float and the dispersions are Python scalars at this point.
  • A regression test asserts np.isinf on the perfectly separated case.
  • Full test_unsupervised.py suite passes (50 tests).

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

When intra-cluster dispersion is zero but between-cluster dispersion is
positive, the score now returns +inf instead of 1.0. This is consistent
with the limiting behavior as intra_disp approaches zero from above.

The case where both dispersions are zero (all samples identical)
preserves the existing 1.0 convention.

Fixes scikit-learn#35387
@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.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Nov 12, 2026
@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


cython-lint

cython-lint detected issues. Please fix them locally and push the changes. Here you can see the detected issues. Note that the installed cython-lint version is cython-lint=0.21.1.

Details

/home/runner/work/scikit-learn/scikit-learn/sklearn/manifold/_utils.pyx:74:13: Loop control variable 'l' not used within the loop body (if this is intended, start the name with an underscore)
/home/runner/work/scikit-learn/scikit-learn/sklearn/cluster/_k_means_common.pyx:30:9: Loop control variable 'i' not used within the loop body (if this is intended, start the name with an underscore)

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

@lorentzenchr

Copy link
Copy Markdown
Member

Thanks for the PR. The linked issue #35387 is still labelled "Needs Triage", meaning maintainers haven't yet agreed that the current behaviour is a bug or what the fix should be. Returning inf vs. 1.0 is a user-visible behaviour change that needs that discussion first. As per our contributing guide, I'm closing this PR.

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 module:metrics size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants