-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathknn_demo.py
34 lines (26 loc) · 1.08 KB
/
knn_demo.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
import numpy as np
from memory_profiler import profile
class KNNClassifier:
def __init__(self, k=3):
self.k = k
def fit(self, X, y):
self.X_train = X
self.y_train = y
def euclidean_distance(self, x1, x2):
diff = (x1 - x2)
sqr_diff = diff ** 2
sqr_diff_sum = np.sum(sqr_diff)
return np.sqrt(sqr_diff_sum)
def predict(self, X):
y_pred = [self._predict(x) for x in X]
return np.array(y_pred)
def _predict(self, x):
# Calculate distances from the input point to all training points
distances = [self.euclidean_distance(x, x_train) for x_train in self.X_train]
# Sort by distance and return indices of the first k neighbors
k_indices = np.argsort(distances)[:self.k]
# Extract the labels of the k nearest neighbor training samples
k_nearest_labels = [self.y_train[i] for i in k_indices]
# Return the most common class label among the k nearest neighbors
most_common = np.bincount(k_nearest_labels).argmax()
return most_common