Skip to main content

Medium evaluation · metrics

Precision and recall

Two honest numbers for how a yes-or-no model actually behaves.

Accuracy alone can hide a bad model. Precision asks: of everything the model flagged, how much was right? Recall asks: of everything that should have been flagged, how much did it catch?

Write precisionRecall(predicted, actual): two arrays of 0s and 1s with the same length. Count true positives (predicted 1, actual 1), false positives (predicted 1, actual 0) and false negatives (predicted 0, actual 1). Return [precision, recall] where precision = tp / (tp + fp) and recall = tp / (tp + fn). When a bottom is 0, that number is 0.

Call show([tp, fp, fn]) and the stage draws the three counts as bars.

Example: precisionRecall([1, 1, 0, 0], [1, 0, 1, 0]) returns [0.5, 0.5].

Tests you can see

  • precisionRecall([1,1,0,0], [1,0,1,0])[0.5,0.5]
  • precisionRecall([1,0,0,0,0], [1,1,1,0,0])[1,0.3333333333333333]
  • precisionRecall([1,1,1,1], [1,0,0,1])[0.5,1]

More tests are hidden. The server runs them all before a time counts.

The idea behind this, in a two-minute live page

Call show() while your code works and the run plays back here.

javascriptprecisionRecall()

Tab indents · Cmd/Ctrl+Enter runs