FIX raise ValueError in compute_class_weight for all-zero class weigh…#34199
FIX raise ValueError in compute_class_weight for all-zero class weigh…#34199Shahaborakzai wants to merge 5 commits into
Conversation
|
⏰ 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 |
…ts - Closes #34139
Reference Issues/PRs
What does this implement/fix? Explain your changes.
AI usage disclosure
I used AI assistance for:
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..."