Understand the idea
Rows represent actual classes and columns predicted classes when using sklearn confusion_matrix. Explicit label order prevents accidental relabelling.
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.
| actual | predicted |
|---|---|
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | B |
| B | A |
| B | A |
| B | A |
| B | B |
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.
| Column | Stored type |
|---|---|
| actual | str |
| predicted | str |
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.