Skip to content

predict a number

Each point has a place across and a value, drawn as its height. Learn from training points whose values you know, then predict the values of more points that follow the same shape, the test points. Linear regression draws one straight line; k-nearest neighbours (here k is 3) averages the values of the 3 training points nearest across. Most test points predicted within 1 wins.

A prediction is right when it is within 1 of the test point's true value: for a true 6, 5.4 is right and 4.8 is wrong. A trend is the shape the values follow, straight or bent. Each point's value is the trend's height there, moved up or down at random by a whole number, at most the noise, which you set to 0, 1 or 2. Then it is rounded and kept between 0 and 10.

Linear regression starts with a flat line at height 5, halfway up. A miss is the gap from the line up or down to a training point. The line's loss is the mean of the squared misses. It takes 7 steps, each tilting the line and lifting or lowering it so the loss falls; the step size, 0.05, sets how far one step goes. It predicts a point's value as the line's height there.

k-nearest neighbours finds the 3 training points nearest across; if two are as near for third place, it takes the one listed first among the training points. It predicts the mean of their values.

When the racers tie on right answers, the one that made fewer measurements wins: distances for k-nearest neighbours, misses for linear regression. The start, where the race begins, is a bent trend with no noise.

moved byheightrounded
down 15.45
06.46
up 17.47
Noise 1 where the trend is at 6.4: moved by a whole number, at most 1, then rounded, the value is 5, 6 or 7. At noise 0 every value is the trend's, rounded.
02468100246810111443142
The start's 9 training points and linear regression's first line, flat at 5. Each gap is a miss, from left to right 4, 1, 1, 4, 3, 2, 1, 1 and 4. Their squares add to 65, so the loss is 65 divided by 9, about 7.22.
acrossvaluedistance across
261
142
592
013
683
On the start a test point stands at 3 across, its true value 7. These are the training points nearest it, nearest first, and left to right when equally near. The top 3 have values 6, 4 and 9, so k-nearest neighbours predicts 19 divided by 3, about 6.33: within 1 of 7, so right.

the trend

how much noise

Each racer learns from 9 training points, then predicts the values of 12 test points.

Without scripts the keys cannot redraw, so the race shows a bent trend at noise 0.

which suits these points

On a bent trend at noise 0, k-nearest neighbours predicted the most test points within 1: 9 of 12, against 2 for linear regression.

k-nearest neighbours9linear regression2
Each row is one racer, each column one test point: a dot is filled if that racer got it right, hollow if wrong.

each racer's own page

k-nearest neighbours

k is 3
02468100246810

9 of 12 within 1, 108 measured

linear regression

step size 0.05, 7 steps
02468100246810

2 of 12 within 1, 72 measured

  • a training point, which the racer learns from
  • a test point, drawn at its true value
  • a test point the racer predicted more than 1 away
  • the racer's predictions: linear regression's line, or k-nearest neighbours' value at each whole number across (0, 1, 2 and so on)

On the start, k-nearest neighbours measures 9 distances for each of the 12 test points: 9 times 12 is 108. Linear regression measures all 9 misses before its first step and again after each of its 7 steps: 8 times 9 is 72.

every trend at noise 0, over five draws

A draw is a fresh set of points on the same trend. Each cell: how many of 60 test points, 12 in each of five draws, were predicted within 1; under it, what the racer measured. The most right on each row is in bold; where several tie, the ones that measured least.

Which racer wins depends on the trend and, on a straight trend, on the noise too. On a bent trend, no straight line follows the bend: on the start linear regression gets 2 of 12 right and k-nearest neighbours 9. Over five draws, k-nearest neighbours wins the bent trend at every noise. On the straight trend, linear regression wins at noise 0 and 1, and k-nearest neighbours at noise 2, but only by 1 test point of 60.

024681002468104.36
55.200.050.1the line's heightthe line's slope
024601234567steps takenthe lossthe loss
The start's 9 training points and linear regression's line after 7 steps at step size 0.05: height 5.06 at 5.33 across, the points' middle, and slope 0.06. That is close to the best straight line for these points, which is nearly level, as their values climb from 1 to 9 and fall back to 1. The number by the line is its longest miss, 4.36, at 10 across.
the trendk-nearest neighbourslinear regression
a straight trend0246810024681058540 measured60360 measured
a bent trend0246810024681045540 measured17360 measured