Understand the idea
F1 balances precision and recall for a class. Macro F1 averages class F1 values equally, regardless of frequency. Production uses macro F1 for classifier selection and reports accuracy alongside it.
Python skill: Combines precision and recall for each class.
Meet the syntax
f1_score(y, predicted, average='macro')f1_score- Combines precision and recall for each class.
average='macro'- Averages class F1 values with equal class weight, rather than weighting by class size.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
from sklearn.metrics import f1_score
answer=f1_score(df.actual,df.predicted,average='macro')
This practice: Read and run the Python. Next: Change · Macro F1 and imbalance.
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
Compute macro F1 for ERROR12.
Hint 1 — Think
Macro averaging gives each class equal weight after its F1 is calculated.
Hint 2 — Tools
f1_score with average='macro'.
Hint 3 — Approach
Score the supplied actual and predicted labels with the requested averaging rule.
Explained solution
from sklearn.metrics import f1_score
answer=f1_score(df.actual,df.predicted,average='macro')
The macro summary balances class contributions rather than weighting them by how many rows they contain.
Helpful prior knowledge: Precision and recall 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.