Fix .balanced_accuracy() returning NaN for single-class data#337
Draft
ishaan-arora-1 wants to merge 1 commit intostan-dev:masterfrom
Draft
Fix .balanced_accuracy() returning NaN for single-class data#337ishaan-arora-1 wants to merge 1 commit intostan-dev:masterfrom
.balanced_accuracy() returning NaN for single-class data#337ishaan-arora-1 wants to merge 1 commit intostan-dev:masterfrom
Conversation
When all observations belong to a single class (all 0s or all 1s), .balanced_accuracy() silently returns NaN for both estimate and se because mean() is called on an empty subset. This adds a guard clause that throws an informative error instead, since balanced accuracy is undefined when only one class is present.
Member
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #337 +/- ##
=======================================
Coverage 92.78% 92.78%
=======================================
Files 31 31
Lines 2992 2995 +3
=======================================
+ Hits 2776 2779 +3
Misses 216 216 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey! I noticed that
.balanced_accuracy()inloo_predictive_metric.Rsilently returnsNaNwhen all observations belong to a single class (e.g., all 1s or all 0s). This happens becausemean()gets called on an empty subset —yhat[mask]is length 0 when there are no negatives (or no positives), andmean(integer(0))givesNaN.Quick repro:
Since balanced accuracy is inherently undefined with only one class present, this PR adds a guard clause that throws a clear error message instead of silently returning garbage values. Also added tests for both single-class cases (all 0s and all 1s).
All existing tests still pass.