◢◤ ARKANE_
SYS_TIME UTC+0
Work with me
/01

项目范例Worked example capstone

A complete capstone on hospital readmissions, condensed to one page, then marked against the rubric. Use it to calibrate your own marking.

This is a full capstone, condensed. It shows what each milestone looks like and how the final work scores against the rubric.

A real submission is a repository, a report, and a talk. Here, each milestone is cut to its key decisions, and numbers. Every number comes from running the code on this page on 21 November 2026.

You can't submit this project. Choose a different dataset, or a different question on this one.

Milestone 1: proposal (week 4)

Problem statement (Procedure 2.1)

  • Decision-maker: the manager of a hospital's transitional care team.
  • Decision: which diabetic patients get a nurse home visit in the week after discharge. The team can't visit everyone.
  • Target: readmitted to hospital within 30 days of discharge. Yes or no. Known 30 days after each discharge.
  • Unit: one discharge. The prediction is made on the day of discharge, from data available that day.

Data

Diabetes 130-US Hospitals, 1999 to 2008 (Strack and others, UCI). 101,766 hospital stays of diabetic patients at 130 US hospitals. CC BY 4.0. It loads in one line:

df = pd.read_csv("https://archive.ics.uci.edu/static/public/296/data.csv")

Error costs

These are assumptions for this example. In a real project they come from the decision-maker and the finance team.

Assumed value
One nurse visit $400
One 30-day readmission $15,000
Share of readmissions a visit prevents 20%

So a visit to a patient who would have been readmitted saves 0.2 × $15,000 − $400 = $2,600 on average. A visit to a patient who wouldn't have been readmitted costs $400. A missed readmission costs nothing extra compared with today.

A visit pays for itself when the patient's chance of readmission is above $400 / $3,000 = 13.3%.

Success criteria and a baseline rule

  • Baseline rule (Procedure 2.3): visit every patient with at least one hospital stay in the past year. It needs no model.
  • Success: the model beats the rule on net value per 1,000 discharges by at least $5,000. That covers the cost of running it. Measured in cross-validation, then confirmed once on the test set.

Milestone 2: data report (week 8)

Cleaning log

Every step runs from the raw download, in this order.

Step Rows left Why
Raw download 101,766
Drop discharges to a hospice or by death 99,343 These patients can't be readmitted. The target is undefined
Keep each patient's first stay only 69,990 Repeat patients would sit in both train and test. See the leakage check
Drop gender "Unknown/Invalid" 69,987 Three rows
Drop weight 97% missing
payer_code, medical_specialty: missing → "Missing" 44% and 48% missing. Missing is a category, not an error
A1Cresult, max_glu_serum: missing → "Not measured" In this copy of the data, "test not done" is stored as missing
Age band "[70-80)" → 75 The midpoint, so age is numeric
Diagnosis codes → 9 groups 700 to 800 ICD-9 codes per column. Grouped into circulatory, respiratory, diabetes, and so on

Then the cleaned data is checked with assertions: no missing values, the target is 0 or 1, and every age is between 5 and 95.

d = raw.sort_values("encounter_id")
d = d[~d.discharge_disposition_id.isin([11, 13, 14, 19, 20, 21])]
d = d.drop_duplicates("patient_nbr", keep="first")
...
assert X.isna().sum().sum() == 0
assert y.isin([0, 1]).all() and X.age.between(5, 95).all()

20% of the rows are set aside as the test set, stratified on the target. The findings below use the training set only.

Five findings

Each finding would have a chart in the real report.

  1. The readmission rate is 9.0%. Across all stays it's 11.2%. Repeat stays are readmitted at 16.7%, so keeping every stay would inflate the rate. A model that always says "no" is 91% accurate, so accuracy is useless here.
  2. Past stays predict future ones. The readmission rate is 8.1% with no stay in the past year, 13.1% with one, 17.3% with two and 26.0% with three or more.
  3. Where patients go matters. 6.9% of patients discharged home are readmitted, against 12.6% of everyone else.
  4. The diabetes test barely matters. HbA1c wasn't measured for 82% of patients. Readmission rates are 8.1% to 9.1% whatever the result. The obvious clinical feature carries little signal.
  5. Length of stay is a weak signal. Its correlation with readmission is 0.06. The number of medications is 0.04.

Leakage check (Procedure 2.5)

  • encounter_id and patient_nbr are identifiers. Dropped.
  • Repeat patients. 77% of patients appear once, but one appears 40 times. With every stay kept, the same patient lands in train and test, and the model learns the person. Fixed by keeping the first stay per patient. Finding 1 shows this matters.
  • Every remaining feature is known on the day of discharge. number_inpatient counts stays in the year before.

Baseline

The dummy classifier scores an average precision of 0.090, the readmission rate. Any useful model must beat that.

Milestone 3: model report (week 12)

Three model families, identical CV (Procedure 11.2)

All preprocessing sits in one ColumnTransformer inside each pipeline: scaling for the numeric columns, one-hot encoding for the categories. Five-fold stratified CV on the training set. Average precision is the main metric because positives are rare.

