All lessons in Machine Learning Engineering: Reliable Predictive Models
Frame a Prediction Contract
Define the observation, prediction time, label window, action, and non-model baseline.
25 minutes · Free lesson
What you will learn
- Specify the unit, prediction time, target, horizon and action of a predictive-model task.
- Reject features whose availability does not match the prediction contract.
- Define a baseline and acceptance evidence before fitting candidate models.
A parcel carrier wants fewer late deliveries. That goal alone does not define a machine-learning problem. A model that recognizes lateness after the delivery event could score perfectly and still be useless to a dispatcher. The engineering task starts by identifying a decision that can change while there is still time to act, then defining what information exists at that moment.
This course follows a fictional carrier that scores parcels when the van leaves the depot. The required outcome is a reproducible model comparison and a defensible decision about whether the model is useful. The core lessons develop the statistical reasoning and investigation needed for that outcome. Optional programmes explore additional model families. You should be comfortable manipulating arrays in Python, reading elementary algebra, and interpreting a probability; derivatives are introduced through small calculations when needed.
Write a contract before opening the model library
A prediction contract connects six pieces of the problem. The unit tells you what one row represents. Prediction time fixes the information boundary. The target defines what you eventually measure. The horizon states how far into the future that measurement reaches. The population describes which cases the claim covers. The action explains why a score matters.
| Contract field | Parcel example |
|---|---|
| Unit | One parcel at its initial dispatch |
| Prediction time | The recorded departure instant |
| Target | Arrival later than the promised delivery window |
| Horizon | From dispatch through the end of that window |
| Population | Eligible deliveries on the routes under study |
| Action | Flag a parcel for a limited dispatcher review queue |
The table needs operational definitions. Does a parcel returned by the customer count as late? What happens if a delivery scan is missing? Is an updated promise allowed to overwrite the promise known at dispatch? These choices affect labels, not just data cleaning. Preserve the original promise or a versioned record; otherwise a successful-looking model may simply learn a retrospective definition.
A late-arriving event can be backdated. A weather forecast issued before dispatch is a possible input; the observed storm total at the end of the day is a different variable. Write down which one you intend to use and how its timestamp is established.
Worked example
A score that arrives too late
- A parcel leaves at 08:00 with a promised arrival by 18:00. The dispatch queue is reviewed at 08:05. Its actual delivery scan arrives at 19:20.
- Depot load recorded at 07:55 and a forecast published at 06:00 are candidates for the 08:00 model. Their later usefulness still needs evaluation.
- A route congestion measurement recorded at 11:00 cannot be an input to the 08:00 score. An end-of-day field saying delivery took 11 hours and 20 minutes reveals much of the answer directly.
- The label is late because 19:20 is after the original 18:00 promise. If the promise is revised to 20:00 at noon, the original dispatch task still uses the version known at 08:00.
- A separate noon model could use later information, but it would need its own prediction contract and evaluation. Changing the cutoff silently changes the task.
A probability is not an action
A probability ranks or describes risk. An action consumes resources and changes outcomes. A high late-delivery probability does not establish that contacting a driver will prevent the delay. For now, the learning problem estimates risk, and the dispatch team reviews candidates. Any later claim about the benefit of intervention needs evidence about that intervention.
Record capacity as well as error costs. If dispatchers can review only twenty parcels per morning, a threshold that selects two hundred parcels cannot be adopted unchanged. A ranked queue and a capacity cutoff may be more useful than labeling every row independently. Ties, unavailable scores and urgent exceptions need explicit policies.
Suppose a missed late parcel has an illustrative cost of four units and an unnecessary review costs one. These are teaching assumptions, not measured business losses. They let you compare decisions using a transparent score. They do not prove reviews resolve delays or that every parcel has the same cost.
Establish the comparison before searching
A baseline gives a more complex model something concrete to beat. For probability prediction, a constant model can return the training late rate. For an action queue, another baseline might review no parcels or use an existing operational rule. These answer different questions, so record both when appropriate.
The target and action determine the scores. Probability quality can be evaluated with log loss. A fixed action rule can be evaluated with a confusion matrix and stated costs. Later lessons derive these calculations. Here, the important step is to agree on the comparison before searching through models and reporting whichever metric looks best.
Worked example
A useful baseline can have misleading accuracy
- In a held-out set of 1,000 eligible parcels, 80 are late and 920 arrive on time. Predicting on time for every parcel gives 920/1,000 = 92% accuracy.
- The rule detects none of the 80 late parcels. At an illustrative cost of four per missed late parcel, its total cost is 80 × 4 = 320 units.
- Another rule flags 100 parcels: 40 are actually late and 60 are on time. It misses the other 40 late parcels. Its cost is 60 × 1 + 40 × 4 = 220 units.
- Its accuracy is (40 true positives + 860 true negatives)/1,000 = 90%. It has lower accuracy but lower cost under the stated assumptions.
- The comparison is still incomplete if only twenty reviews are possible. Report the capacity violation and evaluate a feasible rule before recommending adoption.
Define completion as evidence
A finished investigation includes the prediction contract, a feature-availability table, an evaluation design, a baseline, a fixed candidate budget, diagnostic results and a final decision. It should explain which claims the data supports and what would make you reject or revisit the model. A collection of trained estimators is not enough to reconstruct the reasoning.
The investigation workbook generates independent synthetic parcel rows and compares simple baselines with tree ensembles. It has no real dispatch timestamps, repeated depots or intervention outcomes. Use it to verify calculations and experiment mechanics. When a lesson discusses time or groups, the task is to design an extension or work through a separate fixture, not to pretend those fields exist in the generator.
The capstone requires a reproducible report with a sealed final evaluation. Optional neural or probabilistic material is not necessary to complete it. Study-time estimates include exercises and investigation; they are author estimates rather than measured completion times.
Exercise
A team wants to predict tomorrow's total late parcels at each depot, using information available tonight at 20:00. State the unit and prediction time, and explain why a row containing one parcel at dispatch belongs to a different contract.
Check your answer
The unit is a depot-day, the prediction time is 20:00 on the previous evening, and the target is the count of late parcels assigned to the defined next-day population. A parcel-at-dispatch row has a different unit, information boundary and target. Aggregating its predictions might support a count forecast, but that is a modeling choice requiring evaluation, not an interchangeable label definition.
Exercise
A training late rate is 0.12. A constant probability baseline scores 200 new parcels. How many late parcels does its probability sum predict in aggregate? Does that mean exactly that many will be late?
Check your answer
The sum is 200 × 0.12 = 24 expected late parcels under that probability model. It does not assert exactly 24 outcomes. The observed count varies, and a change in the new population can make the 0.12 baseline systematically wrong.
Exercise
For a 09:00 score, a feature contains a depot incident with event time 08:30 but ingestion time 10:00. Another feature is a forecast issued at 07:00 for weather at 15:00. Which is eligible, and what evidence should you retain?
Check your answer
The incident is unavailable at 09:00 despite its earlier event time. The forecast is eligible if the scoring system actually had access to that 07:00 forecast version. Retain issuance, availability and version information, together with the cutoff rule; the future time being forecast does not make a forecast itself a future observation.
Workbook workflow
Use python ml_workbook.py --development-only --output development-results for lesson investigations. The downloadable workbook README gives the environment setup. Its CSV contains all generated rows for reproducibility; select only manifest.split.train and manifest.split.validation for development, and fit learned transformations on training rows only. Leave final labels and scores unexamined until the capstone procedure is frozen. Advanced branch lessons explicitly distinguish hand calculations and extensions from the four built-in model candidates.
Further reading
Google: Problem framing — connect a modeling target to an operational objective.
scikit-learn model evaluation — compare the questions answered by probability and classification metrics.
Summary
Define the unit, cutoff, target, horizon, population and action before choosing a model. Feature availability is an operational fact, and a probability estimate is distinct from an intervention's benefit. Baselines, capacity and acceptance evidence make the modeling task reviewable.
Next action
Write a one-page prediction contract for the parcel case, including three eligible features, two rejected features and the evidence you would need to justify each availability decision.