Skip to content

Commit

Permalink
Fix F1 sampling error when no positive cases
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-nml committed May 2, 2024
1 parent a793cf4 commit 730ae35
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nannyml/sampling_error/binary_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def f1_sampling_error_components(y_true_reference: pd.Series, y_pred_reference:

tp_fp_fn = np.concatenate([TP, FN, FP])

# If there's no true positives, false negatives or false positives, sampling error is NaN
if tp_fp_fn.size == 0:
return np.nan, 0

correcting_factor = len(tp_fp_fn) / ((len(FN) + len(FP)) * 0.5 + len(TP))
obs_level_f1 = tp_fp_fn * correcting_factor
fraction_of_relevant = len(tp_fp_fn) / len(y_pred_reference)
Expand Down

0 comments on commit 730ae35

Please sign in to comment.