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 by | height | rounded |
|---|---|---|
| down 1 | 5.4 | 5 |
| 0 | 6.4 | 6 |
| up 1 | 7.4 | 7 |
| across | value | distance across |
|---|---|---|
| 2 | 6 | 1 |
| 1 | 4 | 2 |
| 5 | 9 | 2 |
| 0 | 1 | 3 |
| 6 | 8 | 3 |
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.
each racer's own page
k-nearest neighbours
k is 39 of 12 within 1, 108 measured
linear regression
step size 0.05, 7 steps2 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.
| the trend | k-nearest neighbours | linear regression |
|---|---|---|
| a straight trend | 58540 measured | 60360 measured |
| a bent trend | 45540 measured | 17360 measured |