Chapter 7 — Assignment Project: Auditing a Clinic’s Data
Overview¶
A clinic wants a model that predicts, on the day a patient is discharged, whether they will be readmitted within 30 days. Before anyone trains a model, you are asked to audit the data: find what is wrong with it, fix what can be fixed, prepare it without leakage, and write down what a model trained on it can and cannot be trusted to do.
The dataset below is generated by code (so that everyone has the same data), and it has been built with nine problems of the kinds described in Chapter 7. Your job is to find them, with evidence.
| Part | Task | Points |
|---|---|---|
| A | The audit: find the problems, with evidence | 30 |
| B | Cleaning and a leak-free preparation pipeline | 30 |
| C | The leakage hunt: measure what the leaks do to a score | 20 |
| D | A data card | 20 |
import numpy as np
import pandas as pd
def make_clinic(n=1500, seed=11):
"""Discharge records of a clinic. Do not read this function to find the problems: find them in the data."""
rng = np.random.default_rng(seed)
patients = rng.integers(1, 701, n)
base = rng.normal(0, 1, 701) # each patient's own risk
age = np.clip(rng.normal(62, 14, 701), 18, 98).round()[patients]
sex = rng.choice(['Female', 'Male'], 701)[patients]
weight = (np.clip(rng.normal(78, 15, 701), 40, 160)[patients] + rng.normal(0, 1.5, n)).round(1)
stay = rng.poisson(4, n) + 1
previous = rng.poisson(0.8, n)
meds = rng.poisson(6, n)
income = rng.lognormal(10.3, 0.5, n).round(-2)
ward = rng.choice(['cardiology', 'surgery', 'internal', 'geriatrics'], n, p=[0.3, 0.25, 0.3, 0.15])
logit = -2.8 + 1.6 * base[patients] + 0.025 * (age - 62) + 0.12 * stay + 0.35 * previous + 0.08 * meds - 0.4 * (income > 30000)
readmitted = rng.random(n) < 1 / (1 + np.exp(-logit))
df = pd.DataFrame({'record': np.arange(100001, 100001 + n), 'patient_id': patients, 'age': age, 'sex': sex,
'weight': weight, 'ward': ward, 'length_of_stay': stay, 'previous_admissions': previous,
'medications': meds, 'income': income, 'clinic': 'North Clinic',
'readmitted': readmitted.astype(int)})
df['followup_calls'] = np.where(readmitted, rng.poisson(3, n) + 1, rng.poisson(0.3, n))
lb = df['ward'] == 'surgery'; df.loc[lb, 'weight'] = (df.loc[lb, 'weight'] * 2.2046).round(1)
sex_typo = rng.random(n) < 0.05; df.loc[sex_typo, 'sex'] = df.loc[sex_typo, 'sex'].map({'Female': 'F', 'Male': 'male'})
poor = df['income'] < df['income'].quantile(0.35); df.loc[poor & (rng.random(n) < 0.6), 'income'] = np.nan
df.loc[rng.choice(n, 4, replace=False), 'age'] = 999
return pd.concat([df, df.sample(25, random_state=seed)], ignore_index=True)
clinic = make_clinic()
print(clinic.shape)
clinic.head()Part A — The Audit (30 points)¶
Use the tools of Chapter 7 (shape, dtypes, describe, value_counts, duplicated, isna, histograms, group comparisons) to find the nine problems. For each one, give:
what the problem is,
the evidence (a number, a table or a plot from your code),
its kind (identifier, constant, duplicate, inconsistent category, unit mix, impossible value, missing-not-at-random, leak, repeated entity…).
Hint: one problem is about the unit of observation: what one row stands for, and whether rows are independent.
# Your code hereYour findings (a table of the nine problems):
| # | Problem | Evidence | Kind |
|---|---|---|---|
| 1 |
Part B — Cleaning and Preparation (30 points)¶
Fix what can be fixed: remove the duplicates; merge the inconsistent labels; convert the weights that one ward recorded in pounds; mark impossible values as missing. Log every change and how many rows it touched.
Drop the columns that must not be features, and say why for each.
Build a scikit-learn
ColumnTransformer+Pipelinethat imputes, encodes and scales each kind of column correctly, with a missing-indicator where the missingness is informative. Explain each choice in one sentence.
# Your code herePart C — The Leakage Hunt (20 points)¶
Train a random forest (300 trees, random_state=0) with your pipeline and report the cross-validated ROC AUC (Chapter 11; 0.5 is chance, 1.0 is perfect) in four settings, and explain each difference:
with the leaky column added to the features, random 5-fold split;
without it, random 5-fold split;
without it, 5 folds by patient (
GroupKFold);which of these numbers should the clinic believe, and why?
# Your code hereYour explanation:
Part D — A Data Card (20 points)¶
Write a one-page data card for the cleaned dataset, with these headings:
Source and unit of observation (what one row is; how many patients, how many rows)
Target (definition, class balance, the baseline accuracy)
Features (kind and preparation of each)
Known problems and fixes (from Part B, with counts)
Leakage risks (what must never be a feature; how the data must be split)
Limits (who is under-represented; what the model cannot be used for)
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| Audit (30) | all 9 problems, each with clear evidence | 6–8 problems, or weak evidence | fewer than 6 |
| Cleaning (30) | every fix logged with counts; correct pipeline; each choice justified | pipeline works but choices unexplained | leaks in the preparation |
| Leakage hunt (20) | the three scores, correctly explained, the right one chosen | scores without explanation | settings missing |
| Data card (20) | complete, precise, honest about limits | incomplete | missing |