Skip to content
E
Educatum

Precision, recall and F1 score

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Precision, recall, and F1 score evaluate classification performance beyond raw accuracy, particularly on imbalanced datasets. Precision measures the fraction of predicted positives that are truly positive, recall measures the fraction of actual positives successfully detected, and the F1 score takes their harmonic mean to penalize extreme imbalance between the two.

Study Card: Precision, recall and F1 score

Direct Answer

Precision, recall, and F1 score are threshold-dependent classification metrics built from the confusion matrix counts—True Positives (TP\text{TP}), False Positives (FP\text{FP}), and False Negatives (FN\text{FN}):

Precision=TPTP+FP,Recall=TPTP+FN,F1=2⋅Precision⋅RecallPrecision+Recall=2TP2TP+FP+FN\text{Precision} = \frac{\text{TP}}{\text{TP} + \text{FP}}, \qquad \text{Recall} = \frac{\text{TP}}{\text{TP} + \text{FN}}, \qquad F_1 = \frac{2 \cdot \text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} = \frac{2\text{TP}}{2\text{TP} + \text{FP} + \text{FN}}
  • Precision measures positive prediction reliability (how few false alarms occur).
  • Recall (Sensitivity) measures positive coverage (how few true positives are missed).
  • F1 Score is the harmonic mean of precision and recall, heavily penalizing models where either precision or recall is low.

Unlike raw accuracy—which looks deceptively high when one class dominates—precision, recall, and F1 directly capture minority-class errors.

Key Terms

  • True Positives (TP): Actual positives correctly predicted as positive.
  • False Positives (FP): Actual negatives incorrectly predicted as positive (Type I error).
  • False Negatives (FN): Actual positives incorrectly predicted as negative (Type II error).
  • Precision: TPTP+FP\frac{\text{TP}}{\text{TP} + \text{FP}} — proportion of positive predictions that are correct.
  • Recall (Sensitivity): TPTP+FN\frac{\text{TP}}{\text{TP} + \text{FN}} — proportion of actual positives identified.

Example

Imagine screening medical scans for cancer:

  • High Precision, Low Recall: Few false alarms, but many sick patients are missed (high FN)—dangerous when missed cases are life-threatening.
  • Low Precision, High Recall: Catches nearly all cancer cases (low FN), at the cost of flagging more healthy patients for follow-up testing (high FP).
  • High Precision, High Recall: Accurately detects cases while minimizing false alarms.
  • F1 Score: Summarizes both metrics into one number, useful when FP and FN carry comparable importance (and generalized by Fβ=(1+β2)P⋅Rβ2P+RF_\beta = (1+\beta^2)\frac{P \cdot R}{\beta^2 P + R} when one matters more).

Code Implementation

from sklearn.metrics import precision_score, recall_score, f1_score, classification_report, confusion_matrix
import numpy as np

# Example true and predicted labels
y_true = np.array([0, 1, 1, 0, 1, 0, 0, 1, 0, 1])
y_pred = np.array([1, 1, 0, 0, 1, 0, 1, 1, 0, 0])

# Calculate precision, recall, and F1-score
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)

print(f"Precision: {precision:.4f}")
print(f"Recall:    {recall:.4f}")
print(f"F1 Score:  {f1:.4f}")

print("\nClassification Report:\n", classification_report(y_true, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_true, y_pred))
  • Confusion Matrix: Summarizes TP,FP,TN,FN\text{TP}, \text{FP}, \text{TN}, \text{FN} counts for binary or multi-class predictions at a fixed decision threshold.
  • Type I and Type II Errors: False positives (Type I) degrade precision; false negatives (Type II) degrade recall.
  • ROC-AUC and PR-AUC: ROC-AUC evaluates TPR vs. FPR across all thresholds; Precision-Recall curves (PR-AUC) focus directly on precision and recall without relying on TN, making them especially informative for rare positive classes.
  • Multi-class Averaging (Macro, Micro, Weighted): Macro F1 averages per-class F1 equally (sensitive to minority classes), while micro F1 aggregates global TP/FP/FN counts (equal to accuracy in single-label multi-class classification).
  • Diagnosing Bias vs. Variance: Model underfitting (bias) vs. overfitting (variance) is diagnosed by comparing training vs. validation scores (e.g., F1 gap between train and validation), rather than by precision-versus-recall imbalance, which is governed primarily by the chosen classification threshold and class priors.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.