Skip to content

Instantly share code, notes, and snippets.

@victor-iyi
Created August 27, 2017 14:16
Show Gist options
  • Select an option

  • Save victor-iyi/26c9e059fd9ea5ff9be009edb7449003 to your computer and use it in GitHub Desktop.

Select an option

Save victor-iyi/26c9e059fd9ea5ff9be009edb7449003 to your computer and use it in GitHub Desktop.
A Naïve implementation of the K-Nearest neighbor's algorithm
from scipy.spatial.distance import euclidean
from collections import Counter
class KNeighborsClassifier:
def __init__(self, k=5):
self.k = k
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
predictions = [self.nearest(feature) for feature in X_test]
return predictions
def nearest(self, feature):
distances = [(euclidean(feature,f), self.y_train[i]) for i,f in enumerate(self.X_train)]
labels = [l[1] for l in sorted(distances)[:self.k]]
return Counter(labels).most_common(1)[0][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment