Accuracy vs Precision vs Recall in Machine Learning (Complete Beginner's Guide)
// outline
Machine learning models are only as good as the metrics used to evaluate them. When building a classification model, three evaluation metrics often cause confusion: accuracy, precision, and recall. Although they seem similar, they measure very different aspects of model performance — and choosing the wrong one can lead to dangerously misleading conclusions.
What Are Accuracy, Precision, and Recall?
These are classification evaluation metrics used to measure how well a model performs. Each answers a different question about your model's behaviour.
What proportion of all predictions were right?
Of everything flagged positive, how many truly were?
Of all real positives, how many did the model catch?
All three are calculated from a confusion matrix — the foundation of classification evaluation.
Understanding the Confusion Matrix
A confusion matrix is a 2×2 table that breaks down model predictions into four categories. Every classification metric derives from these four values.
All Formulas at a Glance
Accuracy Explained
Accuracy measures the proportion of all correct predictions out of total predictions — both positive and negative.
When Accuracy Works Well
- Classes are roughly balanced (e.g. 50/50 split)
- Cost of false positives and false negatives is similar
- You need a quick, broad overview of model performance
Precision Explained
Precision answers: "Of all the cases my model flagged as positive, how many actually were?" It penalises false alarms.
When to Use Precision
Use precision when false positives are costly. In fraud detection, wrongly flagging a legitimate transaction creates customer frustration and support overhead. High precision ensures flagged transactions are genuinely suspicious.
Recall Explained
Recall answers: "Of all the actual positives that existed, how many did my model catch?" It penalises missed cases.
When to Use Recall
Use recall when false negatives are dangerous. In cancer screening, missing a true positive case is far worse than an unnecessary follow-up test. Maximising recall ensures the model catches as many real cases as possible.
Worked Example — Spam Detection
Suppose a classifier evaluates 200 emails and produces the following confusion matrix values. Let's calculate all four metrics:
// in this balanced example all three metrics agree. The dangerous cases are imbalanced datasets — covered next.
The Accuracy Paradox
In imbalanced datasets, accuracy becomes dangerously misleading. A model can achieve very high accuracy by simply predicting the majority class every time — while completely failing at its actual job.
⚠ ACCURACY PARADOX — Imbalanced Spam Dataset
Dataset: 950 normal emails, 50 spam (95/5 split). Model predicts "not spam" for everything.
The Precision–Recall Trade-Off
There is an inherent tension between precision and recall. Lowering the classification threshold catches more positives (higher recall) but also more false alarms (lower precision). Raising the threshold reduces false alarms (higher precision) but misses more real positives (lower recall).
// as the threshold rises, precision climbs and recall falls. F1 score peaks where the two balance — the optimal zone.
What Is the F1 Score and Why It Matters
The F1 score is the harmonic mean of precision and recall. It gives a single number that balances both, making it the go-to metric for imbalanced datasets where you care about both false positives and false negatives.
- F1 = 1.0 — perfect precision and recall
- F1 = 0.0 — either precision or recall is zero
- F1 > 0.7 — generally considered good on imbalanced data
Metrics for Imbalanced Datasets
In fraud detection where only 1% of transactions are fraudulent, a model that predicts "not fraud" for everything achieves 99% accuracy but recall of 0%. The imbalance exposes accuracy as useless.
⚠ Fraud Detection — 99/1 Class Imbalance
Which Metric Should You Use?
Always choose your evaluation metric based on the business consequences of each type of error in your specific problem.
Flagging a legitimate email as spam is disruptive. Minimise false positives.
Missing a real case can be fatal. Catch every positive even at the cost of false alarms.
Missing fraud (FN) and blocking innocent customers (FP) are both costly. Balance both.
Equal class distribution and equal cost for each error type. Accuracy is reliable here.
| Metric | Measures | Penalises | Best used when |
|---|---|---|---|
| Accuracy | Overall correctness | All errors equally | Balanced classes |
| Precision | Quality of positive flags | False positives | FP is costly (spam, legal) |
| Recall | Coverage of real positives | False negatives | FN is dangerous (medical) |
| F1 Score | Harmonic balance | Both FP and FN | Imbalanced datasets |
Python Implementation
Scikit-learn makes computing all four metrics trivial. Here is a complete example on a binary classification problem:
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
# ── Example predictions ──────────────────────────────────
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 1] # actual labels
y_pred = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0] # model predictions
# ── Individual metrics ───────────────────────────────────
print(f"Accuracy : {accuracy_score(y_true, y_pred):.4f}")
print(f"Precision: {precision_score(y_true, y_pred):.4f}")
print(f"Recall : {recall_score(y_true, y_pred):.4f}")
print(f"F1 Score : {f1_score(y_true, y_pred):.4f}")
# ── Confusion matrix ─────────────────────────────────────
cm = confusion_matrix(y_true, y_pred)
print("\nConfusion Matrix:")
print(cm)
# [[TN, FP],
# [FN, TP]]
# ── Full report (easiest option) ─────────────────────────
print("\nClassification Report:")
print(classification_report(y_true, y_pred))
# ── For imbalanced datasets — adjust averaging ───────────
# zero_division=0 avoids warnings when a class has no predictions
f1_macro = f1_score(y_true, y_pred, average='macro')
f1_weighted = f1_score(y_true, y_pred, average='weighted')
// use classification_report() as your default — it prints precision, recall, F1, and support for every class in one call.
FAQs
What is the difference between accuracy, precision, and recall?
Accuracy measures overall correctness across all predictions. Precision measures what fraction of positive predictions were actually correct (minimises false alarms). Recall measures what fraction of real positives were caught by the model (minimises missed cases). They serve different business goals.
When should you use precision instead of recall?
Use precision when false positives are costly. In spam detection or fraud alerts, high precision ensures flagged items are genuinely problematic, reducing unnecessary friction for legitimate users.
When is recall more important than precision?
Recall is more important when false negatives are dangerous. In medical diagnosis or safety-critical systems, missing a true positive can have severe consequences, so the model should cast a wide net even at the cost of more false alarms.
Why is accuracy misleading for imbalanced datasets?
With a 99% negative / 1% positive split, a model that always predicts negative achieves 99% accuracy while having zero recall. The majority class dominates the metric, hiding the model's complete failure to detect the minority class.
What is a good F1 score?
It depends on the problem. Generally, F1 > 0.9 is excellent, 0.7–0.9 is good, and 0.5–0.7 is acceptable depending on domain. On highly imbalanced datasets, even F1 > 0.7 can represent strong performance since random guessing would score near zero.
Final Thoughts
Understanding accuracy vs precision vs recall is essential for building reliable machine learning models. Accuracy gives a broad overview, but precision and recall provide deeper insight into where your model fails and why.
- On balanced datasets with equal error costs — report accuracy
- When false positives are costly — optimise precision
- When false negatives are dangerous — optimise recall
- On imbalanced datasets — always use F1 score (and consider PR-AUC)
Always align your evaluation metric with the real-world consequences of each error type. The best model is not the one with the highest accuracy — it is the one that minimises the errors that matter most to your problem.
Khalid Hussain
Founder of Review Publically. Writes hands-on guides on data science, machine learning and AI tools, testing every model and library before recommending it.
// related reads