
Accuracy, Precision and Recall Explained With Examples
Short answer: accuracy is the share of all predictions that are correct. Precision is the share of predicted positives that are truly positive, and recall is the share of actual positives that the model finds. Which one matters most depends on the cost of different mistakes.
Start with the confusion matrix
For a yes/no classifier, every prediction falls into one of four groups:
- True positive (TP): predicted yes, actually yes.
- False positive (FP): predicted yes, actually no.
- False negative (FN): predicted no, actually yes.
- True negative (TN): predicted no, actually no.
The formulas
- Accuracy = (TP + TN) / (TP + FP + FN + TN)
- Precision = TP / (TP + FP)
- Recall = TP / (TP + FN)
- F1 score = 2 x precision x recall / (precision + recall)
A worked example
A spam filter checks 200 emails. It finds TP = 40, FP = 10, FN = 20 and TN = 130.
- Accuracy = (40 + 130) / 200 = 0.85
- Precision = 40 / (40 + 10) = 0.80, so 80% of emails flagged as spam really were spam.
- Recall = 40 / (40 + 20) = 0.67, so the filter caught two thirds of the real spam.
- F1 = 2 x 0.80 x 0.67 / (0.80 + 0.67) = about 0.73
Which metric should you use?
- Precision when false alarms are costly, such as blocking a real customer email.
- Recall when missing a case is costly, such as screening for a serious disease or fraud.
- F1 when you need a balance between the two.
- Accuracy when classes are balanced and mistakes cost about the same.
The accuracy trap with imbalanced data
If only 1% of transactions are fraud, a model that always says "not fraud" is 99% accurate and completely useless. In imbalanced problems, look at precision, recall, the confusion matrix and curves such as precision-recall, not accuracy alone.
Compute them in Python
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
y_true = [1, 0, 1, 1, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1]
print(accuracy_score(y_true, y_pred))
print(precision_score(y_true, y_pred))
print(recall_score(y_true, y_pred))
print(f1_score(y_true, y_pred))
Thresholds and the precision-recall trade-off
Many classifiers output a probability, and you choose the cut-off for "yes". Lowering the threshold flags more cases, which usually raises recall but lowers precision. Raising it does the opposite. Pick the threshold based on the cost of each kind of mistake, and compare models with a precision-recall curve instead of a single number.
Key takeaways
- Accuracy can mislead when one class is rare.
- Precision answers: of what I flagged, how much was right?
- Recall answers: of everything that was there, how much did I find?
- F1 balances the two, and the right choice always depends on the cost of each mistake.
The F1 score and other combined metrics
When you need one number that balances precision and recall, use the F1 score, the harmonic mean of the two:
F1 = 2 × (precision × recall) / (precision + recall)
The harmonic mean punishes imbalance: a model with precision 1.0 and recall 0.1 gets an F1 of about 0.18, not 0.55. If recall matters more than precision, the more general F-beta score lets you weight it higher. For multi-class problems, scikit-learn offers macro averaging (each class counts equally) and weighted averaging (classes weighted by how often they appear).
Threshold-independent metrics
| Metric | What it shows | Best for |
|---|---|---|
| ROC AUC | How well the model ranks positives above negatives across all thresholds | Fairly balanced classes |
| PR AUC (average precision) | Precision and recall across thresholds | Rare positive classes, such as fraud |
| Log loss | How good the predicted probabilities are | When you use probabilities directly |
from sklearn.metrics import roc_auc_score, average_precision_score, classification_report
probs = model.predict_proba(X_test)[:, 1]
print("ROC AUC:", roc_auc_score(y_test, probs))
print("PR AUC:", average_precision_score(y_test, probs))
print(classification_report(y_test, model.predict(X_test)))
Common mistakes
- Reporting accuracy alone on imbalanced data. Always show the confusion matrix or precision and recall too.
- Tuning the threshold on the test set. Choose it on a validation set, then report final results on untouched test data.
- Mixing up which class is "positive". Check the
pos_labelsetting so precision and recall describe the class you care about. - Ignoring business costs. A missed fraud case and a false alarm rarely cost the same; choose metrics that reflect real costs.
Put this into context with the data science workflow, learn the tools in essential Python libraries, and see where metrics fit in machine learning vs deep learning.
Frequently asked questions
Can precision and recall both be high?
Yes, for a very good model. Usually there is a trade-off: changing the decision threshold to catch more positives raises recall but lowers precision.
What is the difference between recall and sensitivity?
They are the same measure. Medicine often says sensitivity, and machine learning says recall.
Do these metrics work for regression?
No. They are for classification. Regression uses measures such as mean absolute error, mean squared error and R squared.
What is a good F1 score?
There is no universal cut-off. Compare against a simple baseline and the needs of the task: 0.7 may be excellent for a hard medical problem and poor for an easy one.
Should I use precision or recall for spam filters?
Usually precision, because marking a real email as spam (a false positive) annoys users more than letting the occasional spam through. For disease screening it is the opposite: high recall matters so that real cases are not missed, and follow-up tests handle false alarms.
Why is the harmonic mean used for F1?
It stays low unless both precision and recall are reasonably high, so a model cannot hide a terrible score in one metric behind a great score in the other.
