Compute Precision and Recall from Confusion Counts
Math & Bit ManipulationAuto-graded · 20 test casesLast updated
Compute precision and recall from the four confusion-matrix counts of a binary classifier, returning 0.0 for a metric whose denominator is zero.
Compute Precision and Recall from Confusion Counts
A binary classifier was evaluated on a labeled dataset, and its results were summarized as four counts:
tp: examples that are actually positive and were predicted positive.fp: examples that are actually negative but were predicted positive.tn: examples that are actually negative and were predicted negative.fn: examples that are actually positive but were predicted negative.
Write a function that returns the classifier's precision and recall.
Definitions
- Precision is the fraction of predicted positives that are truly positive:
tp / (tp + fp). - Recall is the fraction of actual positives that the classifier found:
tp / (tp + fn).
The count tn is part of the input, but neither metric depends on it.
Zero denominators
If a metric's denominator is zero, the metric is undefined. In that case, report that metric as 0.0.
- If
tp + fp == 0(no positive predictions), precision is0.0. - If
tp + fn == 0(no actual positives), recall is0.0.
The two metrics are handled independently of each other.
Output
Return a list of two floats in the order [precision, recall]. Use ordinary true division, so integer inputs still give float results.
Constraints
tp,fp,tn,fnare non-negative integers.- All four values may be zero.
- Do not raise an exception for zero denominators.
- Return exactly two values:
[precision, recall].
Examples
Example 1
Input: compute_precision_recall(40, 10, 45, 5)
Output: [0.8, 0.8888888888888888]
Example 2
Input: compute_precision_recall(0, 8, 12, 0)
Output: [0, 0]
Hints
Hint 1
Write down which cells of the confusion matrix each metric uses. Precision looks at everything predicted positive; recall looks at everything actually positive.
Hint 2
TN never appears in either formula, so you can ignore it.
Hint 3
Check each denominator before dividing, and return 0.0 for that metric alone when it is zero.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Apply the definitions directly.
- The number of predicted positives is
tp + fp. Precision istpdivided by this number. - The number of actual positives is
tp + fn. Recall istpdivided by this number. - If either denominator is zero, the metric is undefined, so we return
0.0for it instead of dividing.
tn is accepted only to match the input format. It is not used.
Why it is correct
Precision measures how many of the flagged items were truly positive, so its denominator is every predicted positive: true positives plus false positives. Recall measures how many of the real positives were found, so its denominator is every actual positive: true positives plus false negatives. Guarding each denominator separately gives the required 0.0 in the undefined cases and keeps the two metrics independent. For example, tp=0, fp=8, tn=12, fn=0 has no actual positives and a positive prediction count of 8, so precision is 0/8 = 0.0 and recall is undefined, giving 0.0.
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
from typing import List
def compute_precision_recall(tp: int, fp: int, tn: int, fn: int) -> List[float]:
predicted_pos = tp + fp
actual_pos = tp + fn
precision = tp / predicted_pos if predicted_pos else 0.0
recall = tp / actual_pos if actual_pos else 0.0
return [precision, recall]