Model ROC AUC Average precision
Dummy (prior) 0.500 0.090
Logistic regression 0.647 ± 0.006 0.166 ± 0.007
Random forest 0.656 ± 0.004 0.177 ± 0.008
Gradient boosting 0.653 ± 0.004 0.179 ± 0.007

The ± is the standard deviation across the five folds. Random forest and gradient boosting are inside each other's noise (Procedure 13.5). Either would do.

A randomized search over gradient boosting's settings (15 candidates, same folds) reached 0.181 ± 0.008. That's within the noise too. The tuned model is kept, but tuning didn't change the picture.

Threshold from the costs (Procedure 16.3)

The default threshold of 0.5 flags 6 patients in 14,000. Useless. Instead, sweep the threshold on out-of-fold predictions and compute net value at each one:

Threshold Patients flagged Net value per 1,000 discharges
0.08 46% −$6,508
0.10 28% $15,507
0.13 13% $23,383
0.16 7% $21,754
0.20 4% $16,682

The best threshold, 0.13, matches the 13.3% break-even from the proposal. That's what a well-calibrated model should give. The rule scores $6,787 per 1,000 on the same data. The model clears the success bar.

Calibration check (Procedure 16.4)

Grouped into eight bins, the predicted and observed rates match closely: 4.1% predicted against 4.8% observed in the lowest bin, 18.8% against 21.8% in the highest. The Brier score is 0.078 against the dummy's 0.082. No recalibration needed.

Final results (week 15)

The test set, used once

ROC AUC 0.657, average precision 0.197. Close to the CV scores, so the CV wasn't optimistic.

Policy (13,998 test discharges) Visits Readmissions caught Net value Per 1,000 discharges
Visit nobody 0 0 of 1,257 $0 $0
Visit everyone 13,998 1,257 −$1,828,200 −$130,604
Rule: a stay in the past year 1,630 260 $128,000 $9,144
Model, threshold 0.13 1,719 378 $446,400 $31,890

The model makes about the same number of visits as the rule and reaches 45% more patients who'd be readmitted.

Where the model fails

The model misses 879 of the 1,257 readmissions. 88% of those patients had no hospital stay in the past year.

Group Readmission rate Readmissions caught
A stay in the past year 16.0% 60%
No stay in the past year 8.1% 22%
Discharged home 7.1% 10%
Discharged elsewhere 12.2% 51%

The model finds patients who are already known to be at risk. A first-time patient sent home looks low-risk to it, and most of the readmissions it misses come from that group. This data can't find them. A cheaper step for everyone, such as a phone call, would do more for them than a better model.

How much the assumptions matter

The 20% prevention rate is the weakest number in the proposal. With the threshold reset to break-even each time:

Visits prevent Net value per 1,000 discharges
5% $0. No patient is worth a visit
10% $2,207
15% $14,109
20% $30,933
30% $79,476

The report's opening

Recommendation: use the model to choose who gets a nurse visit after discharge, in a three-month pilot. On past data it chooses 12% of diabetic patients and earns about $32,000 per 1,000 discharges. The current best rule earns $9,000.

The main risk: the value depends on how many readmissions a visit prevents. We assumed 20%. Below 10%, the visits barely pay. The pilot should measure this: visit half the flagged patients at random and compare.

Limits: the data is from US hospitals between 1999 and 2008. Retrain on your own records before going live. The model rarely flags first-time patients discharged home. They need a different approach.

Self-marking against the rubric

Marked strictly: meets only when every item in the cell is there.

Criterion Mark Evidence
1. Framing Exceeds Decision-maker, target, window, costs, and success threshold all set in week 4. The rule baseline tests whether ML is needed. Out of scope: the dataset can't say which patients a visit helps, so the pilot measures it
2. Data and exploration Exceeds Cleaning runs from the download, every step logged with a reason, assertions at the end. Five findings. A leakage check. Finding 1 changed the modelling: first stays only
3. Modelling and validation Meets Dummy baseline, three families under identical CV, pipelines throughout, test set used once, compared against fold noise. Not exceeds: no nested CV. The threshold was chosen on out-of-fold scores from the model that was tuned on the same folds, which is slightly optimistic
4. Evaluation and decision Exceeds Metric from the costs, threshold from a cost sweep, calibration checked, expected value against every alternative. Error analysis shows who the model misses and what to do about it
5. Communication and reproducibility Meets Runs from a fresh clone. The report leads with the recommendation, the main risk, and the limits. Not exceeds until a stranger has reproduced it from the README

All five are at meets or better, so this passes.

What "not yet" looked like

The first draft of the model report failed criterion 4. It reported 91% accuracy and used the 0.5 threshold. That threshold flags 6 patients in 14,000, so the model did almost nothing. The accuracy looked good only because the dummy gets 91% too. The milestone feedback said so. The cost sweep above was the fix.

Most capstones fail like this. Not on the model, but on a step that was skipped.

online /teaching/semester-1/worked-example Newcastle, UK v0.1.0 // rss