Precision
- Machine Learning course
- Beginner
- Lesson 99/112
- 5 min
Precision shows how many positive predictions are correct.
Averaging types
For multi-class precision (and recall, F1) the class scores must be combined:
- macro: the plain mean of class scores; small classes have an equal say.
- weighted: the mean weighted by class size.
- micro: all TPs and FPs are summed and a single ratio is computed.
for a in ["macro", "weighted", "micro"]:
print(a, round(precision_score(y_true, y_pred, average=a), 3))If the small class's success matters, macro is reported.
Per-class precision
average=None gives each class's precision separately. In the confusion matrix this is the diagonal divided by the column sums (row sums for recall):
print(precision_score(y_true, y_pred, average=None))
cm = confusion_matrix(y_true, y_pred)
print(cm.diagonal() / cm.sum(axis=0)) # the samePrecision asks "how many of those I called this class are right?", recall "how many of this class did I find?".
Practice Three averages
Compute macro, weighted and micro precision for the given labels. On each line print the name and the value with round(..., 3).
Starter code
from sklearn.metrics import precision_score
y_true = [0, 0, 0, 0, 0, 0, 1, 1, 2, 2]
y_pred = [0, 0, 0, 0, 0, 1, 1, 0, 2, 2]
# buraya yazSample solution
from sklearn.metrics import precision_score
y_true = [0, 0, 0, 0, 0, 0, 1, 1, 2, 2]
y_pred = [0, 0, 0, 0, 0, 1, 1, 0, 2, 2]
for a in ["macro", "weighted", "micro"]:
print(a, round(precision_score(y_true, y_pred, average=a), 3))Expected output
macro 0.778 weighted 0.8 micro 0.8
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In a single-label multi-class problem, what does micro precision equal?
- Half the recall
- Accuracy Correct answer
- Macro precision
- Zero
Explanation: Every wrong prediction is an FP for some class; total TP / total predictions = accuracy.
2 Get precision weighted by class size.
precision_score(y_true, y_pred, average=____)
precision_score(y_true, y_pred, average="weighted")
Explanation: "weighted" weights each class by its support count.
3 Match the averaging types.
| Term | Meaning |
|---|---|
macro |
Equal weight to classes |
weighted |
Weight by size |
micro |
Total TP / total predictions |
None |
Per-class array |