-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_learner.py
46 lines (37 loc) · 1.24 KB
/
knn_learner.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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import pandas
import numpy
def knn_learner(x_train, y_train, x_test, y_test):
"""
:param x_train:
:param y_train:
:param x_test:
:param y_test:
"""
accuracy_values = []
for i in range(1, 100):
knn = KNeighborsClassifier(n_neighbors=i)
knn.fit(x_train, y_train)
# Calculate the accuracy of the model
if i % 2 == 0:
print(
"Iteration K =",
i,
"Accuracy Rate=",
knn.score(x_test, y_test),
)
print(knn.score(x_test, y_test))
accuracy_values.append(
[i, knn.score(x_test, y_test)]
)
k_accuracy_pair = pandas.DataFrame(accuracy_values)
k_accuracy_pair.columns = ["K", "Accuracy"]
# Let's see the K value where the accuracy was best:
k_accuracy_pair[
k_accuracy_pair["Accuracy"]
== max(k_accuracy_pair["Accuracy"])
]
# Best iteration was K = 41 and K = 47 and K = 51, all three with Accuracy = 89.3%.
# This is actually slightly better than the neural network's accuracy.
# The neural network's accuracy was 87.23%.