By the end
- Build a stratified train/validation/test split.
- Read a confusion matrix by actual and predicted class.
- Calculate precision and recall from counts.
- Choose a threshold from error consequences rather than habit.
Week 6 · Hands-on foundations
Which evidence shows whether a classifier is useful for its intended decision?
The later classification notebook combines deeper networks, dropout, learning-rate schedules, early stopping, hyperparameter search, exercises, and project groups. The new core omits private group information and optional Optuna machinery. It concentrates on a reproducible pipeline and the error trade-offs students must interpret.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Add a cost table and choose the threshold minimizing total validation cost.
Core ideas
A confusion matrix keeps correct and incorrect outcomes separated by class. It makes the direction of a mistake visible, which a single accuracy value cannot.
Precision asks how often positive predictions are correct. Recall asks how many actual positives were found. They answer different operational questions.
A binary classifier can convert a score or probability to an action using a threshold. Changing the threshold changes the mix of false positives and false negatives without retraining.
Mathematical intuition
Precision conditions on predicted positives; recall conditions on actual positives. The denominators encode the question each metric answers.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
actual = [1, 1, 1, 0, 0, 0]
pred = [1, 0, 1, 1, 0, 0]
tp = sum(a==1 and p==1 for a,p in zip(actual,pred))
fp = sum(a==0 and p==1 for a,p in zip(actual,pred))
fn = sum(a==1 and p==0 for a,p in zip(actual,pred))
print(tp, fp, fn)Predict the result, then click Run.
tp, fp, fn = 2, 1, 1
print(round(tp/(tp+fp), 3), round(tp/(tp+fn), 3))Predict the result, then click Run.
scores = [0.2, 0.49, 0.51, 0.8]
threshold = 0.5
print([int(s >= threshold) for s in scores])Predict the result, then click Run.
Failure checks
What goes wrong: Precision is reported without stating which outcome is positive.
Check: Name the decision and class explicitly.
What goes wrong: One subset contains very few examples of a class.
Check: Inspect counts after splitting.
What goes wrong: The final estimate becomes optimistic.
Check: Select the threshold on validation data only.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. It concentrates on a reproducible pipeline and the error trade-offs students must interpret.
Standard answer: A confusion matrix keeps correct and incorrect outcomes separated by class. It makes the direction of a mistake visible, which a single accuracy value cannot.
Standard answer: Precision asks how often positive predictions are correct. Recall asks how many actual positives were found. They answer different operational questions.
Standard answer: A binary classifier can convert a score or probability to an action using a threshold. Changing the threshold changes the mix of false positives and false negatives without retraining.
Standard answer: Precision conditions on predicted positives; recall conditions on actual positives. The denominators encode the question each metric answers. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: State which class is treated as positive and why.
Standard answer: Name the decision and class explicitly.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 7 replaces the linear classifier with hidden layers while keeping exactly the same data split and evaluation contract.
Terminology
Go further
Next: Week 7 replaces the linear classifier with hidden layers while keeping exactly the same data split and evaluation contract.