Fix TypeError in macro f1 when zero_division=0.0 - #168
Conversation
_compute_macro dispatched with `if zero_division:`, which is falsy for the
valid, documented value 0.0 (and 0), so it called _f_score_micro without the
required zero_division argument and raised TypeError. The other documented
values ("warn", 1.0, np.nan) are truthy, which is why it went unnoticed.
Guard on `if zero_division is not None:` instead; precision and recall macro
keep the None default path. Added explicit zero_division=0.0 coverage to all
five macro f1 tests.
|
Note on the failing |
|
Hi @mirand863, I have three small fixes open here, would you have time to look?
The red readthedocs check on all three is the docs build rather than the test suite, and it fails the same way on PRs that aren't mine. |
Problem
f1(y_true, y_pred, average="macro", zero_division=0.0)raises:0.0is a documented, valid value forzero_division(the docstring lists{"warn", 0.0, 1.0, np.nan}), so this should work the same as the other options.Root cause
In
_compute_macro, the per-sample dispatch guards with a truthiness check:The intent of that branch is "if the micro function accepts a
zero_divisionargument, forward it."precision/recallmacro leave the argument at itsNonedefault and correctly take the 2-argument path. But the f-score path always passes a value, and0.0(and0) is falsy, so it wrongly falls through to the 2-argument call, and_f_score_microrequires the third argument. The other documented values ("warn",1.0,np.nan) are all truthy, which is why this went unnoticed.Fix
Guard on
is not Noneinstead of truthiness:precision/recallmacro still take theNonedefault path; the f-score path now forwards0.0correctly. No other behavior changes.Tests
The existing macro tests exercised
zero_divisionvia the default"warn", plus1.0andnp.nan, but never the explicit0.0, which is exactly the gap that let this ship. Addedassert 0.0 == f1(y_true, y_pred, "macro", 0.0)to all five macrof1tests (1d/2d/3d, list and array).Verified: reproduced the
TypeErrorbefore the change, confirmed it is gone after, confirmed explicit0.0returns0.0with no spurious warning, and confirmedprecision/recallmacro are unaffected. Fulltests/test_metrics.pypasses (63/63).Closes #161