K-Nearest Neighbors


-Nearest Neighbors (NN) is one of the easiest (but effective) algorithms in machine learning used for classification and regression. When I have a new piece of data that I want to classify, I simply take that new piece of data and compare it to every piece of data in my training set. I then store  ( can be set to any number) examples from the training set most similar to the new data point, and predict the majority label from those  examples. The similarity metric is usually computed by the Euclidean distance. There is no need to train or create a model. All I need is data and to set .

Once Upon A Time

You’re a new student at a high school in Kansas. It’s a stereotypical high school with a bunch of different cliques . It has the jocks, the nerds, the cheerleaders, the rock band kids, and the theatre students. None of these are mutually exclusive, but for this story lets assume they are :). You, being a new student wanting friends, look for the clique that’s most similar to you to hang out with.

During your first week, you go around the whole school meeting each student (training data). You know what clique each student belongs to (label). When you meet each person, you compare your hobbies, wardrobe, and GPA (features). You record how similar they are to you in your journal, based off of those 3 features.

By the end of the week, you have met the entire school and decide it’s time to fully integrate into one of the cliques. You look in your journal and decide that you are going to find the 5 (you set  = 5) people that are most similar to you and are going to join the clique that the majority of the 5 belong to.

You see the top 5 people most similar to you are Ceejorn (jock), Nickenny (cheerleader), Nacolm (nerd), Dayaan (nerd), and Adam (nerd). You see that a majority of the people that are most similar to you are the nerds. You now classify yourself as a nerd. As a result, you hang out with the nerds for the remainder of your high school career.

K-Nearest Neighbors

-Nearest Neighbors, unlike all the other algorithms, doesn’t build a model. There is no training step. The main idea of it is to simply use the similarity between examples’ features. The assumption that drives this algorithm is that similar examples should have the same label, which intuitively makes sense in real life. We also assume that all examples (instances) are points in -dimensional space .

For example, if you wanted to classify whether someone was an adult or child (labels), and you were given 2 features (height and weight), you would model it with a 2 dimensional graph.

For our similarity measure, we use the Euclidean distance. The closer an example is, the more similar. Given two examples  and , we can calculate the Euclidean distance as:

with  being each feature in the examples.

Euclidean Distance between two examples.

Given a new example that we want to classify, we simply compare it to our entire training set using the euclidean distance:

for all in our training set.

We then take the  training examples’ labels  with the smallest euclidean distances, which correspond to the  nearest neighbors, and place it in the set , i.e. . Since all the labels are -1 or 1, we can simply compute:

which will return the majority label within . That is the label we will predict.

If you're still a little confused, keep reading on and it should clear up by the time you read through the example section. 


Given a training set with features and parameter , the -Nearest Neighbors algorithm works like this:


Going with our example, assume we are trying to predict which clique you will fit into the best.

The two class labels are jocks -1 or rock band kids +1.

The 2 features are  love of sports and   love of rock music, all rated on a scale of 0-9. You love sports and you’re pretty neutral about rock music  So, .

You are trying to predict which clique you will belong to .

Let’s say we set  = 3 and we’re given (training data) 10 students, 5 jocks and 5 rock band kids:

\(x_1 = (9, 0), \ y_1 = \) -1
\(x_2 = (7,3), \ y_2 = \) -1
\(x_3 = (8,3), \ y_3 =\) -1
\(x_4 = (9,2), \ y_4 = \) -1
\(x_5 = (7,1), \ y_5 = \) -1
\(x_6 = (3,9), \ y_6 = \) +1
\(x_7 = (4,8), \ y_7 = \) +1
\(x_8 = (2,7), \ y_8 = \) +1
\(x_9 = (4,7), \ y_9 = \) +1
\(x_{10} = (0,9), \ y_{10} = \) +1

We compare each one’s features to ’s features using the euclidean distance formula. For example, if we compare the 3rd person to you, we’d get:

We calculate this for the other 9 examples and return the 3 smallest numbers in the set which corresponds to the 3 closest examples. The 3 closest to  are .

To find our predicted label  we take the sum of the 3 labels corresponding to , given by:


So based off of your features, we predict that you will most likely get along with the jocks!


-Nearest Neighbors is simple to implement, works well in practice, and can be extended easily with new examples. It does not require the user to build a model, make assumptions, or tune parameters.

There is no training step. When given a new data to classify, you simply compare that new data’s features to each training example’s features and return the closest examples determined by the euclidean distance. The predicted label will be the label of the majority of the examples.

In order to choose the most optimal parameter , see cross validation.

Note that there are definitely cons when it comes to NN. For one, it requires large space to store the entire training dataset. The actual algorithm can also be extremely slow! Given  examples and  features. The method takes  to run. Last but not least, it could suffer from the curse of dimensionality if implemented by an approximate nearest neighbor search algorithm like a K-D tree, but is free if implemented using brute force.

Other observations: