实验Lab: Choose the metric, then the threshold
- Dataset
- UCI Bank Marketing (45,211 rows, CC BY 4.0)
- Time
- 3 hours
- Chapters
- Ch 16: Model Evaluation
- Before you start
- slides ch 16 quiz ch 16 notebook (opens in a new tab)
Capstone Due this week: Capstone model report: validated models and the metric choice.
This week you'll be able to
- Compute precision, recall, F1, ROC-AUC, and PR-AUC from predictions, and choose one from error costs.
- Pick a decision threshold from a cost matrix instead of using 0.5.
- Check calibration and fix it with Platt or isotonic scaling.
- Research an evaluation metric beyond the chapter, build it from scratch, and show an input where it misleads.
Goal
Evaluate a classifier the way the business will use it. Choose a metric from the costs, set the threshold from a cost matrix, and check the probabilities are calibrated.
Data
The bank campaign from week 8. Drop the leaky column you found then.
import pandas as pd
bank = pd.read_csv("https://archive.ics.uci.edu/static/public/222/data.csv")
y = (bank["y"] == "yes").astype(int)
X = bank.drop(columns=["y", "duration"])The costs
Assume a call costs the bank £5 in staff time. A client who subscribes is worth £60 to the bank. A client you don't call doesn't subscribe.
Tasks
- Reuse your week 8 pipeline. Fit logistic regression and gradient boosting on a stratified 80/20 split.
- Compute every classification metric at once (Procedure 16.1). Why is accuracy misleading here?
- Plot ROC and precision-recall curves for both models (Procedure 16.2). Which curve shows the difference between them better at this class balance?
- Turn the costs into a confusion-matrix value table. What does each of TP, FP, FN, and TN earn or cost?
- Sweep thresholds from 0.01 to 0.99. Plot expected profit per 1,000 clients. Pick the best threshold (Procedure 16.3). Compare it with 0.5.
- Plot calibration curves for both models (Procedure 16.4). Fix the worse one with Platt or isotonic scaling. Does recalibration move the best threshold?
- Write a one-paragraph recommendation for the campaign manager: which model, which threshold, how many calls per 1,000 clients and the expected profit.
Deliverable
A notebook with the curves, the profit sweep, the calibration plots, and the recommendation.
Done when
- The threshold comes from the cost sweep, not from 0.5 or from F1.
- Calibration is fitted on data the model wasn't trained on.
- The recommendation states a number of calls and a profit.
Capstone
Your capstone model report is due this week. See the capstone brief.
Research challenge
The research challenge talks are in this week's session. Bring your metric card, your notebook, and your slides. The talks take the lab's slot in the session, so work through this lab in your own time.