k-Nearest Neighbors

kNN classification algorithm


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
from numpy import *
import operator

In [ ]:
def create_data_set():
    group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
    labels = ['A', 'A', 'B', 'B']
    return group, labels

In [ ]:
def classify(in_x, data_set, labels, k):
    data_set_size = data_set.shape[0]
    diff_mat = tile(in_x, (data_set_size, 1)) - data_set
    sq_diff_mat = diff_mat ** 2
    sq_distances = sq_diff_mat.sum(axis=1)
    distances = sq_distances ** 0.5
    sorted_dist_indices = distances.argsort()
    class_count = {}
    for i in range(k):
        vote_ilabel = labels[sorted_dist_indices[i]]
        class_count[vote_ilabel] = class_count.get(vote_ilabel, 0) + 1
    sorted_class_count = sorted(iter(class_count.items()),
                                key=operator.itemgetter(1),
                                reverse=True)
    return sorted_class_count[0][0]

In [ ]:
group, labels = create_data_set()
print(group, labels)

In [ ]:
classify([0.0,0.0], group, labels, 3)