k-Nearest Neighbors (KNN) Model

What is a k-nearest neighbors model?

k-nearest neighbors (KNN) is a predictive modeling and machine learning technique that predicts a response for a given observation using the responses of nearby observations. This approach works for both categorical and continuous responses.

How do you build a KNN model?

Let’s look at a simple example to illustrate how KNN models work. Suppose you have the following data, found in the table below. There is one input, X, and one output, Y.

X Y
knn-pic1.png 10 14
11 15
12 16
30 34
31 35
32 36

Let’s start with the model for k = 1. We will go through the observations, one at a time, determine the nearest neighbor, then use that observation as the prediction.

knn-1.gif

When k = 1, the nearest neighbor for each point is found using a scaled Euclidean distance. The predicted value for each observation is the value of the nearest neighbor. In the event of a tie, one of the nearest neighbors is chosen at random.

knn-2.gif

When k = 2, the two closest neighbors to an observation are used to create the prediction. For a continuous response, the prediction is the average of the nearest neighbors. For a categorical response, the prediction is the most frequent level for all neighbors. This approach can be generalized to larger values of k.

To determine the best value of k, evaluate models from k = 1 up to a user-specified value, and choose the value that performs best on a validation set.

What are the advantages and disadvantages of KNN models?

Advantages

Disadvantages

Example of a KNN model

Let’s fit a KNN model on the Recovery data we introduced in our overview of predictive modeling. We’ll look at the continuous response Percent Recovered.

knn-pic2.png

The picture above shows the model performance as a function of k for k = 1 to 10. The Y axis is the square root of the average squared error (RASE), and smaller is better. The minimum value of RASE on the validation set occurs at k = 8. You compare models (for example, different values of k) using the validation set to help avoid overfitting and then choose the value of k that works best on new data.

knn-pic3.png

The plot of actual values by predicted values for the KNN model with k = 8 shows slight model bias on both the training and validation sets. On average, high values are being predicted lower, and lower values are being predicted higher than they actually are.