实验Lab: How much can you trust one score?
- Dataset
- UCI Statlog German Credit (1,000 rows, CC BY 4.0)
- Time
- 3 hours
- Before you start
- slides ch 13 quiz ch 13 notebook (opens in a new tab)
This week you'll be able to
- Explain why one train/test split gives a noisy, sometimes optimistic score.
- Choose k-fold, stratified, grouped or time-series CV for a dataset, and justify it.
- Run nested CV and explain why a tuned CV score is optimistic.
- Write a capstone modelling plan: the CV scheme, the main metric, and the baseline, each with a reason.
Goal
See how much one train/test split can mislead on a small dataset. Then estimate performance honestly, with the right CV and nested CV for tuning.
Data
1,000 loan applicants, 700 rated good and 300 bad. The columns are coded (A11, A34 and so on). The dataset page explains each code. In class, 1 is good and 2 is bad.
import pandas as pd
credit = pd.read_csv("https://archive.ics.uci.edu/static/public/144/data.csv")
X = credit.drop(columns="class")
y = (credit["class"] == 2).astype(int) # 1 = bad credit, the class we want to catchTasks
- Build one pipeline: one-hot the coded columns, scale the numeric ones, then logistic regression.
- Split 80/20 with 50 different random seeds. Record the test ROC-AUC each time. Plot the 50 scores. What's the range? What would you have reported if you'd only run one seed?
- Run 5-fold CV in one line (Procedure 13.1). Then stratified 5-fold (Procedure 13.2). Compare the fold-to-fold spread.
- Tune
CwithGridSearchCV. Report the best CV score. Explain why that number is optimistic. - Run nested CV (Procedure 13.4): tuning inside, evaluation outside. Compare the nested score with task 4's.
- Compare logistic regression with a random forest using the same folds (Procedure 13.5). Is the difference bigger than the fold-to-fold noise?
- Put the scaler outside the pipeline: fit it on all the data before CV. Does the score change? Explain why this is still a leak, even when the number barely moves.
Deliverable
The seed-variance plot, a table of every estimate from tasks 3 to 6, and a recommendation: which number would you report, and why?
Done when
- The plot shows all 50 seeds.
- The nested CV score is reported next to the tuned CV score.
- The model comparison uses identical folds.
Capstone: your modelling plan
The last 45 minutes of this week's session are for your capstone. Chapter 13 is short, so use the reading time you save to finish the lab.
Plan the modelling before you write any model code. Your model report is due in week 12.
Write one page that answers four questions. Give a reason for each.
- The CV scheme. Plain, stratified, grouped, or time-based k-fold? How many folds? Is there anything in your data, such as repeat customers or dates, that would let a random split leak?
- The main metric. Which one, and how does it follow from the error costs in your proposal? Name a second metric you'll report alongside it.
- The baseline. Your dummy score from the data report, and your rule baseline from the proposal. What does a model have to beat to be worth using?
- The models. Which three families will you compare, and what will you tune in each?
Show the plan to your instructor or a classmate before you leave. If you're studying alone, check it against the rubric's criterion 3 on the capstone page. The worked example shows a plan like this carried through.
The plan isn't marked. Keep it in your repository. It becomes the opening of your model report.