Skip to content

Fix infinite Calinski-Harabasz score for perfect clusters - #35390

Closed
Surja2003 wants to merge 1 commit into
scikit-learn:mainfrom
Surja2003:fix-calinski-harabasz-infinite-score
Closed

Surja2003 wants to merge 1 commit into
scikit-learn:mainfrom
Surja2003:fix-calinski-harabasz-infinite-score

Conversation

@Surja2003

Copy link
Copy Markdown

Reference Issues/PRs

Related to #35387.

What does this implement/fix? Explain your changes.

The calinski_harabasz_score function currently returns 1.0 whenever the within-cluster dispersion (intra_disp) is zero.

However, when clusters are perfectly separated, the within-cluster dispersion is zero while the between-cluster dispersion (extra_disp) is positive. In this case, the score should be infinite.

This PR:

  • Returns float("inf") when intra_disp == 0.0 and extra_disp > 0.0.
  • Preserves the existing return value of 1.0 when both dispersions are zero.
  • Adds a regression test covering perfectly separated clusters.

The existing scoring formula remains unchanged for nonzero within-cluster dispersion.

Introduce yourself

Hello! I am a Computer Science and Engineering student specializing in Artificial Intelligence and Data Science.

I am interested in machine learning, data science, and open-source software development. This is my first contribution to scikit-learn.

I found this issue while exploring the clustering metrics implementation. I reproduced the behavior locally, investigated the underlying calculation, implemented a fix, and added a regression test.

I look forward to learning from the scikit-learn community and receiving feedback from the maintainers.

AI usage disclosure

I used AI assistance for:

  • Code generation and implementation guidance.
  • Regression test generation.
  • Research and understanding of the issue and existing implementation.

I independently reproduced the issue, ran the tests, reviewed the changes, and validated the implementation locally.

Any other comments?

Validation performed:

  • Targeted regression test: 1 passed.
  • Complete test module (test_unsupervised.py): 50 passed.
  • Ruff check: passed.
  • Ruff format: passed.
  • Pyrefly: passed.
  • Codespell: passed.
  • git diff --check: passed.

I would appreciate maintainer feedback on the expected behavior for the zero within-cluster dispersion case, particularly when both dispersions are zero.

@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.

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.

1 participant