Skip to content
mlmentorship

Binary Precision and Recall

Compute precision and recall from binary labels and predictions.

Published · 4 min read ·Role-specific ·Intermediate

30-second answer map

Visual first · depth when needed

Build TP, FP, and FN with elementwise Boolean masks, then divide TP by predicted-positive and actual-positive totals with zero-denominator guards.

Preparing the visual…

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

How practice works

ML breadth · closed-book attempt

Binary Precision and Recall

Explain the mechanism, why it works, when it fails, and one alternative.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Compute precision and recall from binary labels and predictions.

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

Binary Precision and Recall: Build TP, FP, and FN with elementwise Boolean masks, then divide TP by predicted-positive and actual-positive totals with zero-denominator guards.

Input and goalCompute precision and recall from binary labels and predictions.
Classify index 0 as true positivelabel=1 and prediction=1 makes both equality masks true, so TP increases from 0 to 1.
indexlabelpredictioncell
011TP
101FP
210FN
300TN
labels[1,0,1,0]predictions[1,1,0,0]masks(label==1)&(prediction==1) = truecountsTP=1 FP=0 FN=0

Recognize it
Use these masks when binary labels and predictions must be reduced into precision and recall, whose denominators answer different conditional questions.
Keep true
After each index, TP, FP, and FN equal the counts of their Boolean conjunctions over the processed prefix; TN affects neither precision nor recall numerator or denominator.
Reuse it
Define classification metrics as Boolean event counts first, then name each denominator population and guard empty populations before division.
Read it this way: label=1 and prediction=1 makes both equality masks true, so TP increases from 0 to 1. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.

Pattern: Boolean masks and safe division.

Simple idea: Count true positives, false positives, and false negatives with Boolean array operations. Return zero when a denominator is zero.

import numpy as np

def binary_metrics(labels: np.ndarray, predictions: np.ndarray) -> dict[str, float]:
   true_positive = int(np.sum((labels == 1) & (predictions == 1)))
   false_positive = int(np.sum((labels == 0) & (predictions == 1)))
   false_negative = int(np.sum((labels == 1) & (predictions == 0)))

   precision_total = true_positive + false_positive
   recall_total = true_positive + false_negative
   precision = true_positive / precision_total if precision_total else 0.0
   recall = true_positive / recall_total if recall_total else 0.0
   return {"precision": precision, "recall": recall}

Cost: time and temporary Boolean arrays.