-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.py
44 lines (38 loc) · 1.13 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
from scipy.spatial import distance
def eu(a, b):
return distance.euclidean(a, b)
class KNN:
def fit(self, x_train, y_train):
self.x_train = x_train
self.y_train = y_train
def predict(self, x_test):
predictions = []
for row in x_test:
label = self.closest(row)
predictions.append(label)
return predictions
def closest(self, row):
best_dist = eu(row, self.x_train[0])
best_index = 0
for i in range (1, len(self.x_train)):
dist = eu(row, self.x_train[i])
if dist < best_dist:
best_dist = dist
best_index = i
return self.y_train[best_index]
from sklearn.datasets import load_iris
#from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
x = iris.data
y = iris.target
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
clf = KNN()
#clf2= KNeighborsClassifier()
clf.fit(x_train, y_train)
#clf2.fit(x_train,y_train)
prediction = clf.predict(x_test)
#pred=clf2.predict(x_test)
from sklearn.metrics import accuracy_score as acs
print("Accuracy by custom kNN is: "+str(acs(prediction, y_test)*100))
#print(acs(pred, y_test))