Skip to main content

How to Calculate Precision, Recall and F1 Score in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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)
PartWhat it means
y_trueThe true classes, with 1 for the class you care about
y_predThe classes the model predicted, in the same order
precisionThe share of predicted 1s that are really 1
recallThe share of real 1s the model predicted as 1
f1One 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:

The precision_recall_f1.py script in a macOS-style code window: ten true labels with four ill patients, the test's ten answers, and the precision, recall and F1 score printed.

precision_recall_f1.py
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))
Terminal
uv run python precision_recall_f1.py
Output
precision: 0.6
recall: 0.75
f1: 0.6666666666666666

The output of precision_recall_f1.py in a terminal: precision 0.6, recall 0.75 and 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​

MistakeWhat happensDo this instead
Swapping the arguments: precision_score(predicted, actual)Precision and recall trade placesAlways pass y_true first
Labelling the class you care about 0The scores describe the other classLabel it 1, or set pos_label

Troubleshooting​

SymptomLikely causeFix
UndefinedMetricWarning: Precision is ill-definedThe model never predicted 1, so precision divides by zeroLook at the predictions; set zero_division to choose the value
ValueError: Target is multiclass but average='binary'More than two classesPass 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​