How to Calculate Precision, Recall and F1 Score in scikit-learn
Accuracy says how often a classifier is right overall. Precision and recall say how it treats the class you care about: precision is how many of its "yes" answers were right, recall is how many of the real "yes" cases it found, and F1 balances the two. This guide computes all three for a medical test on ten patients, with real output from scikit-learn 1.9.1.
Syntax
from sklearn.metrics import f1_score, precision_score, recall_score
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
| Part | What it means |
|---|---|
y_true | The true classes, with 1 for the class you care about |
y_pred | The classes the model predicted, in the same order |
precision | The share of predicted 1s that are really 1 |
recall | The share of real 1s the model predicted as 1 |
f1 | One number that is high only when both are high |
scikit-learn's references define them. precision_score: "The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives." recall_score: "The recall is the ratio tp / (tp + fn)", "intuitively the ability of the classifier to find all the positive samples." f1_score: "The F1 score can be interpreted as a harmonic mean of the precision and recall".
Step 1: Score a Test
Ten patients, four of them ill (1), and what a test said about each one:

from sklearn.metrics import f1_score, precision_score, recall_score
# Ten patients, four of them ill (1), and what a test said about each one.
actual = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
predicted = [1, 1, 1, 0, 1, 1, 0, 0, 0, 0]
print("precision:", precision_score(actual, predicted))
print("recall: ", recall_score(actual, predicted))
print("f1: ", f1_score(actual, predicted))
uv run python precision_recall_f1.py
precision: 0.6
recall: 0.75
f1: 0.6666666666666666

Counting by hand shows where each number comes from:
- The test said "ill" five times, and three of those patients were ill: precision is 3 / 5 = 0.6.
- Four patients were ill, and the test found three: recall is 3 / 4 = 0.75.
- F1 is the harmonic mean of the two: 2 × 0.6 × 0.75 / (0.6 + 0.75) = 0.667.
Which matters more depends on the cost of each mistake: a missed patient lowers recall, a healthy patient sent for more tests lowers precision.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Swapping the arguments: precision_score(predicted, actual) | Precision and recall trade places | Always pass y_true first |
Labelling the class you care about 0 | The scores describe the other class | Label it 1, or set pos_label |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
UndefinedMetricWarning: Precision is ill-defined | The model never predicted 1, so precision divides by zero | Look at the predictions; set zero_division to choose the value |
ValueError: Target is multiclass but average='binary' | More than two classes | Pass average, such as "macro" |
FAQ
Why a harmonic mean instead of an ordinary average?
It drags the score towards the lower of the two: a model with precision 1.0 and recall 0.1 gets an F1 of about 0.18, not 0.55.
Which one should I watch?
Recall when missing a real case is costly, precision when a false alarm is, and F1 when both matter.
Conclusion
On ten patients, the test's precision was 0.6, three right out of five "ill" answers, its recall 0.75, three found out of four ill patients, and its F1 0.667, the harmonic mean of the two.
Additional Resources
- scikit-learn: precision, recall and F-measures — the user guide's section.
