Skip to Content

Accuracy, Precision, Recall, F1

When to use

Score a classifier and decide between precision-heavy (do not bother users with false alarms) and recall-heavy (do not miss anything) workloads.

Analogy

Fishing with a net. Precision is how much of what you caught IS fish; recall is how many fish in the lake you actually caught; F1 is the harmony of the two.

Data-flow diagram

   TP=10  FP=2  FN=5
   precision = 10/12 = 0.83
   recall    = 10/15 = 0.67
   F1        = 2*P*R/(P+R) = 0.74

   rare-class -> F1 preferred, accuracy lies
   spam       -> push precision higher
   medical    -> push recall higher

Deep explanation

Precision is TP divided by TP plus FP. Recall is TP divided by TP plus FN. F1 is the harmonic mean — high only when both are high. Accuracy is right divided by total — useful on balanced data, misleading on imbalanced. For multi-class pass average=macro for per-class balance or average=weighted for class-frequency balance. F-beta generalises F1 with beta controlling precision-vs-recall bias.

Examples

Example 1

from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 1, 1, 0, 1]
y_pred = [0, 1, 0, 1, 0, 1]
print('P:', precision_score(y_true, y_pred))
print('R:', recall_score(y_true, y_pred))
print('F1:', f1_score(y_true, y_pred))

Default binary metrics show only the positive class.

Example 2

from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 0, 1]
y_pred = [0, 1, 2, 0, 0, 2]
print(classification_report(y_true, y_pred, digits=3))

classification_report prints per-class precision/recall/F1 plus macro and weighted averages.

Example 3

from sklearn.metrics import fbeta_score
print('F2 (recall-heavy):', fbeta_score(y_true, y_pred, beta=2))
print('F0.5 (prec-heavy):', fbeta_score(y_true, y_pred, beta=0.5))

F-beta(2) prioritizes recall by 4x; F-beta(0.5) prioritizes precision by 4x.

Common mistake

Reporting accuracy on rare-event data. A 99 percent accurate fraud model is just one that says no fraud to every transaction. Switch to PR-AUC and F1.

Key takeaway

Use precision plus recall plus F1 together. Pick the F-beta that matches your cost: medical uses F2, spam uses F0.5.

Production Failure Playbook

Failure scenario 1: rare-class-f1-zero

Failure scenario 2: weighted-f1-mask