Precision
- Machine Learning course
- Beginner
- Lesson 67/112
- 5 min
Precision is the accuracy ratio of positive predictions.
Precision
Precision measures how right the model is when it says "positive": TP / (TP + FP). It matters where false alarms are costly.
from sklearn.metrics import precision_score
y_true = [1, 1, 0, 0, 0]
y_pred = [1, 1, 1, 0, 0]
print(precision_score(y_true, y_pred)) # 2 / 3 = 0.667Spam filter example: if precision is low, important e-mails land in the spam folder.
With no positive predictions
If the model never says positive, the denominator is zero and precision is undefined. scikit-learn returns 0 with a warning in that case; zero_division picks the behaviour:
print(precision_score([1, 0], [0, 0], zero_division=0)) # 0.0In a multi-class problem the average parameter is needed: "macro" is the class mean, "weighted" the mean weighted by class size.
Practice Compute precision
Compute precision for the given labels with precision_score and print it with round(..., 3). Then compute the same value by hand with TP / (TP + FP) (count TP and FP with a loop or zip) and print it.
Starter code
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
y_true = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
y_pred = [1, 1, 1, 0, 1, 0, 0, 0, 0, 0]
# buraya yazSample solution
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
y_true = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
y_pred = [1, 1, 1, 0, 1, 0, 0, 0, 0, 0]
print(round(precision_score(y_true, y_pred), 3))
tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
fp = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 1)
print(round(tp / (tp + fp), 3))Expected output
0.75 0.75
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If TP=30 and FP=10, what is the precision?
- 0.75 Correct answer
- 0.25
- 0.33
- 3.0
Explanation: 30 / (30 + 10) = 0.75.
2 Take the class mean of multi-class precision.
p = precision_score(y_true, y_pred, average=____)
p = precision_score(y_true, y_pred, average="macro")
Explanation: "macro" gives every class equal weight.
3 Match the concepts.
| Term | Meaning |
|---|---|
precision |
TP / (TP + FP) |
low precision |
Many false alarms |
zero_division |
Zero-denominator behaviour |
average="macro" |
Class mean |