We are given a data set of various items with their values, and we want to be able to classify a new item given to us based on that data set (often called *training set*). A simple algorithm that does that is the *k-Nearest Neighbors* algorithm (kNN for short).

In a nutshell, it classifies an item depending on the items in the data set closer to it.

### Intro

Say we are given a data set of items, each having numerically valued features (like Height, Weight, Age, etc). If the count of features is *n*, we can represent the items as points in an *n*-dimensional grid. Given a new item, we can calculate the distance from the item to every other item in the set. We pick the *k* closest neighbors and we see where most of these neighbors are classified in. We classify the new item there.

So the problem becomes how we can calculate the distances between items. The solution to this depends on the data set. If the values are real we usually use the Euclidean distance. If the values are categorical or binary, we usually use the Hamming distance.

The algorithm in pseudocode:

Given a new item: Find distances between new item and all other items Pick k shorter distances Pick the most common class in these k items That class is where we will classify the new item

### Reading Data

Let our input file be in the following format:

Height,Weight,Age,Class 1.70,65,20,Programmer 1.90,85,33,Builder 1.78,76,31,Builder 1.73,74,24,Programmer 1.81,75,35,Builder 1.73,70,75,Scientist 1.80,71,63,Scientist 1.75,69,25,Programmer

Each item is a line and under “Class” we see where the item is classified in. The values under the feature names (“Height” etc.) is the value the item has for that feature. All the values and features are separated by commas.

You can find two example data file on my Github, here and here. Choose one and paste the contents as is into a text file named *data*.

We will read from the file (named “data.txt”) and we will split the input by lines:

f = open('data.txt', 'r'); lines = f.read().splitlines(); f.close();

The first line of the file holds the feature names, with the keyword “Class” at the end. We want to store the feature names into a list:

#Split the first line by commas, remove the first element #and save the rest into a list. #The list now holds the feature names of the data set. features = lines[0].split(',')[:-1];

Then we move onto the data set itself. We will save the items into a list, named *items*, whose elements are dictionaries (one for each item). The keys to these item-dictionaries are the feature names, plus “Class” to hold the item class. At the end, we want to shuffle the items in the list (this is a safety measure, in case the items are in a weird order).

items = []; for i in range(1, len(lines)): line = lines[i].split(','); itemFeatures = {"Class" : line[-1]}; for j in range(len(features)): #Iterate through the features f = features[j]; #The first item in the line is the class, skip it v = float(line[j]); itemFeatures[f] = v; items.append(itemFeatures); shuffle(items);

### Classifying

With the data stored into *items*, we now start building our classifier. For the classifier, we will create a new function, *Classify*. It will take as input the item we want to classify, the items list and *k*, the number of the closest neighbors.

If *k* is greater than the length of the data set, we do not go ahead with the classifying, as we cannot have more closest neighbors than the total amount of items in the data set. (alternatively we could set k as the *items* length instead of returning an error message)

if(k > len(Items)): return "k larger than list length";

We want to calculate the distance between the item to be classified and all the items in the training set, in the end keeping the *k* shortest distances. To keep the current closest neighbors we use a list, called *neighbors*. Each element in the least holds two values, one for the distance from the item to be classified and another for the class the neighbor is in. We will calculate distance via the generalized Euclidean formula (for *n* dimensions). Then, we will pick the class that appears most of the time in *neighbors* and that will be our pick. In code:

def Classify(nItem, k, Items): if(k > len(Items)): return "k larger than list length"; #Hold nearest neighbors. #First item is distance, second class neighbors = []; for item in Items: #Find Euclidean Distance distance = EuclideanDistance(nItem, item); #Update neighbors, #either adding the current item in neighbors or not. neighbors = UpdateNeighbors(neighbors, item, distance, k); #Count the number of each class in neighbors count = CalculateNeighborsClass(neighbors, k); #Find the max in count, #aka the class with the most appearances. return FindMax(count);

The external functions we need to implement are *EuclideanDistance*, *UpdateNeighbors*, *CalculateNeighborsClass* and *FindMax*.

### Euclidean Distance

def EuclideanDistance(x,y): S = 0; for key in x.keys(): S += math.pow(x[key]-y[key], 2); return math.sqrt(S);

### Update Neighbors

We have our neighbors list (which should at most have a length of *k*) and we want to add an item to the list with a given distance. First we will check if *neighbors* has a length of *k*. If it has less, we add the item to it irregardless of the distance (as we need to fill the list up to *k* before we start rejecting items). If not, we will check if the item has a shorter distance than the item with the max distance in the list. If that is true, we will replace the item with max distance with the new item.

To find the max distance item more quickly, we will keep the list sorted in ascending order. So, the last item in the list will have the max distance. We will replace it with the new item and we will sort again.

To speed this process up, we can implement an Insertion Sort where we insert new items in the list without having to sort the entire list. The code for this though is rather long and, although simple, will bog the tutorial down.

Code:

def UpdateNeighbors(neighbors, item, distance, k): if(len(neighbors) distance): #If yes, replace the last element with new item neighbors[-1] = [distance, item["Class"]]; neighbors = sorted(neighbors); return neighbors;

### Calculate Neighbors Class

Here we will calculate the class that appears most often in *neighbors*. For that, we will use another dictionary, called *count*, where the keys are the class names appearing in *neighbors*. If a key doesn’t exist, we will add it, otherwise we will increment its value.

def CalculateNeighborsClass(neighbors, k): count = {}; for i in range(k): if(neighbors[i][1] not in count): #The class at the ith index is not in the count dict. #Initialize it to 1. count[neighbors[i][1]] = 1; else: #Found another item of class c[i]. #Increment its counter. count[neighbors[i][1]] += 1; return count;

### Find Max

We will input to this function the dictionary *count* we build in *CalculateNeighborsClass* and we will return its max.

def FindMax(countList): maximum = -1; #Hold the max classification = ""; #Hold the classification for key in countList.keys(): if(countList[key] > maximum): maximum = countList[key]; classification = key; return classification, maximum;

### Conclusion

With that this kNN tutorial is finished.

You can now classify new items, setting *k* as you see fit. Usually for *k* an odd number is used, but that is not necessary. To classify a new item, you need to create a dictionary with keys the feature names and the values that characterize the item. An example of classification:

newItem = {'Height' : 1.74, 'Weight' : 67, 'Age' : 22}; print Classify(newItem, 3, items);

You can find the complete code on my GitHub (the code includes a Fold Validation function, but it is unrelated to the algorithm, it is there for calculating the accuracy of the algorithm).