-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKNeighborsClassifier.py
69 lines (52 loc) · 1.86 KB
/
KNeighborsClassifier.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def most_common(lst):
return max(set(lst), key=lst.count)
def euclidean(point, data):
# Euclidean distance between points a & data
return np.sqrt(np.sum((point - data)**2, axis=1))
class KNeighborsClassifier:
def __init__(self, k=5, dist_metric=euclidean):
self.k = k
self.dist_metric = dist_metric
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
neighbors = []
for x in X_test:
distances = self.dist_metric(x, self.X_train)
y_sorted = [y for _, y in sorted(zip(distances, self.y_train))]
neighbors.append(y_sorted[:self.k])
return list(map(most_common, neighbors))
def evaluate(self, X_test, y_test):
y_pred = self.predict(X_test)
accuracy = sum(y_pred == y_test) / len(y_test)
return accuracy
# Unpack the iris dataset, from UCI Machine Learning Repository
iris = datasets.load_iris()
X = iris['data']
y = iris['target']
# Split data into train & test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Preprocess data
ss = StandardScaler().fit(X_train)
X_train, X_test = ss.transform(X_train), ss.transform(X_test)
# Test knn model across varying ks
accuracies = []
ks = range(1, 30)
for k in ks:
knn = KNeighborsClassifier(k=k)
knn.fit(X_train, y_train)
accuracy = knn.evaluate(X_test, y_test)
accuracies.append(accuracy)
# Visualize accuracy vs. k
fig, ax = plt.subplots()
ax.plot(ks, accuracies)
ax.set(xlabel="k",
ylabel="Accuracy",
title="Performance of knn")
plt.show()