Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessonsQUESTIONS · MODELS · EVIDENCE
Evidence · ML-C03 · 12–18 MIN

Macro F1 and imbalance

Evaluate every class explicitly.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python

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.

Evaluate every class explicitly.Class A F1: .78Class B F1: .33Macro F1: (.78 + .33) / 2Each class contributes equally, regardless of frequency.
Schematic · Evaluate every class explicitly.Scroll the diagram horizontally if needed.

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.

ERROR12 · all rows
actualpredicted
AA
AA
AA
AA
AA
AA
AA
AB
BA
BA
BA
BB

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.

Input schema
ColumnStored type
actualstr
predictedstr

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.

Your task · Follow

Compute macro F1 for ERROR12.

answer

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.