Skip to content
Snippets Groups Projects
Commit bd4e2a31 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[utils.measure] Return tuple instead of list for base measures; Formalize special by-zero division

parent 4a219b48
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,12 @@ class SmoothedValue: ...@@ -28,6 +28,12 @@ class SmoothedValue:
return d.mean().item() return d.mean().item()
def tricky_division(n, d):
"""Divides n by d. Returns 0.0 in case of a division by zero"""
return n/(d+(d==0))
def base_measures(tp, fp, tn, fn): def base_measures(tp, fp, tn, fn):
"""Calculates measures from true/false positive and negative counts """Calculates measures from true/false positive and negative counts
...@@ -105,16 +111,14 @@ def base_measures(tp, fp, tn, fn): ...@@ -105,16 +111,14 @@ def base_measures(tp, fp, tn, fn):
""" """
tp = float(tp) return (
tn = float(tn) tricky_division(tp, tp + fp), #precision
precision = tp / (tp + fp + ((tp + fp) == 0)) tricky_division(tp, tp + fn), #recall
recall = tp / (tp + fn + ((tp + fn) == 0)) tricky_division(tn, fp + tn), #specificity
specificity = tn / (fp + tn + ((fp + tn) == 0)) tricky_division(tp + tn, tp + fp + fn + tn), #accuracy
accuracy = (tp + tn) / (tp + fp + fn + tn) tricky_division(tp, tp + fp + fn), #jaccard index
jaccard = tp / (tp + fp + fn + ((tp + fp + fn) == 0)) tricky_division(2*tp, (2*tp) + fp + fn), #f1-score
f1_score = (2.0 * tp) / (2.0 * tp + fp + fn + ((2.0 * tp + fp + fn) == 0)) )
# f1_score = (2.0 * precision * recall) / (precision + recall)
return [precision, recall, specificity, accuracy, jaccard, f1_score]
def auc(x, y): def auc(x, y):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment