Skip to content

FIX raise ValueError in compute_class_weight for all-zero class weigh…#34199

Open
Shahaborakzai wants to merge 5 commits into
scikit-learn:mainfrom
Shahaborakzai:fix/34139-all-zero-class-weights
Open

FIX raise ValueError in compute_class_weight for all-zero class weigh…#34199
Shahaborakzai wants to merge 5 commits into
scikit-learn:mainfrom
Shahaborakzai:fix/34139-all-zero-class-weights

Conversation

@Shahaborakzai
Copy link
Copy Markdown

…ts - Closes #34139

Reference Issues/PRs

What does this implement/fix? Explain your changes.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

Description

Closes #34139

What was the bug?

When compute_class_weight("balanced", ..., sample_weight=...) is called
and all sample weights for a given class are zero, the function performs
a division by zero, silently returning inf or nan instead of raising
a clear error.

What changed?

Added a check after computing weighted_class_counts to detect any class
with a total weight of zero and raise a clear ValueError with the
affected class labels listed.

How to reproduce the original bug

import numpy as np
from sklearn.utils.class_weight import compute_class_weight
y = np.array([0, 0, 1, 1, 2, 2])
classes = np.unique(y)
sample_weight = np.array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0])
compute_class_weight("balanced", classes=classes, y=y, sample_weight=sample_weight)

Before fix: returns inf/nan silently

After fix: raises clear ValueError

Test

Verified fix works locally - ValueError is correctly raised with message:
"Classes [2] have all-zero sample weights..."

@lorentzenchr lorentzenchr added the autoclose PR automatically closed 14 days after setting the label label Jun 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 4, 2026

⏰ This pull request might be automatically closed in two weeks from now.

Thank you for your contribution to scikit-learn and for the effort you have put into this PR. This pull request does not yet meet the quality and clarity needed for an effective review. Project maintainers have limited time for code reviews, and our goal is to prioritize well-prepared contributions to keep scikit-learn maintainable.

To increase the chance of a productive review, please refer to: How do I improve my issue or pull request? As the author, you are responsible for driving this PR, which entails doing necessary background research as well as presenting its context and your thought process. If you are a new contributor, or do not know how to fulfill these requirements, we recommend that you familiarise yourself with scikit-learn's development conventions via other contribution types (e.g., reviewing PRs) before submitting code.

Scikit-learn maintainers cannot provide one-to-one guidance on this PR. However, if you ask focused, well-researched questions, a community member may be willing to help. 💬

If you substantially improve this PR within two weeks, a team member may remove the autoclose label and the PR stays open. Cosmetic changes or incomplete fixes will not be sufficient. Maintainers will assess improvements on their own schedule. Please do not ping (@) maintainers.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Jun 4, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autoclose PR automatically closed 14 days after setting the label module:utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling of classes with all-zero weights

2 participants