-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2P3.py
32 lines (22 loc) · 853 Bytes
/
A2P3.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
import numpy as np
class UnfittedError(Exception):
pass
class KNN_classifier:
def __init__(self, n_neighbors: int, **kwargs):
self.K = n_neighbors
self.params = None
self.markers = None
def fit(self, x: np.array, y: np.array):
self.params = x
self.markers = y
def predict(self, x: np.array):
predictions = []
if self.params is None:
return 'UnfittedError'
for point in x:
point_dist = np.linalg.norm(self.params - point, axis=1)
neighbors = self.markers[np.argsort(point_dist)]
cls, count = np.unique(neighbors[:self.K], return_counts=True, axis=0)
cls_count = dict(zip(cls, count))
predictions += [max(cls_count, key=cls_count.get)]
return predictions