Skip to content

Commit

Permalink
NER example: fix divisions by zero (apache#15068)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamTambellini authored and Mike Mao committed Jun 5, 2019
1 parent e2982fe commit 546e992
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions example/named_entity_recognition/src/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ def classifer_metrics(label, pred):
correct_entitites = np.sum(corr_pred[pred_is_entity])

#precision: when we predict entity, how often are we right?
precision = correct_entitites/entity_preds
if entity_preds == 0:
precision = np.nan
else:
precision = correct_entitites/entity_preds

#recall: of the things that were an entity, how many did we catch?
recall = correct_entitites / num_entities
if num_entities == 0:
recall = np.nan
f1 = 2 * precision * recall / (precision + recall)
# To prevent dozens of warning: RuntimeWarning: divide by zero encountered in long_scalars
if precision + recall == 0:
f1 = 0
else:
f1 = 2 * precision * recall / (precision + recall)
return precision, recall, f1

def entity_precision(label, pred):
Expand Down

0 comments on commit 546e992

Please sign in to comment.