AI Development · Level 1 · Beginner

Which Algorithm Wins? Six Classic Methods Compared

Six classic methods on the same data, from logistic regression to a neural network, compared fairly with cross-validation, plus what each one costs to run.

By 6 min read
Six classic methods on the same data, from logistic regression to a neural network, compared fairly with cross-validation, plus what each one costs to run.

Machine learning offers dozens of methods for the same task, and every one of them has fans. Which one is best? The honest answer is: it depends on the data, and you only find out by testing. Testing fairly, however, is harder than it looks, because a single test can make a method look better or worse than it is by chance.

This is part 4 of Level 1 in our learning path AI Fundamentals. We ran six classic methods on the same handwritten digits and compared them with cross-validation, the standard way to test fairly. You will learn what each method does in one sentence, how cross-validation works, which method won on our data and why that does not make it the best method in general.

Six methods in one sentence each

Which Algorithm Wins? Six Classic Methods Compared
MethodHow it decidesStrengthWeakness
Logistic regressionWeighs every pixel and adds up the evidence for each digitFast, easy to inspectOnly straight boundaries between categories
k-nearest neighborsLooks up the most similar training images and lets them voteNo training at all, strong when similar means sameSlow with large data, needs a good similarity measure
Decision treeAsks a series of yes-or-no questions about pixelsEasy to explainOverfits quickly
Random forestLets hundreds of slightly different trees voteRobust, little tuning neededHarder to explain than one tree
Support vector machineDraws the widest possible border between categoriesStrong on medium-sized dataSlow on large data, many settings
Neural networkLearns its own features in a hidden layerMost flexibleNeeds more data and tuning

Cross-validation: five tests instead of one

A single split into training and test data is a bit like a single exam: the result depends partly on which questions happened to come up. Cross-validation repeats the test. In five-fold cross-validation, the data is split into five equal parts. Each method is trained five times, each time on four parts, and tested on the fifth, so that every image is used for testing exactly once. The result is an average and a range, which shows how stable a method is.

Five rows, each showing the data split into five parts; in each row a different part is the test set and the other four are used for training
Five-fold cross-validation: every part of the data serves once as the test set.

The result on our data

Dot chart: average accuracy of six methods in five-fold cross-validation, with a line from the worst to the best fold; k-nearest neighbors highest, decision tree far behind
Average accuracy across five folds, with the range from the worst to the best fold.

On the handwritten digits, the winner was k-nearest neighbors, with 98.9% on average, ahead of the support vector machine, the random forest and the neural network, which were close together. Logistic regression reached 97%. The single decision tree came last with 85.8%, and its results varied most between the folds, with a standard deviation of 1.6 percentage points.

Six methods, five-fold cross-validation on all images
MethodMeanWorst foldBest fold
k-nearest neighbors (k = 3)98.9%98.3%99.4%
Support vector machine98.4%97.5%99.2%
Random forest (200 trees)97.9%97.2%98.6%
Neural network (64 neurons)97.8%97.2%98.3%
Logistic regression97.0%96.1%98.1%
Decision tree85.8%83.8%87.5%

Our own experiment: dataset Optical Recognition of Handwritten Digits (E. Alpaydin, C. Kaynak, UCI Machine Learning Repository, license CC BY 4.0), 1797 images of 8 × 8 pixels, 540 of them held back for testing; scikit-learn 1.9.1, fixed random seed, so every run gives the same numbers.

Why does the simplest method win here? Because on these small, centered images, two handwritten 3s really do look alike pixel by pixel, so “find the most similar known example” works very well. On data where similarity is not so obvious, such as customer records with dozens of columns in different units, k-nearest neighbors usually falls behind, and tree ensembles or gradient boosting tend to lead.

Accuracy is not the whole bill

Two methods with almost the same accuracy can differ by orders of magnitude in what they cost to run. For each trained model, we counted what it has to keep in memory and how much work it does, at most, for every new image.

What each trained model stores and the most work it does per new image
MethodKeeps after trainingSizeWork per new image (at most)
Logistic regressionWeights650640 multiplications
k-nearest neighbors (k = 3)All training images1,257 images, 80,448 pixel valuesCompare with 1,257 images (80,448 pixel values)
Decision treeQuestions and leaves241 nodes11 questions
Random forest (200 trees)Questions and leaves of all trees61,848 nodes2,653 questions
Support vector machineSupport vectors: training images near the borders686 images, 43,904 pixel valuesCompare with 686 images (43,904 pixel values)
Neural network (64 neurons)Weights and biases4,8104,736 multiplications

Source as for the first table on this page.

The winner on accuracy is the most expensive to run. k-nearest neighbors keeps all 1,257 training images, 80,448 pixel values, and compares every new image with each of them. Logistic regression gets by with 650 weights, about 124 times fewer numbers, and reached 97% in cross-validation instead of 98.9%. The support vector machine keeps only the training images close to the borders between digits, 686 of them. The neural network stores 4,810 weights, and the random forest 61,848 questions and leaves across its 200 trees.

With small images like ours, all of this fits easily on any phone. With millions of large images or documents, the difference decides whether a method can run at all. That is why k-nearest neighbors in production relies on special search indexes that find similar items without comparing against every single one, the same technology that vector databases are built on.

There is no best method

Machine learning research has a name for this observation: the no free lunch principle. No method is best for every kind of data. That is why practitioners follow a few habits:

  1. Start with a simple baseline, such as logistic regression. Every more complex method has to beat it clearly.
  2. Compare with cross-validation, not with one lucky split, and look at the range as well as the average.
  3. Count more than accuracy. Speed, memory, the cost of training and whether people can follow the decisions matter in production.
  4. Prefer the simpler model when results are close. A difference of a few tenths of a percent is often within the range of chance.
  5. Test on your own data. Benchmarks from other people’s data, including ours, only tell you what to try first.

How we ran the experiment

We used the Python library scikit-learn with mostly standard settings: k-nearest neighbors with k = 3, a random forest with 200 trees, a support vector machine with its standard kernel on standardized pixel values and a neural network with one hidden layer of 64 neurons. The five folds are stratified, so every fold contains about the same share of each digit, and the split uses a fixed seed, so the numbers on this page come out the same on every run. The counts in the cost table come from the models trained on the 1,257 training images. The last part of Level 1 turns to data without any labels: finding groups with clustering and simplifying data with PCA.