The question this layer answers
How do systems infer patterns instead of receiving every rule?
Behavior learned from data. This layer connects Models, Training, Evaluation, Deployment to the rest of the computing stack.
Learning introduces uncertainty into a software system
An ordinary rule can say “flag every transaction above a fixed amount.” A learned classifier estimates a relationship from examples. Its usefulness depends on the data-generating process, the objective, and the consequences of mistakes. This chapter is an extension of the systems foundation: a model is one component whose behavior still needs a measurable contract.
Begin with a baseline. If only 20 of 1,000 cases are positive, predicting negative for everything yields 98% accuracy and finds none of the positives. A high headline number can conceal failure at the task.
Suppose a model flags 40 cases, of which 15 are actually positive:
| Actually positive | Actually negative |
|---|
| Predicted positive | 15 true positives | 25 false positives |
| Predicted negative | 5 false negatives | 955 true negatives |
Precision is 15 / (15 + 25) = 37.5%; recall is 15 / (15 + 5) = 75%; accuracy is (15 + 955) / 1000 = 97%. Compared with the trivial baseline, accuracy decreased while detection improved. Whether that trade-off is worthwhile depends on review capacity and error costs.
A threshold is a policy decision
A model may emit a score, with a threshold converting scores into actions. Raising the threshold generally reduces the number flagged and trades recall against precision, though results depend on the score ordering and data. A score of 0.8 is not automatically an 80% probability unless calibration supports that interpretation.
If reviewers can inspect only 30 cases per day, a model flagging 400 cases may be operationally unusable. Define the human workflow, fallback, and latency budget alongside the statistical metric. Evaluate important subgroups and failure categories rather than trusting an aggregate alone.
Use training data to fit parameters, validation data to choose among alternatives, and a held-out test set for a final estimate. Fit preprocessing transformations on the training partition. If records from the same person are strongly related, splitting individual rows randomly can leak identity-specific information between partitions. If deployment predicts future events, use a split that respects time and avoids future information in features.
Repeatedly selecting changes against the test result turns the test set into another validation set. Uncertainty remains even with a careful split, especially when positive examples are rare or the deployment population differs.
Before moving on: state which error is more harmful, calculate precision and recall by hand, and propose a split matching the intended deployment. The lab tests these decisions without requiring a model API, a paid service, or a large dataset.
Free AI & machine learning lessons
Targets, data, baselines, and leakage
Define who uses the output, which action it changes, when features are available, and how success is measured. The label is a proxy for the real outcome and can encode historical bias or process quirks. A simple heuristic or linear baseline reveals whether complexity earns its cost. Leakage occurs when training sees information unavailable at decision time.
For rare-event prediction, always guessing “no” can have high accuracy while providing no value. Compare against prevalence, a business rule, or a simple model using the metric and cost that matter. If a complex model barely improves decisions, its latency, maintenance, and failure surface may not be justified.
Models, loss, gradients, and regularization
Examples flow forward to predictions. A loss turns prediction error into an optimization signal. Gradients estimate how parameter changes affect that loss; an optimizer updates parameters across batches. Regularization, architecture, and data augmentation constrain the search toward functions likely to generalize rather than memorize.
High training and validation error suggest the model, features, or optimization cannot capture enough signal. Low training error with worsening validation error suggests memorization or distribution mismatch. More capacity helps the first case and can worsen the second; regularization or better data may help the second.
Generalization, metrics, calibration, and uncertainty
Random splits can leak people, time, devices, or near-duplicates across train and test. Temporal or group-based splits better simulate many deployments. Precision and recall describe different error tradeoffs; calibration asks whether predicted probabilities match frequencies; confidence intervals reflect sampling uncertainty. Slice analysis finds failures hidden by an average.
A model can rank risky cases well but output probabilities that are systematically too high. Ranking metrics may look good while a “0.8” score corresponds to only 0.5 observed frequency. If decisions multiply probability by cost, calibration matters directly and should be checked over time and subgroups.
Deployment, drift, feedback, safety, and human judgment
The model influences decisions, which influence future labels and who appears in the data. Feature pipelines can skew between training and serving. Populations, incentives, and measurement systems drift. Production therefore needs input validation, shadow or staged rollout, outcome monitoring, fallback behaviour, auditability, and retraining criteria.
Track schema and feature health, input distribution, prediction distribution, latency, decision rate, delayed outcomes, calibration, subgroup performance, and business guardrails. A shifted input is a warning, not proof of harm; unchanged inputs do not prove outcomes are sound. Link alerts to investigation and rollback actions.
Calculate a split, then explain what the training score leaves out.
Separate optimization failure from a model that fits too much or too little.
Understand what an unlabeled objective can and cannot discover.
Model delayed reward without confusing prediction with control.
Practice lab: Find the model hidden by accuracy
About 30 minutes. Calculate fractions and percentages. Understand that training fits parameters while evaluation measures predictions on held-out examples.
A held-out dataset has 1,000 cases: 100 positives and 900 negatives. A model flags 80 cases: 60 are true positives and 20 are false positives. The other 920 cases contain 40 missed positives and 880 true negatives. A review team can inspect at most 50 cases per day.
- Calculate accuracy, precision, recall, and the accuracy of always predicting negative.
- Explain why the given operating point is incompatible with the review capacity. State what additional evidence is needed to choose a new threshold.
- Design a data split for predicting next month’s behaviour when multiple records belong to the same person. Include one check for uneven harm across groups.
Hint
Accuracy counts all correct predictions. Raising a threshold can reduce volume, but this table does not tell you which errors will disappear.
Worked solution
Accuracy is (60+880)/1000 = 94%; precision is 60/80 = 75%; recall is 60/100 = 60%. Always-negative accuracy is 900/1000 = 90%, despite zero positive recall. If these counts describe a day, 80 flags exceed the capacity of 50. Evaluate ranked scores and decision costs at the feasible operating point; you cannot infer precision at 50 from the aggregate confusion matrix. Use time-respecting splits and ensure future information does not leak through person histories; whether to hold out whole people depends on whether the claim concerns new people or future records for known people. Report errors and uncertainty for relevant groups rather than relying only on the aggregate.
- Use the correct denominator for each metric.
- Avoid inventing threshold performance from aggregate counts.
- Match the split and subgroup checks to the actual deployment claim.
Change one assumption
If missing a positive costs ten times as much as a false alert, explain why neither accuracy nor precision alone determines the best decision policy.
Evaluate a model without fooling yourself
Calculate objectives, construct leakage counterexamples, and distinguish an empirical pattern from a causal or deployment claim.
1. Calculate and choose · allow 40 minutes
A better accuracy can be the worse decision
On 1,000 cases there are 100 positives. Model A has TP=60 and FP=10; model B has TP=85 and FP=60. Compute confusion matrices, accuracy, precision, and recall. A false negative costs 20 units and a false positive costs 1. Which model has lower empirical cost? For a calibrated positive probability p, derive the threshold for predicting positive under these costs. Explain why a tree trained on impurity does not directly optimize this deployment cost.
Get a starting hint
Complete each matrix from the class totals. Compare conditional expected loss for the two possible actions.
Compare your reasoning with the worked solution
A has FN=40 and TN=890: accuracy 950/1,000=95%, precision 60/70≈85.71%, recall 60%. B has FN=15 and TN=840: accuracy 925/1,000=92.5%, precision 85/145≈58.62%, recall 85%. Costs are A:40×20+10=810 and B:15×20+60=360. B is preferable under the stated cost despite lower accuracy and precision.
Predicting positive has expected cost 1−p; predicting negative costs 20p. Choose positive when 1−p<20p, or p>1/21≈4.76%, with an explicit tie policy. This derivation requires calibrated probabilities for the deployment population and the assumed costs; a threshold tuned on a changing population may not retain its meaning. A decision tree's impurity reduction is a local training criterion. Its splits, class weights, pruning, probability calibration, and final decision rule must be evaluated against the actual objective using held-out data. These finite sample costs are estimates, not guarantees about future users.
Check your reasoning
- Compute all four matrix cells.
- Derive the decision threshold from losses.
- State calibration and population assumptions.
2. Experimental design · allow 40 minutes
The split must match the future prediction
A hospital dataset has ten visits per patient. A team randomly splits visits, standardizes using all records, chooses regularization on the test set, and reports test accuracy. Identify three leakage or evaluation failures. Design a pipeline for predicting outcomes for new patients next year. Interpret two learning curves: low training error with high validation error; high error on both. Why does adding more data not guarantee either problem disappears?
Get a starting hint
The unit of independence is not necessarily a row. Every learned preprocessing step belongs inside the training fold.
Compare your reasoning with the worked solution
Visits from one patient can appear in both sets, letting patient-specific signals inflate performance. Computing means and scales from all records uses held-out information, even without labels. Repeatedly selecting regularization on the test set turns it into a validation set and biases the final reported estimate. Use patient-disjoint development splits, fit preprocessing on each training fold, tune with validation data, then evaluate once on an untouched patient-disjoint future-period test set. Ensure every feature would be available at prediction time; post-outcome treatment codes are not valid predictors for an earlier decision.
Low training error with high validation error suggests overfitting, leakage-related mismatch, or distribution shift; regularization and data may help, but diagnose subgroup and time behavior. High error on both can indicate underfitting, poor features, optimization failure, label noise, or an intrinsically difficult target. More examples cannot repair an invalid target, unavailable features, systematic label bias, or a model class incapable of expressing the needed relation. Report uncertainty and the population represented by the evaluation rather than a single score without context.
Check your reasoning
- Separate patients and time appropriately.
- Fit all learned transforms within training.
- Treat learning-curve patterns as diagnostic evidence, not unique diagnoses.
3. Derive and refute · allow 40 minutes
Maximum variance is not maximum usefulness
Four centered 2D points are (2,1),(2,−1),(−2,1),(−2,−1). Using covariance divisor n, compute the covariance matrix, principal axes, and fraction of variance retained by one component. Labels are positive exactly when y>0. What information does one-component PCA lose? Explain how scaling and the choice of k affect k-means on the same points.
Get a starting hint
The cross-products cancel. An unsupervised objective does not know the labels.
Compare your reasoning with the worked solution
The covariance matrix is [[4,0],[0,1]]. Eigenvectors are the x and y axes with eigenvalues four and one. The first component retains 4/5=80% of variance but maps (2,1) and (2,−1) to the same coordinate, and likewise the two x=−2 points. It therefore discards all label information in this symmetric example: predicting from x alone cannot distinguish the balanced classes. Using n−1 instead scales both eigenvalues equally and leaves axes and explained-variance fractions unchanged.
With k=2 and ordinary squared Euclidean distance, grouping by x has within-cluster sum of squares four; grouping by y costs sixteen. The k-means objective favors x groups even though y determines the labels. Rescaling y by a large factor can reverse that preference. At k=4 each point can be its own cluster with zero training distortion, which does not establish a useful model. Choose scaling from measurement meaning, and choose k using a stated purpose, stability, and held-out or domain evidence. PCA and clustering optimize geometric criteria, not causal structure or automatically meaningful categories.
Check your reasoning
- Compute covariance and explained variance.
- Show the collision of opposite-label points after projection.
- Explain scaling and objective dependence.
4. Bellman calculation · allow 40 minutes
Immediate reward and long-run value disagree
State S has two actions. Quick gives reward 4 and terminates. Wait gives reward 0 and moves to T. T has one action giving reward 6 and terminating. Calculate optimal values with discount γ=0.5 and γ=0.9. Now T yields 6 with probability 0.8 and −10 otherwise; recompute the choice at γ=0.9. Explain why off-policy evaluation from logs containing only Quick cannot identify Wait's value without additional assumptions.
Get a starting hint
The value at S includes one discount factor before T's reward. An action absent from the data is not made observable by a large sample of other actions.
Compare your reasoning with the worked solution
In the deterministic case V(T)=6. At γ=0.5, Q(S,Wait)=3, so Quick wins with value four. At γ=0.9, Wait is worth 5.4 and wins. In the stochastic case, expected reward at T is 0.8×6+0.2×(−10)=2.8. Discounted Wait value is 2.52, below Quick's four. The calculation uses expected discounted reward; a risk-sensitive or safety-constrained objective could make a different comparison even with the same expectation.
Logs with only Quick contain no outcomes under Wait. They cannot identify its value without a model, prior knowledge, or new exploration assumptions. Importance weighting requires support: the behavior policy must assign positive probability to actions the target policy may take in the relevant states. A large unsupported weight cannot be computed when that probability is zero. Any proposed experiment should account for the cost of the −10 outcome and whether exploration is acceptable in the application. Distinguish statistical uncertainty from missing identifiability.
Check your reasoning
- Apply discount at the correct transition.
- Compute the stochastic expected reward before discounting.
- Explain the support problem and objective assumptions.
Study the underlying methods
Section project: Train and audit a small classifier from first principles
5 to 7 hours. Implement binary logistic regression using only Python’s standard library on a generated, fully documented tabular dataset. Inputs are a random seed, sample count, two numeric features, and labels drawn from a specified probabilistic rule. Generate a separate held-out dataset before model selection. Fit feature scaling on the training partition only, implement the logistic function with a numerically safe branch, and minimize average cross-entropy with gradient descent. Output the learned parameters, training-loss trace, held-out confusion matrix, precision, recall, and accuracy at a declared threshold, with explicit behavior for zero denominators. Compare with a majority-class baseline and inspect individual mistakes. Then deliberately shift one feature’s distribution in a second evaluation set and describe the observed change without claiming general deployment safety. Include a finite-difference gradient check on a tiny fixture. No external dataset, hosted model, or paid API is required. The report must distinguish the synthetic data-generating assumptions, optimization behavior, threshold choice, and generalization evidence. Keep the generated true probabilities for analysis, but do not pass them to training as hidden features. Include an imbalanced dataset to show why accuracy alone can obscure minority-class failures.
Milestones
- Specify and implement reproducible data generation and an untouched evaluation split.
- Implement training-only scaling, stable probabilities, loss, and analytic gradients.
- Check gradients numerically and train with a documented learning rate and stopping rule.
- Evaluate the baseline and model using confusion counts and threshold-dependent metrics.
- Run a distribution-shift experiment and examine concrete false positives and false negatives.
Acceptance criteria
- The same seed and configuration reproduce identical datasets and fitted results within the stated arithmetic tolerance.
- Analytic gradients agree with central finite differences on a small nonsaturated fixture within a justified tolerance.
- Feature means and scales are derived only from the training partition and remain fixed for evaluation.
- Confusion-matrix cells sum to the held-out sample count, and every reported metric can be recomputed from them.
- Extreme positive and negative scores do not overflow the logistic calculation or produce an undocumented non-finite loss.
- The report preserves the untouched evaluation results and does not tune the threshold on that same set before presenting it as held out.
Implement a calibration table with bin counts and uncertainty caveats, then compare probability quality with classification accuracy.
Practise AI & machine learning free →