-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkNN.py
49 lines (38 loc) · 1.4 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
from sklearn.base import BaseEstimator
def get_eucliedean_dist(x, y):
return np.sqrt(np.sum(y**2, axis=1).reshape(y.shape[0], 1) \
+ np.sum(x**2, axis=1) - 2 * y.dot(x.T))
class kNN(BaseEstimator):
"""
Implementation of k-nearest neighbor algorithm.
Args:
k Number of neighbors to be considered for prediction.
Default: 5
classifier Boolean variable to decide if it's a classifier or regressor.
If false, the predict function will return mean instead of
majority class.
Default: True
"""
def __init__(self, k=5, classifier=True):
self.k = k
self.classifier = classifier
def fit(self, X, y):
assert(self.k < X.shape[0])
self.X = X
self.y = y
def predict(self, x):
dists = get_eucliedean_dist(self.X, x)
# get index of first k sorted predictions
idxs = np.argsort(dists)[:, :self.k]
preds = list()
# use these idx to find labels
for i, idx in enumerate(idxs):
labels = self.y[idx]
if self.classifier:
cnts = np.bincount(labels.reshape(labels.shape[0]))
preds.append(np.argmax(cnts))
else:
mean = np.mean(labels.reshape(labels.shape[0]))
preds.append(mean)
return preds