How to Choose a Machine Learning Algorithm
"Which machine learning algorithm is best?" has a short honest answer: none of them, in general. An algorithm that does well on one problem can do badly on another. In machine learning this is sometimes called the "no free lunch" idea: averaged over every possible problem, no method beats all the others, so you have to find out what works on your problem.
That does not mean the choice is random. A few questions about your task narrow the field to a sensible shortlist, and then a fair comparison picks the winner. This guide walks through both steps.
The four questions
The ML Algorithm Selector asks four questions. They are a good habit even without the tool.
1. What kind of task is it?
- Classification: the answer is a category. Spam or not spam; apple, orange or pear.
- Regression: the answer is a number. Tomorrow's temperature; the points a player will score.
- Clustering: there is no answer column; you want to find natural groups in the data.
- Dimensionality reduction: you have many features and want to squeeze them into a few, often to draw the data on a chart or to speed up another model.
2. Is the data labeled?
A label is the known answer for each example. Classification and regression need labels to learn from (this is supervised learning). If you have no labels, you cannot train a classifier, so the problem becomes unsupervised. The guide to supervised, unsupervised and reinforcement learning explains the difference.
3. How much data is there?
The selector offers two choices: small (up to a few thousand rows) or large (tens of thousands of rows or more). Flexible models with many parameters usually need more data before they beat simple ones.
4. Do you need to explain the model?
Sometimes you must be able to say why the model gave an answer: to a teacher, a customer, or yourself while you check it is fair. Some models are easy to read (a short decision tree, a line with a weight per feature). Others are harder to read but can be more accurate on complex data.
The selector's full decision table
Here is every rule the selector uses. The first algorithm in each row is the one it suggests starting with.
| Your answers | Shortlist |
|---|---|
| Clustering (any answers), or classification with unlabeled data | K-Means, DBSCAN, Hierarchical Clustering |
| Dimensionality reduction (any answers) | PCA, t-SNE, UMAP |
| Regression, need to explain it | Linear Regression, Decision Tree Regressor, Random Forest Regressor |
| Regression, accuracy matters most | Gradient Boosting, Random Forest Regressor, Neural Network |
| Labeled classification, need to explain it | Decision Tree, Logistic Regression, Naive Bayes |
| Labeled classification, accuracy matters most, large data | Gradient Boosting, Random Forest, Neural Network |
| Labeled classification, accuracy matters most, small data | Random Forest, Logistic Regression, k-NN |
A few things worth knowing about how the table behaves:
- Unlabeled "classification" becomes clustering. Without answers to learn from, the honest option is to group similar examples and then look at the groups yourself.
- For regression, the label and size answers are ignored. Only the explain-it question changes the result. Regression always needs labels (known numbers to learn from), so if you have none, you do not yet have a regression problem.
- Clustering ignores the label answer. If you do have labels, consider whether the task is really classification.
- t-SNE and UMAP are mainly for drawing pictures of data. They are good at showing groups on a chart, but PCA is the usual choice when you want fewer features to feed into another model.
Every result from the selector ends the same way: these are starting points, always train a few and compare them on your own data before deciding. That is the right attitude, and the rest of this guide is about how to do it.
What the four questions leave out
A shortlist built from four answers cannot know everything about your project. Before you commit, think about:
- What kind of features you have. Tables of numbers, text, images and sound suit different methods. Raw images usually call for neural networks; a table of measurements is often handled well by trees or logistic regression.
- Missing and messy data. Some methods cope with gaps or mixed types more easily than others, and all of them need cleaning first.
- How much computing you have. A neural network may need far more time and memory than a random forest for a similar result on a table.
- How fast predictions must be, and how often the model will be retrained.
- Who it could be unfair to. Any model can learn unfair patterns from its data, readable or not.
A correction about bias and variance
Some pages claim that "small training data has high variance and bias, while large training sets have low variance and bias". That mixes things up. Bias and variance describe how a model goes wrong, not the data itself:
- High bias means the model is too simple for the pattern (a straight line through data that curves). It is wrong in the same way however much data you give it. More data does not fix it; a more flexible model might.
- High variance means the model is so flexible that it changes a lot with the exact training examples it saw, which shows up as overfitting. More (varied) data usually helps, and so does a simpler model or stopping training earlier.
So more data mainly helps with variance, not bias. The guide to overfitting and underfitting shows what each looks like in practice.
Comparing candidates fairly
- Start with a baseline. For classification, a "lazy" model that always guesses the most common answer. For regression, one that always guesses the average. Any real model has to beat this, or it has learned nothing useful.
- Pick two or three candidates from the shortlist, including at least one simple one.
- Train them all on the same training set and score them all on the same validation set, with the same measure. Comparing models scored on different data tells you nothing.
- Choose the right measure. Accuracy alone can mislead when one answer is rare; precision and recall show what kind of mistakes a model makes. See accuracy, precision and recall.
- Prefer the simpler model when scores are close. It is easier to explain, check and run.
- Test the winner once on the test set at the very end, and report that number honestly.
Done this way, "which algorithm is best?" turns into a question you can actually answer: which of these few scored highest on my validation data, and did it beat the lazy baseline?