Understand the idea
Precision asks how often predictions of a class are correct. Recall asks how many actual members of a class were found. Different costs can favour different trade-offs.
What the syntax does
precision_score- Measures how often the predicted positive label is correct.
pos_label='B'- Declares which label is treated as the positive class in this binary calculation.
recall_score(actual, predicted, pos_label="B")- Measures the fraction of actual B observations correctly recovered.
Outputs you create
precision_b- Requested result described in the task and deliverables.
recall_b- Requested result described in the task and deliverables.
actual | predicted |
|---|---|
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | B |
| B | A |
| B | A |
| B | A |
| B | B |
- Calculate class B precision and recall from the supplied
actualandpredictedlabels. - Store them in
precision_bandrecall_b.
precision_brecall_bHints
Think: Precision starts from predicted positives; recall starts from actual positives.
Tools: precision_score, recall_score and pos_label.
Approach: Calculate both measures for the named positive class using the same aligned labels.
Explained solution
from sklearn.metrics import precision_score,recall_score
precision_b=precision_score(df.actual,df.predicted,pos_label='B')
recall_b=recall_score(df.actual,df.predicted,pos_label='B')
Explicitly naming B avoids relying on numeric-label defaults and preserves the intended error interpretation.