Contents/ Part XXXI · Capstone Projects: Machine Learning/ Chapter 190

The Modeling Workflow

When the product is a prediction rather than a coefficient, the framework changes shape. Assumption checking becomes validation design, the enemy becomes leakage, and the metric stops being a statistical choice and starts being an accounting one.

⏱️ ~18 min read
🎯 The workflow
📊 Chapter 190

Every part before this one estimated something. This one predicts something, and that single change moves almost every decision in the framework, including two that stop existing and two that appear from nowhere.

Ŷ
An estimate is judged against the truth it is trying to recover, which you never see. A prediction is judged against outcomes you can hold back and check. That is the whole difference, and it is why the machinery of validation replaces the machinery of assumption.
What this part is for

Seven projects across supervised, unsupervised and reinforcement learning. In each one the model trains without complaint, the headline metric looks good, and the naive answer is wrong: by a metric that flatters a useless model, by a feature that could not have existed, by clusters in data with no clusters in it, and by a policy evaluated on the log it came from.

1

What Changes, and What Does Not

The twelve-step framework from Capstone 1 still runs. Four of its steps mean something different here, and pretending otherwise is how most first machine-learning projects go wrong.

Framework stepIn an inference projectIn a prediction project
State the questionWhich effect, on whom, compared with whatWhich decision, made how often, at what cost per error
Check the assumptionsNormality, constant variance, independenceValidation design: does the split mimic how the model will be used?
Choose the methodFollows from the data type and the designFollows from the data type, and then from a bake-off nobody can skip
Report the resultAn effect with an intervalOut-of-sample performance, on a metric tied to the cost
State the limitationsConfounding, generalizability, measurementLeakage, drift, and the population the training data represents

Two things vanish. Statistical significance stops being interesting: nobody asks whether a random forest's improvement over a baseline is significant, they ask whether it is large enough to pay for the pipeline. And the coefficient stops being the product, which is what makes explainability a separate problem rather than a free by-product.

Two things appear. Leakage, which has no analog in the inference chapters, and the baseline, which does: it is the "compared with what" of the first row, and it is skipped just as often.

2

Validation Design Replaces Assumption Checking

A held-out set is not a formality. It is the only instrument you have, and how you cut it decides what it measures. The rule is short: the split must mimic the way the model will actually be used.

If the model willSplit byA random split would
Score new customersCustomerPut the same customer in train and test
Forecast next monthTime, with a rolling originTrain on the future to predict the past
Score a new hospital's patientsHospital, groupedLearn that hospital's quirks and call it generalization
Run on next year's transactionsTime, then check driftReport a number that decays after deployment
Score any unit onceAt random, stratified on a rare outcomeBe correct, which is why the default is a default
Three sets, and what each one is for

Train fits the parameters. Validation chooses between models and settings. Test is looked at once, at the end, to report a number. The moment you use the test set to choose anything, it has become a validation set and you no longer have an honest estimate of performance. Cross-validation replaces the first two; it does not replace the third.

3

Leakage, and Why It Looks Like Success

Leakage is information in the training data that will not exist at the moment of prediction. It is the defining failure of applied machine learning, and its signature is a model that performs implausibly well.

information that exists at prediction time information that does not exist yet the moment of prediction legitimate features the outcome in disguise a field updated after the event a statistic computed over all the data a duplicate that is also in the test set LEAKAGE
One question catches most of it. For every feature, ask: would this value have been available, with this value, at the moment the prediction is needed? A field that is only populated once the outcome is known answers no, however innocent its name.

The subtle cases are the ones that survive review. Scaling before splitting lets the test set contribute its mean and standard deviation to the training data. Imputing before splitting does the same with medians. Selecting features on the full dataset and then cross-validating the chosen ones reports the performance of a search that already saw the answers. The fix in all three cases is the same: every step that learns anything from data belongs inside the fold, which is what a pipeline object is for.

4

The Metric Comes From the Cost

Accuracy is a default that suits almost nothing. The metric is an accounting decision about what being wrong costs, and it should be settled before a model is fitted.

