Sub topic 8
F1 score: combining precision and recall
This chapter assumes you can already compute precision and recall from confusion matrix counts (TP, FP, FN) and write basic Python. It shows how to merge the two into one number, and where that number can mislead you.
Why one number is useful
Precision and recall pull against each other. When you compare two models or try many settings, a pair of numbers is awkward to rank. Model A may have higher precision and model B higher recall, and neither is clearly better.
The F1 score summarizes both in a single value between 0 and 1. It is the harmonic mean of precision and recall:
F1 = 2 * precision * recall / (precision + recall)Why a harmonic mean
A plain average hides a weak side. Suppose a model flags almost everything as positive. Its recall is 1.0, but its precision is tiny. The arithmetic mean still looks decent. The harmonic mean is pulled toward the smaller value, so a model must do reasonably well on both to score well.
def f1_from(precision, recall):
if precision + recall == 0:
return 0.0
return 2 * precision * recall / (precision + recall)
precision, recall = 1.0, 0.1
print((precision + recall) / 2) # 0.55
print(round(f1_from(precision, recall), 2)) # 0.18With precision 1.0 and recall 0.1, the average is 0.55. F1 is 0.18, which reflects that the model misses 90% of the real positives.
Computing F1 from counts
Use the counts from the earlier example: TP=3, FP=1, FN=1. Precision and recall are both 0.75, so F1 is 0.75. When both values are equal, F1 equals that value.
F1 can also be written directly in terms of counts:
F1 = 2 * TP / (2 * TP + FP + FN)tp, fp, fn = 3, 1, 1
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_a = 2 * precision * recall / (precision + recall)
f1_b = 2 * tp / (2 * tp + fp + fn)
print(f1_a, f1_b) # 0.75 0.75The count form is useful because it makes one fact visible: F1 never uses TN. True negatives do not appear anywhere in the formula.
F1 across thresholds
Recall from the threshold chapter that raising the threshold raises precision and lowers recall. F1 shows where the balance lands. This sweep reuses the same scores:
y_true = [1, 1, 1, 0, 0, 0, 0, 0]
scores = [0.9, 0.6, 0.4, 0.7, 0.3, 0.2, 0.1, 0.05]
for threshold in (0.3, 0.5, 0.8):
y_pred = [int(s >= threshold) for s in scores]
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, y_pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
f1 = 2 * tp / (2 * tp + fp + fn)
print(threshold, round(f1, 2))
# 0.3 0.75
# 0.5 0.67
# 0.8 0.5Here the lowest threshold gives the best F1. At 0.8, precision is perfect, but recall falls to 0.33 and F1 drops to 0.5. If you must pick one threshold and you value both sides equally, the one with the highest F1 is a reasonable choice.
Common errors
- Treating F1 as always the right metric. F1 weights precision and recall equally. If a missed positive costs far more than a false alarm, or the reverse, F1 hides that. Choose the metric from the error costs first.
- Expecting F1 to reflect negatives. Because TN is absent, F1 says nothing about how well the model handles the negative class.
- Dividing by zero. If the model predicts no positives, or there are no true positives, precision and recall can be 0 or undefined. The guard in
f1_fromreturns 0.0 when both are 0. In scikit-learn,f1_scorewarns in such cases and returns 0.0 by default. - Comparing F1 across different datasets. F1 depends on how common the positive class is. A score of 0.75 on one dataset is not directly comparable to 0.75 on another.
Exercise
A model has precision 0.5 and recall 1.0. Compute its F1 score and say how it compares with the arithmetic mean of the two values.
Show answer
F1 = 2 0.5 1.0 / (0.5 + 1.0) = 1.0 / 1.5 ≈ 0.67. The arithmetic mean is 0.75. F1 is lower because the harmonic mean is pulled toward the weaker value, here precision.
Exercise
A classifier produces TP=8, FP=2, FN=4. Using the count form of the formula, what is F1?
Show answer
F1 = 2 8 / (2 8 + 2 + 4) = 16 / 22 ≈ 0.73.