-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkNN.py
30 lines (23 loc) · 774 Bytes
/
kNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
from collections import Counter
def eu_dis(x1, x2):
'''
function to find euclidean distance
'''
distance = np.sqrt(np.sum(x1-x2)**2)
return distance
class kNN:
def __init__(self, k=3):
self.k = k
def fit(self, X, y):
self.X_train = X
self.y_train = y
def predict(self, X):
predictions = [self._predict(x) for x in X]
return predictions
def _predict(self, x):
distances = [eu_dis(x, x_tr) for x_tr in self.X_train]
k_indices = np.argsort(distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in k_indices]
most_common, _ = Counter(k_nearest_labels).most_common()[0]
return int(most_common)