Concept beginner · 3 min read

What is precision and recall in machine learning

Quick answer
Precision measures the accuracy of positive predictions by calculating the ratio of true positives to all predicted positives. Recall measures the ability to find all relevant positive cases by calculating the ratio of true positives to all actual positives. Both are key classification metrics in machine learning to evaluate model performance.
Precision and Recall are classification metrics that measure the accuracy of positive predictions and the completeness of finding positive cases, respectively.

How it works

Precision is the fraction of relevant instances among the retrieved instances, answering "Of all predicted positives, how many are truly positive?" Recall is the fraction of relevant instances that were retrieved, answering "Of all actual positives, how many did the model find?" Imagine a spam filter: precision tells you how many flagged emails are actually spam, while recall tells you how many spam emails were caught.

Concrete example with PyTorch

Here is how to compute precision and recall using PyTorch tensors for binary classification predictions and labels.

python
import torch

def precision_recall(preds, labels):
    # Convert predictions to binary (0 or 1)
    preds = (preds > 0.5).int()
    labels = labels.int()

    true_positives = (preds * labels).sum().item()
    predicted_positives = preds.sum().item()
    actual_positives = labels.sum().item()

    precision = true_positives / predicted_positives if predicted_positives > 0 else 0.0
    recall = true_positives / actual_positives if actual_positives > 0 else 0.0

    return precision, recall

# Example tensors
predictions = torch.tensor([0.9, 0.4, 0.6, 0.3, 0.8])
labels = torch.tensor([1, 0, 1, 0, 0])

precision, recall = precision_recall(predictions, labels)
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
output
Precision: 1.00
Recall: 0.67

When to use it

Use precision when the cost of false positives is high, such as in email spam detection where you want to avoid marking legitimate emails as spam. Use recall when missing positive cases is costly, such as in medical diagnosis where failing to detect a disease is dangerous. Often, balancing both with metrics like F1-score is necessary depending on the application.

Key terms

TermDefinition
True Positive (TP)Correctly predicted positive cases
False Positive (FP)Incorrectly predicted positive cases
False Negative (FN)Positive cases missed by the model
PrecisionTP / (TP + FP), accuracy of positive predictions
RecallTP / (TP + FN), completeness of positive detection

Key Takeaways

  • Precision quantifies how many predicted positives are actually correct.
  • Recall quantifies how many actual positives the model successfully identified.
  • Use precision when false positives are costly; use recall when false negatives are costly.
Verified 2026-04
Verify ↗