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.
Python skill: Measures how often the predicted positive label is correct.
Meet the syntax
precision_score(y, predicted, pos_label='B')
recall_score(actual, predicted, pos_label="B")
average=Noneprecision_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.
average=None- Returns one score per class rather than combining classes into a single summary.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
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')
This practice: Read and run the Python. Next: Change · Precision and recall.
Given data · ERROR12
12 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
| 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 |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| actual | str |
| predicted | str |
Your task · Follow
Calculate class B precision and recall from the supplied actual and predicted labels. Store them in precision_b and recall_b.
Hint 1 — Think
Precision starts from predicted positives; recall starts from actual positives.
Hint 2 — Tools
precision_score, recall_score and pos_label.
Hint 3 — 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.
Helpful prior knowledge: Read a confusion matrix These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.