Traditional models · lesson 14 of 16
Parts in this lesson
ADummyClassifier(strategy='most_frequent')2.8: the score to beat
BStratifiedKFold(5, shuffle=True, random_state=0)one set of folds, shared by every model
Ccross_val_score(model, X, y, cv=folds)2.9: every model scored the same way

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:

[no][yes]Ask the question;is ML right? (2.12)Get the data; split offa test set and lock itExplore, clean, buildfeatures (4.1, 4.2)Score a baseline(2.11, 4.14)Compare a short listby cross-validationTune the best one(4.13)Good enough?Score the test set,onceReport it; release it;watch it for drift

Press Walk through it to follow the work round the chart, one stop at a time.

  1. Ask the question, and decide whether ML is right (lesson 2.12).
  2. Get the data, and split off a test set at once.
  3. Explore, clean and build features, inside a pipeline (lessons 4.1, 4.2).
  4. Score a baseline.
  5. Compare a short list of models by cross-validation (lesson 4.14).
  6. Tune the best one (lesson 4.13).
  7. Good enough? If no, go back to step 3.
  8. Score the test set, once; report it, release it, and watch it for drift.
A machine learning project from question to monitoring, as a UML activity diagram. In UML’s notation: the filled dot is where it starts and the ringed dot where it ends; rounded boxes are actions; a diamond with a question is a decision, with the condition for each way out in [brackets], and an empty diamond is a merge, where a way back joins the flow; Pink arrows go back to an earlier action.

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:

[no][yes][yes][no: a table][yes][no][yes][no]Are therelabels?Groups: k-means, DBSCANFewer features: PCAOdd rows: isolation forestText, imagesor sound?Text: naive Bayes, logisticImages, sound: neuralnetworks (Stages 6–12)Must peopleread the model?Linear or logisticregression, a shallow treeThousands ofrows or more?Gradient boosting,random forest,and a linear modelLinear or logistic, k-NN,SVM, random forestEvery short list: compareagainst a baseline, on thesame folds (lesson 4.14)

Press Walk through it to follow the work round the chart, one stop at a time.

  1. Are there labels? If no: k-means or DBSCAN for groups, PCA for fewer features, an isolation forest for odd rows.
  2. Text, images or sound? If yes: naive Bayes or logistic regression for text; neural networks (Stages 6–12) for images and sound.
  3. Must people read the model? If yes: linear or logistic regression, or a shallow tree.
  4. Thousands of rows or more? If yes: gradient boosting, a random forest, and a linear model to compare.
  5. Otherwise: linear or logistic regression, k-NN, an SVM, a random forest.
  6. Every short list is then compared against a baseline, on the same cross-validation folds.
Choosing a short list of models, as a UML activity diagram. In UML’s notation: the filled dot is where it starts and the ringed dot where it ends; rounded boxes are actions; a diamond with a question is a decision, with the condition for each way out in [brackets]; Pink arrows go back to an earlier action.

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:

choosing.ipynb

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:

choosing.ipynb

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:

Model race342 penguins, 4 measurements
0.750.80.850.90.951logistic0.988k-NN0.982SVM0.985tree0.947forest0.977boosting0.9655-fold cross-validated accuracy (the axis starts at 0.75)

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, k-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:

choosing.ipynb

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:

choosing.ipynb

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

Model A scores 0.912 and model B 0.908 in 5-fold cross-validation, each with a standard deviation of about 0.02. A is a gradient-boosted model; B is logistic regression. Which would you pick, and why?
A colleague reports that their SVM beat your random forest by 2 points. They used their own train–test split; you used yours. What do you say?
A model’s accuracy was 92% at release. Six months later, complaints rise, but the code hasn’t changed. What’s the most likely cause, and the first check?

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.

All sources for Simple Machine Learning