Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

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

Precision and recall

Connect different errors to different questions.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeReason about the Python
  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.
Schematic · Connect different errors to different questions.Scroll the diagram horizontally if needed.

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=None
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.
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.

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

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.

Your task · Follow

Calculate class B precision and recall from the supplied actual and predicted labels. Store them in precision_b and recall_b.

precision_brecall_b

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.