-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN from scratch.py
112 lines (61 loc) · 2.31 KB
/
KNN from scratch.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# coding: utf-8
# Because no work is required until a prediction is done, KNN is often referred to as lazy learning method
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
from mlxtend.data import loadlocal_mnist
from tqdm import tqdm as tqdm
# In[2]:
def euclidean_measure(image1, image2):
return np.sqrt(np.sum(np.subtract(image1,image2) ** 2))
# In[55]:
def predict(train_data, test_image, train_labels, k):
distances = []
for j in range(train_data.shape[0]):
distances.append((euclidean_measure(train_data[0], test_image),j))
distances.sort(key = lambda x : x[0])
# return distances
return predict_label(distances[:k],train_labels)
def predict_label(distances, train_labels):
labels = list(train_labels[[item[1] for item in distances]])
prediction = max(set(labels), key = labels.count)
return prediction
def plot_curve(k, accuracies):
plt.plot(k, accuracies)
# In[56]:
train_data, train_labels = loadlocal_mnist(images_path = 'F:\\Masters at ASU\\Semester 1\\SML\\train-images.idx3-ubyte',
labels_path = 'F:\\Masters at ASU\\Semester 1\\SML\\train-labels.idx1-ubyte')
test_data, test_labels = loadlocal_mnist(images_path = 'F:\\Masters at ASU\\Semester 1\\SML\\t10k-images.idx3-ubyte',
labels_path = 'F:\\Masters at ASU\\Semester 1\\SML\\t10k-labels.idx1-ubyte')
# In[57]:
test_data[0].shape
# In[58]:
K = [1, 3, 5, 10, 20, 30, 40, 50, 60]
accuracies = []
# print(predict(train_data, test_data[0], train_labels, k))
"""
print(t[:10])
d = train_labels[[item[1] for item in t]]
print(d.shape)
"""
for k in range(0,len(K)):
count = 0
for i in tqdm(range(test_data.shape[0])):
predicted_label = predict(train_data, test_data[i], train_labels, K[k])
actual_label = test_labels[i]
if(predicted_label == actual_label):
count = count+1
print("Accuracy : {0}".format((count / test_data.shape[0]) * 100.0))
accuracies.append((count / test_data.shape[0]) * 100.0)
# In[ ]:
plot_curve(k, accuracies)
# In[ ]:
a = [1,2,3]
b = [4,5,6]
print(np.sqrt(np.sum(np.subtract(a,b) ** 2)))
# In[ ]:
int(test_data.shape[0])
# In[48]:
np.zeros((2,2))
# In[ ]: