Implementing K-Nearest Neighbors

Learn how to implement the K-Nearest Neighbors algorithm.

  1. Calculate Euclidean distance:
import math

def euclidean_distance(p1, p2):
    return math.sqrt(sum((x-y)**2 for x,y in zip(p1,p2)))
  1. Find nearest neighbors:
def get_neighbors(train, test, k):
    distances = [(euclidean_distance(test, t), t) for t in train]
    distances.sort(key=lambda x: x[0])
    return [d[1] for d in distances[:k]]

Read more: KNN Algorithm