call(back)
Algorithms & Data StructureseasySign in to star problems

Precision, Recall and F1

Given counts of true positives tp, false positives fp and false negatives fn, return [precision, recall, f1]:

  • precision = tp / (tp + fp)
  • recall = tp / (tp + fn)
  • F1 = the harmonic mean of precision and recall, 2pr / (p + r)

Each is 0 when its denominator is 0. Results are compared after rounding to six decimals.

precisionRecallF1(8, 2, 4)  ->  [0.8, 0.666667, 0.727273]
precisionRecallF1(0, 0, 0)  ->  [0, 0, 0]

Hints

Hint 1

Guard every denominator: precision needs tp + fp, recall tp + fn, F1 p + r.

Hint 2

F1 is the harmonic mean, which punishes imbalance: precision 1 with recall 0 gives F1 0, not 0.5.

Asked at