Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Evidence · ML-C03 · 12–18 MIN

Macro F1 and imbalance

Evaluate every class explicitly.

  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.
Evaluate every class explicitly.Scroll the diagram horizontally if needed.

What the syntax does

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.
  • average: "binary", "micro", "macro", "weighted", None

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
ERROR12 · all rows
actualpredicted
AA
AA
AA
AA
AA
AA
AA
AB
BA
BA
BA
BB
Your task
  1. Compute macro F1 for ERROR12.
answer
Hints

Think: Macro averaging gives each class equal weight after its F1 is calculated.

Tools: f1_score with average='macro'.

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.

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.