Machine Learning · Chapter 26 of 26
Capstone: a broken model, and its model card
By the end of this chapter you can review someone else's model the way Tanvi does: find each fault, measure how much it changed the number, fix them in order of size, report the honest score with its spread, and write the model card that says what the model is for, how it does for each group of buyers, and when Dobara should not use it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real work
- 5 tasks
- Python
- In the all labs plan
The first 2 chapters of Machine Learning are free. This one comes with the all labs plan.
Why you need this
Before Tanvi joined, Dobara's first analyst built a credit model and sent Rachna one line: "99.5 % accurate". Rachna never used it, because she could not tell what the number meant, and she was right not to. The code is still on the shared drive, and Raju found it last week and asked whether Dobara could start lending by it.
Every fault in it is one this lab has met, and each one alone makes a model look better than it is. Together they produce a number that is nearly perfect and means nothing. You know how to build a model right; this chapter is about the other half of the job, reading a model built wrong, saying exactly what is wrong and by how much, and writing down what the fixed one can and cannot do so the next person does not have to guess.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
leak_hunt.py: find the leak, one column at a time
Write single_column_aucs(X, y) in leak_hunt.py. X is a DataFrame of number columns, some with blanks, and y the label, 0 or 1, with the same row labels. For each column of X, keep the rows where that column is not blank and take the ROC AUC of the column's own values as the score against y on those rows. A column that ranks the wrong way round (an AUC below 0.5) is just as telling, so use 1 minus its AUC then.
Return a pandas Series named auc, labelled by X's columns, sorted from the largest AUC to the smallest. The Check tries the loans' ten number columns with collection_calls, the last 800 loans, and a made-up table of 3 columns (one with blanks, one that ranks backwards).
- Task 2
fixed_model.py: the analyst's model, every fault fixed
Dobara's first analyst wrote honest_model(loans) in fixed_model.py and reported "99.5 % accurate". loans is a DataFrame with the columns of loans.csv. It has six faults: it uses collection_calls (known only after a loan is given), has no baseline, reports accuracy on 14 % defaults, does not stratify its split, picks the depth by the score on the test rows, and lets a tree memorise (None in its depths).
Fix it so that X is the NUMBERS columns only (listed in the starter) and y is defaulted; it splits with train_test_split, test_size=0.25, random_state=0 and stratify=y; it picks max_depth from [2, 3, 4, 5, 6] with a GridSearchCV of DecisionTreeClassifier(random_state=0), cv=StratifiedKFold(5, shuffle=True, random_state=0) and scoring="roc_auc", fitted on the training rows; and it scores the search's best tree once on the test rows, as a ROC AUC from its probabilities of default.
Return three values separated by a comma: the best tree (fitted on the training rows), the search's best_score_ (its mean cross-validated AUC), and the test AUC. The Check also tries the first 1,600 loans.
- Task 3
by_group.py: recall for each occupation
Write recall_by_group(y_true, y_pred, groups) in by_group.py. y_true is the test loans' defaulted (a Series), y_pred the model's decisions for them (a NumPy array of 0 or 1, where 1 is refuse), and groups a Series of each loan's group (an occupation or a branch), all three in the same order: compare them by position, not by row label.
Return a DataFrame with one row per group, labelled by the group's name and sorted by name, with exactly these columns in this order: loans (how many loans the group has), defaults (how many of them defaulted), caught (how many of the defaults were refused) and recall (caught divided by defaults, a float; NaN, missing, for a group with no defaults). The Check tries occupation and branch on its own model's test loans at a cut of 0.2, and 7 loans of its own with one group that has no defaults.
- Task 4 · interview question
Interview: which_fault.py, which fault mattered most
Rachna asks which of the analyst's faults mattered, and by how much. Write fault_sizes(loans) in which_fault.py: loans is a DataFrame with the columns of loans.csv. Every model is a DecisionTreeClassifier with random_state=0, y is defaulted, the split is train_test_split with test_size=0.25 and random_state=0, and each value is the tree's ROC AUC on the test rows from its probabilities of default.
Return a dict with exactly four keys: "fixed" (X the NUMBERS columns, max_depth=4, stratify=y), "leak" (X the NUMBERS columns and collection_calls, max_depth=4, stratify=y), "unstratified" (X the NUMBERS columns, max_depth=4, no stratify) and "memorising" (X the NUMBERS columns, no max_depth, stratify=y). The Check also tries the first 1,600 loans.
Tanvi will ask: which fault changed the number most, which changed it least, and why is the unstratified number not a reason to skip stratifying?
- Task 5 · interview question
Interview: model_card.py, when not to use it
The model card's last section says when Dobara should not let the model decide alone. Write needs_a_person(applications, train_loans) in model_card.py. applications is a DataFrame of new applications (the columns of applications_new.csv), and train_loans the loans the model was trained on (the columns of loans.csv).
An application needs a person when its dealer is not among the train_loans' dealers, or its monthly_income_rs is below the smallest or above the largest monthly_income_rs of train_loans, or its age is below the smallest or above the largest age of train_loans (a value equal to the smallest or largest is inside the range). Return a pandas Series of True and False named needs_person, one per application, with the row labels of applications. The Check tries this week's applications against the loans before 1 April 2025 and against the first 600 loans, and 5 applications of its own.
Tanvi will ask: why can the model not be trusted on these rows, and what else in the model card says when to stop using it?
What you will learn
- Six faults and the chapter each one comes from: a leaked column, no baseline, the wrong metric, an unstratified split, tuning on the test rows, a memorising tree.
- Measuring a fault: change one thing at a time and watch the number.
- The review checklist: the questions to ask of any model before anyone lends by it.
- The model card: purpose, data, metrics with their spread, the threshold in rupees, groups, columns left out, drift, and when not to use it.
More in Real work
Build "Capstone: a broken model, and its model card" yourself
Dobara refurbishes used bikes in Indore and lends to its buyers. You build the models that decide whom to lend to and at what price to sell. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Machine Learning are free. This one comes with the all labs plan.