0Pricing
Machine Learning Academy · Lesson

Precision, Recall, and F1-Score in Practice

Learners will calculate precision and recall, understand the precision-recall trade-off, and choose the right metric for imbalanced real-world tasks.

The Precision-Recall Framework

When classes are imbalanced or when different error types have different costs, we need metrics that focus specifically on how the model handles the positive class. Precision and Recall are the two complementary metrics that fill this role.

Together, precision and recall give a complete picture of classifier behaviour on the positive class without being distorted by a large number of true negatives. This matters enormously in fraud detection, disease screening, and content moderation — all domains where the positive class is rare but critical.

Precision: Quality of Positive Predictions

Precision answers: Of all the examples the model predicted as positive, what fraction were actually positive?

Precision = TP / (TP + FP)

High precision means few false alarms. A spam filter with precision 0.99 means 99% of emails sent to the spam folder are actually spam — very few legitimate emails are misclassified. However, precision says nothing about how many real spam emails were missed. A model that flags only one email as spam and that email is spam has precision 1.0, even if it missed thousands of spam emails.

from sklearn.metrics import precision_score
from sklearn.metrics import confusion_matrix
import numpy as np

y_true = np.array([1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0])
y_pred = np.array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0])

cm = confusion_matrix(y_true, y_pred)
TN, FP, FN, TP = cm.ravel()

precision_manual = TP / (TP + FP)
precision_sklearn = precision_score(y_true, y_pred)

print(f'TP={TP}, FP={FP}, FN={FN}, TN={TN}')
print(f'Precision (manual): {precision_manual:.3f}')
print(f'Precision (sklearn): {precision_sklearn:.3f}')

All lessons in this course

  1. From Regression to Classification: Threshold Decisions
  2. Logistic Regression and the Sigmoid Function
  3. The Confusion Matrix Explained
  4. Precision, Recall, and F1-Score in Practice
← Back to Machine Learning Academy