Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

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

Read a confusion matrix

Track which labels are confused.

Exercises within this concept

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

Understand the idea

Rows represent actual classes and columns predicted classes when using sklearn confusion_matrix. Explicit label order prevents accidental relabelling.

Track which labels are confused.Predicted classABActual AActual B7131B precision: 1 / 2B recall: 1 / 4Inspect every class; accuracy can hide missed cases.
Schematic · Track which labels are confused.Scroll the diagram horizontally if needed.

Python skill: Counts actual/predicted class pairs; rows are actual classes and columns are predicted classes.

Meet the syntax

confusion_matrix(actual, predicted, labels=['A','B'])
confusion_matrix
Counts actual/predicted class pairs; rows are actual classes and columns are predicted classes.
labels=['A','B']
Fixes both axis orders, including a class with zero predictions.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

from sklearn.metrics import confusion_matrix
answer=confusion_matrix(df.actual,df.predicted,labels=['A','B'])

This practice: Read and run the Python. Next: Change · Read a confusion matrix.

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

Build the A/B confusion matrix from ERROR12.

Hint 1 — Think

Matrix rows and columns have different meanings and need an explicit class order.

Hint 2 — Tools

confusion_matrix with labels.

Hint 3 — Approach

Pass actual labels first, predictions second and supply the requested A/B order.

Explained solution
from sklearn.metrics import confusion_matrix
answer=confusion_matrix(df.actual,df.predicted,labels=['A','B'])

An explicit order keeps true-class and predicted-class counts interpretable even when frequencies differ.

Helpful prior knowledge: Supervised Workflow checkpoint · Readiness · unfamiliar classification 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

Build the A/B confusion matrix from 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.