PIXELBANKv9.1.0
Menu

Precision, Recall, and F1 Score

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:
  • First, we identify the true positives (TPTP), false positives (FPFP), and false negatives (FNFN) by comparing y_true and y_pred: TP=3TP = 3, FP=1FP = 1, FN=1FN = 1
  • Then, we calculate precision and recall using the given formulas: Precision=33+1=34=0.75\text{Precision} = \frac{3}{3 + 1} = \frac{3}{4} = 0.75, Recall=33+1=34=0.75\text{Recall} = \frac{3}{3 + 1} = \frac{3}{4} = 0.75
  • Next, we calculate the F1 score using the precision and recall values: F1=2â‹…0.75â‹…0.750.75+0.75=2â‹…0.56251.5=0.75\text{F1} = 2 \cdot \frac{0.75 \cdot 0.75}{0.75 + 0.75} = 2 \cdot \frac{0.5625}{1.5} = 0.75
  • The final output is a tuple of the calculated metrics, each rounded to 4 decimal places: (0.75,0.75,0.75)(0.75, 0.75, 0.75)

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)
solution.py

Test Results

0/0
Run code to see test results.
Precision, Recall, and F1 Score - Easy | PixelBank