AI Run Lab

All chapters

How do you know a model works?

A test must reflect the decision the model will support. Examine what was held out, which mistakes count and whether the data resembles the setting where the model will be used.

Open this chapter with experiments and audio

What counts as an unseen example?

Training data influences fitted parameters. Validation data helps choose features, settings or models. A final test set estimates how the chosen procedure performs on unseen examples. If you repeatedly adjust the model after inspecting the test results, those examples influence the design and no longer provide the same independent check.

Consider estimating next year’s house prices. A random split can place records from later months in training and earlier months in testing. A chronological split better reflects the intended forecast. Similarly, several photos of the same property can leak property-specific information across a random image split. Splitting by property tests a more meaningful kind of novelty.

An apparent improvement can come from a more informative test leak rather than a better model. Document the unit you split, the cutoff for time-dependent data and every preprocessing operation that learns from examples. Those details make later comparisons interpretable.

Does a closer fit travel to new inputs?

A sensor should report twice its input. We record it at 1, 2 and 3. Add a measurement error to the middle reading. Both models see the same three observations. Predict which model will be closer at the two unmeasured positions.

Try zero error. Both models recover the same rule. The advantage of the line in the noisy case depends on the assumed straight sensor response. A sensor with real curvature would call for a different test.

Read a confusion matrix as counts

An email system evaluates 1,000 labelled messages. Of 120 spam messages it flags 90 and misses 30. Of 880 genuine messages it flags 10 and keeps 870. Read each row as a real-world category and each column as the system’s decision. Keeping these directions explicit prevents denominator mistakes.

Actual categoryFlaggedKept
Spam90 true positives30 false negatives
Genuine10 false positives870 true negatives

Among the 100 flagged messages, 90 are spam. That fraction is precision, 90/100 = 0.90. Among the 120 actual spam messages, 90 were caught. That fraction is recall, 90/120 = 0.75. Accuracy combines both correct groups, (90 + 870)/1,000 = 0.96. Each number answers a different question about the same decisions.

Keep 90 caught spam messages and reduce flagged genuine mail to zero. Precision reaches 100%, yet 30 spam messages still get through. Then set both controls to zero. Recall falls to zero and precision has a zero denominator because the system never made a positive prediction. That result explains why a single headline metric can be misleading.

Which mistake matters more?

A mail provider may care especially about hiding legitimate mail. Raising the flagging threshold can decrease false positives while also missing more spam. A monitoring system for an important rare event may prioritize recall. Choose an operating point from validation data, then evaluate once on held-out data. Compare with a baseline; always keeping all messages already yields 88% accuracy here but catches no spam.

tp, fp, fn, tn = 90, 10, 30, 870
print("precision", tp / (tp + fp))
print("recall", tp / (tp + fn))
print("accuracy", (tp + tn) / (tp + fp + fn + tn))

Metrics vary with the population. A test made exclusively of short English messages can leave errors on long Dutch messages invisible, even if the overall accuracy is high. Report meaningful subsets with their sample counts. Severe uncertainty remains when a subset contains only a handful of cases.

Build a small evaluation for the housing model

Keep the three sales from chapter 10 for training. Create unseen sizes of 60, 90 and 110 m² with targets of €180,000, €270,000 and €340,000. The last target deliberately differs from the rule. Write the error for each prediction, then compute the mean absolute error. A table of individual examples tells you where the average comes from.

What does a rule of €3,000 per square metre score on these test homes?

Predictions are €180,000, €270,000 and €330,000. The absolute errors are 0, 0 and €10,000. The mean is about €3,333. With only three invented examples the result demonstrates the metric; it cannot establish market performance.

scikit-learn · Cross-validation

scikit-learn · Metrics and scoring

The detector stays the same. Why do its alerts become less reliable?

A quality inspector tests 10,000 items from a production line. Exactly 1,000 are defective. A detector catches 90% of defective items, so it raises 900 correct alerts. It also flags 5% of the 9,000 good items, or 450 false alerts. Someone receiving an alert therefore sees a real defect in 900 out of 1,350 alerts, about 67%. This fraction is precision. The 90% catch rate is recall. Their denominators answer different questions.

The factory improves its process. Only 100 of the next 10,000 items are defective. Assume the same conditional detection rates still apply. Now there are 90 correct alerts and 495 false alerts among 9,900 good items. Precision falls to 90 / 585, about 15%. The detector has the same settings and the same sensitivity. The population from which its alerts are drawn changed.

Will a higher alert threshold always reduce the cost of inspection?

It can reduce false alerts and increase missed defects. To choose a threshold, measure both changes on representative items and assign consequences to each type of mistake. Even the calculation above depends on an assumption. A new manufacturing process might produce different kinds of defects that the detector misses more often.

Compare the four cells in the confusion matrix →

Further reading · scikit-learn · Metrics and scoring