CarlosMalaga's picture
Upload 201 files
2f044c1 verified
raw
history blame
548 Bytes
def safe_divide(num: float, den: float) -> float:
if den == 0:
return 0
else:
return num / den
def f1_measure(precision: float, recall: float) -> float:
if precision == 0 or recall == 0:
return 0.0
return safe_divide(2 * precision * recall, (precision + recall))
def compute_metrics(total_correct, total_preds, total_gold):
precision = safe_divide(total_correct, total_preds)
recall = safe_divide(total_correct, total_gold)
f1 = f1_measure(precision, recall)
return precision, recall, f1