Skip to content

Return 0 from binary F1 when precision and recall are 0 - #551

Open
bardrop wants to merge 1 commit into
smartcorelib:mainfrom
bardrop-oss:fix-f1-binary-nan
Open

bardrop wants to merge 1 commit into
smartcorelib:mainfrom
bardrop-oss:fix-f1-binary-nan

Conversation

@bardrop

@bardrop bardrop commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #550

Checklist

  • My branch is up-to-date with main branch.
  • Everything works and tested on latest stable Rust.
  • Coverage and Linting have been applied

Current behaviour

For binary labels, F1::get_score computes (1 + beta²) * p * r / (beta² * p + r) from Precision and Recall. With no true positives both are 0, so the score is 0 / 0 = NaN. For example y_true = [0, 1, 0, 1] and y_pred = [0, 0, 0, 0] gives NaN. scikit-learn gives 0.0, and the multiclass path in F1 already gives 0 for such a class.

New expected behaviour

When beta² * p + r is 0, the binary score is 0.0. Other inputs give the same result as before.

New test f1_binary_no_true_positives covers no positive predictions and all-wrong predictions, for beta 0.5, 1 and 2. It fails on main (left: NaN, right: 0.0) and passes here. cargo test --lib metrics::f1: 7 passed.

Change logs

Changed

  • metrics: binary F1 returns 0 when precision and recall are both 0. Before this change, it returned NaN.

@bardrop
bardrop requested a review from Mec-iS as a code owner November 30, 2026 02:01
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.28%. Comparing base (9eaae9e) to head (b7838a2).
⚠️ Report is 232 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #551       +/-   ##
===========================================
+ Coverage   43.97%   87.28%   +43.30%     
===========================================
  Files          85      100       +15     
  Lines        7281    10002     +2721     
===========================================
+ Hits         3202     8730     +5528     
+ Misses       4079     1272     -2807     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary F1 returns NaN when there are no true positives

1 participant