Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

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

Precision and recall

Connect different errors to different questions.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. TransferAdapt the Python

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.

Connect different errors to different questions.Predicted classABActual AActual B7131B precision: 1 / 2B recall: 1 / 4Inspect every class; accuracy can hide missed cases.
Connect different errors to different questions.Scroll the diagram horizontally if needed.

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
  1. Requested result described in the task and deliverables.
recall_b
  1. Requested result described in the task and deliverables.
ERROR12 · all rows
actualpredicted
AA
AA
AA
AA
AA
AA
AA
AB
BA
BA
BA
BB
Your task
  1. Calculate class B precision and recall from the supplied actual and predicted labels.
  2. Store them in precision_b and recall_b.
precision_brecall_b
Hints

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.

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.