k-Nearest Neighbors (kNN)

Make fake data

Plot data

kNN classifier

kNN regressor

Weighted kNN classifier

Recall that with unweighted kNN classifier, above, we saw "For k=3, predict green is 1."

Now we try weighted kNN.

Inspect the distances:

With unweighted 3-NN, we get 1 (blue). With weighted 3-NN, we get 0 (red) because the red's weight is greater than the sum of the two blue weights.

Here is a more natural k-NN regression example.