v9.1.1
Menu

Precision, Recall, and F1 Score

easyFREEModel EvaluationMachine Learning 1

Compute precision, recall, and F1 score for binary classification.

Given lists of true and predicted labels:

Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP} Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN} F1=2⋅Precision⋅RecallPrecision+Recall\text{F1} = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}

Return a tuple (precision, recall, f1), each rounded to 4 decimal places. If a denominator is 0, return 0.0 for that metric.

Example

Input
y_true = [1, 0, 1, 1, 0, 1]
y_pred = [1, 0, 0, 1, 1, 1]
Output
(0.75, 0.75, 0.75)
Reasoning
  • Inputs: y_true holds the six actual labels and y_pred the model's six guesses; position ii in one list pairs with position ii in the other, and 11 means "positive".
  • Classify each pair (true, predicted): positions 1, 4 and 6 are (1,1)(1,1), so TP=3TP = 3; position 5 is (0,1)(0,1), a false alarm, so FP=1FP = 1; position 3 is (1,0)(1,0), a missed positive, so FN=1FN = 1; position 2 is (0,0)(0,0), a true negative, which none of the three formulas uses.
  • Precision asks "of everything predicted positive, how much was right?": 33+1=0.75\frac{3}{3+1} = 0.75.
  • Recall asks "of every real positive, how much was found?": 33+1=0.75\frac{3}{3+1} = 0.75.
  • F1 is the harmonic mean: 2⋅0.75⋅0.750.75+0.75=1.1251.5=0.752 \cdot \frac{0.75 \cdot 0.75}{0.75 + 0.75} = \frac{1.125}{1.5} = 0.75, giving (0.75, 0.75, 0.75).
  • Notice all three match only because FP=FNFP = FN here. If nothing is ever predicted positive, TP+FP=0TP + FP = 0 and precision is defined as 0.00.0; the same convention applies to recall and F1.

Constraints

  • y_true and y_pred are lists of 0s and 1s
  • Return tuple (precision, recall, f1) rounded to 4 decimal places
  • Handle edge case where denominator is 0 (return 0.0)

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.