feat: add zero_division parameter to F1 metric#753
Open
YousefZahran1 wants to merge 1 commit intohuggingface:mainfrom
Open
feat: add zero_division parameter to F1 metric#753YousefZahran1 wants to merge 1 commit intohuggingface:mainfrom
YousefZahran1 wants to merge 1 commit intohuggingface:mainfrom
Conversation
sklearn.metrics.f1_score supports zero_division to control the value returned when a label has no predicted or true samples (UndefinedMetricWarning case). The evaluate F1 metric did not expose this argument, causing a TypeError for callers who tried to pass it — even though sklearn's own warning message tells them to do exactly that. precision and recall already accept zero_division; this brings F1 into parity. Default value is 'warn' to preserve backward compatibility. Adds Example 6 to _KWARGS_DESCRIPTION demonstrating the parameter. Fixes huggingface#699
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.
What
precisionandrecallboth accept azero_divisionparameter that controls what value is returned when a label has no predicted or true samples.f1does not, which causes:UndefinedMetricWarningfor F1 explicitly tells users to passzero_division, but the evaluate wrapper silently drops it with aTypeError.precisionandrecallhave it;f1doesn't).Reproduce the bug
Fix
Add
zero_division="warn"toF1._compute()(matching the default inprecisionandrecall) and pass it through tosklearn.metrics.f1_score.Changes
metrics/f1/f1.py: addzero_divisionargument to_compute(), document it in_KWARGS_DESCRIPTION, and add Example 6 showing the parameter in action.Testing
Fixes #699