Created
August 27, 2017 14:16
-
-
Save victor-iyi/26c9e059fd9ea5ff9be009edb7449003 to your computer and use it in GitHub Desktop.
A Naïve implementation of the K-Nearest neighbor's algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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