SituationMetricBecause
Rare positive, both errors costPrecision-recall AUC, then a cost-weighted thresholdROC-AUC flatters a model when negatives dominate
The number itself is usedCalibration, Brier scoreRanking is not enough when a probability is read
A ranked shortlistPrecision at kOnly the top of the list will ever be looked at
Continuous outcome, outliers matterMAE, or a quantile lossSquared error lets a few large errors set the model
Continuous outcome, scale variesMASE or a percentage error, carefullyPercentage errors break at values near zero
A policy that actsExpected reward, evaluated off-policyThe log was produced by a different policy than the one you are testing

Notice how much of that table is about asymmetry. Most real decisions have two errors that cost different amounts, and almost every default metric assumes they cost the same. Capstone 29 makes this concrete on a fraud problem where the two errors differ by three orders of magnitude.

5

The Baseline That Has to Be Beaten

A performance number on its own means nothing. It means something against the cheapest thing that could have been done instead, and that comparison is what tells you whether the model earns its maintenance.

ProblemThe baseline to beat
ClassificationPredict the majority class; then a logistic regression on five obvious features
RegressionPredict the mean; then a linear model
ForecastingPredict last period, or the same period last year
Ranking or targetingThe current business rule, whatever it is
Any of themDoing nothing, which is free and sometimes wins

The last row is not a joke. A model that improves accuracy by a point and costs an engineer a week a month to maintain has a negative return, and no metric on the model's own scorecard will surface that.

6

Explanations, and What They Are Evidence Of

Because the coefficient is no longer the product, explanation becomes a separate exercise with its own tools and its own failure mode.

The one thing to keep straight

SHAP and LIME explain what the model did. They do not explain what the world does. A feature with a large SHAP value is a feature the model leaned on, which may be because it causes the outcome, because it proxies for something that does, or because it leaked. An explanation is evidence about the model and is not evidence about the mechanism, and the difference matters most in exactly the settings where somebody wants to act on it.

That said, they are the best leakage detector there is. A feature at the top of an importance ranking that nobody expected to matter is the single most reliable sign that something in the pipeline knows the answer.

7

When There Is No Outcome at All

Three of the projects ahead have no labels, which removes the instrument the rest of this chapter is built on. Without a held-out outcome there is nothing to be right or wrong about, and the discipline has to come from somewhere else.

Without labels you cannotSo instead you
Measure accuracyMeasure stability: does the answer survive resampling the data?
Tune to a metricCompare against what pure noise produces, which is never nothing
Validate a thresholdPrice it: how many alerts per day can a human actually read?
Prove the structure is realState what would have to be true for it to be, and test that

Clustering always returns clusters and anomaly detection always returns anomalies, on any data whatsoever, including data generated with no structure in it at all. Capstones 33 and 35 run exactly that experiment, because the output looks identical either way.

8

The Seven Projects Ahead

Seven projects across the three families, each one built around a decision the metric cannot make for you. The Contents always shows what is live.

Supervised · when you have labels
1
Imbalanced classification
Credit Card Fraud
Resampling, class weights and threshold tuning are three answers to one problem. Only one of them survives a cost matrix.
2
Explainability
Churn Prediction
A full bake-off, SHAP and LIME, and the retention economics that decide who is actually worth a phone call.
3
Gradient boosting
Tabular Regression
Hyperparameter search, learning curves and error analysis, measured against the linear model it has to beat.
Unsupervised · when you have none
4
Dimension reduction
A Survey Battery
Twenty correlated Likert items into a few indices. How many components, and what are you entitled to call them?
5
Clustering
Choosing k Honestly
Every method returns clusters. The question is whether they survive resampling, and on noise they do not.
6
Anomaly detection
Without Labels
Setting a threshold with no ground truth, and the alert fatigue that follows from setting it wrong.
Reinforcement · when the model acts
7
Reinforcement learning
Dynamic Pricing
Bandits to contextual bandits to Q-learning, and how to evaluate a policy offline before it touches a customer.

🎓 Key Takeaways

  • Validation design replaces assumption checking. The split has to mimic how the model will be used, or the number it produces is about a situation that will never occur.
  • Leakage looks like success. Ask of every feature whether it would exist, with that value, at the moment of prediction, and keep every fitted step inside the fold.
  • The metric is an accounting decision. Most real problems have two errors that cost different amounts, and almost every default assumes they do not.
  • A number means nothing without a baseline. Including doing nothing, which is free and sometimes wins.
  • Explanations describe the model, not the world. Which is also what makes them the best leakage detector available.

Quiz: Test Yourself