forked from efratoio/MLClass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNN_2.py
More file actions
47 lines (28 loc) · 1.16 KB
/
Copy pathKNN_2.py
File metadata and controls
47 lines (28 loc) · 1.16 KB
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
from sklearn import datasets
import numpy as np
from collections import Counter
from sklearn.model_selection import train_test_split
def KNN_predict(X_train, y_train, X_test, k):
# create list for distances and targets
distances = []
targets = []
for i in range(len(X_train)):
# first we compute the euclidean distance
distance = np.sqrt(np.sum(np.square(X_test - X_train[i, :])))
# add it to list of distances
distances.append([distance, i])
# sort the list
distances = sorted(distances)
# make a list of the k neighbors' targets
for i in range(k):
index = distances[i][1]
targets.append(y_train[index])
# return most common target
return Counter(targets).most_common(1)[0][0]
def estimate_accuracy(X_train,y_train,X_test,y_test,k):
prediction_results = np.array([y_test[i]==KNN_predict(X_train,y_train,X_test[i,:],k) for i in range(X_test.shape[0])])
return prediction_results.sum()/prediction_results.size
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.33, random_state=42)
acc = estimate_accuracy(X_train,y_train,X_test,y_test,3)
print("The accuracy is %f for k=3"%acc)