The nearest neighbors
Neighbor distances and mpg
◆ The Blueprint
The dashed ring

Every point inside the ring is closer to the new car than its \(k\)th neighbor. In raw units the ring is a circle in horsepower and pounds, so a 40-hp gap and a 40-lb gap count the same. Weights span about 3,500 lbs and horsepower about 180 hp, so on these axes the circle appears as a flat ellipse. Normalized, the ring is an ellipse whose half-widths are the radius times each standard deviation.

Normalizing
$$z = \frac{x - \bar{x}}{s}$$

\(\bar{x}\) and \(s\) are the mean and standard deviation of all 391 cars, with \(s\) dividing by \(n - 1\). For horsepower, \(s\) = 38.5 hp. For weight, \(s\) = 850 lbs. The mean cancels in every gap between two cars, so only \(s\) changes the distances.

Ties

Horsepower and weight are whole numbers, so many cars are exactly the same distance from the new car. When two cars tie, the one earlier in the data comes first, as R's order() puts them. With a package that breaks ties another way, the \(k\)th neighbor and the prediction can differ slightly.

In R

order(d)[1:3] gives the rows of the three nearest cars, and mean(mpg[nn]) averages their mpg, as in Parts 1 to 3 of The Script.

Held-out RMSE by k
◆ The Blueprint
Why each car is held out

If a car could be its own neighbor, \(k = 1\) would predict most cars with their own mpg. Graded on the same 391 cars it uses, \(k = 1\) has an RMSE of 0.71 mpg, above 0 only because some cars share a horsepower and weight but differ in mpg.

Small and large k

At \(k = 1\) each prediction is one car's mpg, noise included, so the predictions vary a lot (high variance). As \(k\) grows, the average includes cars farther from the one predicted, so the predictions come closer to the overall mean (high bias). At \(k = 390\) each prediction is the mean mpg of the other 390 cars, nearly the same number for every car, and the RMSE is 7.83 mpg.

Every k at once

For each held-out car, sort the other 390 by distance once. The prediction at \(k\) is the average mpg of the first \(k\) cars in that list, so running averages give the prediction at every \(k\) from one sort.

$$\hat{y}_{(-i)}(k) = \frac{1}{k}\sum_{j=1}^{k} y_{[j]}$$

\(y_{[j]}\) is the mpg of the \(j\)th nearest car to car \(i\).

Other ways to split the cars

A common alternative first splits the cars into a training set and a test set. It then splits the training cars into 5 equal groups (folds), holds out one group at a time to choose \(k\), and grades the chosen model once on the test cars. Leave-one-out is cross-validation with 391 folds of one car each. It needs no random split, so it gives the same curve every time.

In R

Parts 4 and 5 of The Script compute both curves and their minimums. Part 6 repeats the search with knn.reg() from the FNN package.

The R calls
CallWhat it returns
order(d)[1:k]The rows of the k nearest cars. Tied cars stay in row order, and an NA distance (the held-out car) goes last.
(x - mean(x)) / sd(x)The variable in standard deviations. sd() divides by n − 1.
cumsum(y) / (1:150)The running average of the sorted neighbors' mpg: the prediction at every k from 1 to 150.
which.min(rmse)The k with the smallest RMSE.
knn.reg(train, y = mpg, k = 36)FNN's KNN regression. With no test argument it holds out each row in turn and returns the held-out predictions in $pred.
knn_in_R.R