Choosing a model, and applying it properly
Last updated
You now know a dozen models. Which one for this problem? The honest answer is: make a short list from what you know about the data, then let the data decide, fairly, against a baseline. This lesson puts the whole job in order, from the question to watching the model after release: how to make the short list, how to race models without fooling yourself, why boosted trees so often win on tables, and why a model that was right last year may not be now.
By the end of this lesson you can
- Follow an ML project from question to monitoring, and say what each stage is for
- Make a short list of models from the task and the data
- Compare models fairly: against a baseline, on the same cross-validation folds
- Weigh accuracy against readability, and watch a released model for drift
From question to report
Every lesson so far has been one piece of a bigger job. In order, as a UML activity diagram:
Press Walk through it to follow the work round the chart, one stop at a time.
- Ask the question, and decide whether ML is right (lesson 2.12).
- Get the data, and split off a test set at once.
- Explore, clean and build features, inside a pipeline (lessons 4.1, 4.2).
- Score a baseline.
- Compare a short list of models by cross-validation (lesson 4.14).
- Tune the best one (lesson 4.13).
- Good enough? If no, go back to step 3.
- Score the test set, once; report it, release it, and watch it for drift.
Three things about the order matter more than any model:
- The test set is split off first, before you've looked closely at the data, and it's used once, at the end (lesson 2.5). Everything you decide from looking at data, you decide from the training part.
- The loop goes back to the data, not just the model. If the result isn't good enough, better features, more data or cleaner labels usually help more than another model (lessons 4.2 and 4.13).
- The job doesn't end at release. Step 8 shows why.
A short list
You don't need to try every model. The task and the data narrow it down. scikit-learn publishes its own map, "Choosing the right estimator"; here's a shorter one for this course:
Press Walk through it to follow the work round the chart, one stop at a time.
- Are there labels? If no: k-means or DBSCAN for groups, PCA for fewer features, an isolation forest for odd rows.
- Text, images or sound? If yes: naive Bayes or logistic regression for text; neural networks (Stages 6–12) for images and sound.
- Must people read the model? If yes: linear or logistic regression, or a shallow tree.
- Thousands of rows or more? If yes: gradient boosting, a random forest, and a linear model to compare.
- Otherwise: linear or logistic regression, k-NN, an SVM, a random forest.
- Every short list is then compared against a baseline, on the same cross-validation folds.
It makes a short list, two to four models, not a choice. Lesson 4.15 puts every model in one filterable table, with worked cases.
Baselines
Before any race, the score of a model that learns nothing. On the breast cancer data from lesson 2.8:
Always saying "benign", the commoner class, is right 62.7% of the time. Any model has to beat that to be worth anything, and a model reported at "90%" means much less once you know the floor is 63%. For a regressor the baseline is the mean (lesson 2.11), and in many fields there's a third, often the hardest to beat: the rule the experts already use (lesson 2.12).
A fair race
Six models from this course on three data sets. One thing makes the race fair: one set of folds, made once and used for every model, so every model trains and tests on exactly the same rows:
The first table is each model's mean accuracy over the five folds; the second, how much it varied from fold to fold. As bars, with one standard deviation either side:
On penguins, logistic scores highest, 0.988. But logistic, k-NN, SVM, forest, boosting are all within one standard deviation of it: too close to call on this data. A single tree is last every time.
What the race says:
- No model wins everywhere. Logistic regression is top on the penguins and the cancer data, the SVM on the digits.
- Many results are too close to call. On the penguins, logistic regression, the SVM, -NN and the forest are all within about one standard deviation. A difference of 0.003 with a spread of 0.01 is noise. Pick the simplest of those that tie.
- A single tree is always last, which is why lessons 4.5 and 4.6 exist.
- Boosting doesn't win here. These tables are small and clean, and the classes have fairly smooth borders. It's on bigger, messier tables, like 4.P's, that boosting pulls ahead (step 6).
Wrong way: comparing on different splits
How much does one model's score move if you only change which rows land in the test set? Logistic regression on the cancer data, 20 random 80–20 splits:
From 94.7% to 99.1%, for the same model on the same data. Now imagine scoring logistic regression on one split and an SVM on another. A gap of three points between them could be entirely the luck of the split. That's why the race uses one set of folds for every model, and reports the spread as well as the mean; and why one train–test split is too noisy to choose between close models.
Why boosted trees often win on tables
On large, messy tables, gradient-boosted trees and random forests usually beat everything else, neural networks included. Grinsztajn, Oyallon and Varoquaux (2022) tested both on 45 tables and found three reasons in how tables differ from images and text:
- Useless features. Tables often have columns that don't matter. Trees ignore them by never splitting on them; neural networks have to learn to ignore them, and with limited data they don't fully manage.
- Irregular patterns. The label often jumps at thresholds (a price bracket, an age limit). Trees make jumps naturally; neural networks are biased towards smooth functions.
- Columns aren't interchangeable. Each column of a table means something different, with no "nearby pixels" for a network to exploit.
So for a table, put gradient boosting and a random forest on the short list, with a linear model to show how much their extra power is worth. Neural networks win when the data is images, sound or text (Stages 8 to 13), where those three things aren't true.
Readable or accurate?
Sometimes the most accurate model isn't the right one. If a doctor must check a model's reasoning, a bank must tell a customer why a loan was refused, or a regulator must approve the model, it has to be readable: a linear model's weights, or a small tree's rules.
Often the price is small. Lesson 4.15 finds a depth-2 tree, four rules a person can read, within 4 points of gradient boosting on the cancer data. Rudin (2019) argues that for high-stakes decisions you should look hard for a readable model that's good enough before reaching for a black box and then trying to explain it afterwards, because explanations of a black box can be wrong in ways nobody notices.
When the stronger model is worth it, permutation importance (lesson 4.5) says which features it relies on overall, and that's at least a check that it's using sensible ones.
After release: drift
A model learns the world as it was in its training data. The world moves. Prices rise, customers change, a new sensor is fitted, a pandemic arrives. When the data a model meets in use drifts away from its training data, its accuracy falls, and nothing in the model raises an alarm.
A simulation: a house-price model trained only on southern California, then used in the north:
On held-out southern districts, RMSE 0.643. On the north, which it never saw, 0.793: nearly a quarter worse, with the same model and the same features. Moving through time does the same thing, more slowly.
So a released model needs watching:
- Log its inputs and predictions, and compare their distributions with the training data's. A feature whose mean has moved is an early warning.
- Score it on new labelled data as the true answers arrive, and compare with the test score you reported.
- Plan to retrain, on a schedule or when the scores drop, and keep the old model's test results to compare against.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Grinsztajn, Oyallon and Varoquaux, Why do tree-based models still outperform deep learning on tabular data?, NeurIPS Datasets and Benchmarks, 2022: 45 tables, and why trees win on them
- scikit-learn: Choosing the right estimator: scikit-learn’s own map from task and data size to model
- Amershi et al., Software engineering for machine learning: a case study, ICSE-SEIP 2019: the nine stages of an ML workflow, and monitoring after release
- Rudin, Stop explaining black box machine learning models for high stakes decisions and use interpretable models instead, Nature Machine Intelligence 1, 2019: the case for readable models where decisions matter