-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_mnist.txt
53 lines (39 loc) · 1.45 KB
/
knn_mnist.txt
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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
ds = pd.read_csv('./mnist_train.csv')
print(ds.shape)
data = ds.values
print(data.shape)
y_train = data[:, 0]
X_train = data[:, 1:]
# X_train = (X_train - X_train.mean(axis=0))/(X_train.std(axis=0) + 1e-03)
print(y_train.shape, X_train.shape)
plt.figure(0)
idx = 1009
print(y_train[idx])
plt.imshow(X_train[idx].reshape((28, 28)), cmap='gray')
plt.show()
def dist(x1, x2): #function to calculate ecludian distance for x1 and x2 vector points
return np.sqrt(((x1 - x2)**2).sum())
def knn(X_train, x, y_train, k=5):
vals = [] #to store distance
for ix in range(X_train.shape[0]):
#compute distance of ix from all the points
v = [dist(x, X_train[ix, :]), y_train[ix]]
vals.append(v) #appends all distances in a list
updated_vals = sorted(vals, key=lambda x: x[0]) #sort to get the minimum distance to return the index which is closest to the testing example
pred_arr = np.asarray(updated_vals[:k])
pred_arr = np.unique(pred_arr[:, 1], return_counts=True)
pred = pred_arr[1].argmax()
# return pred_arr[0][pred]
return pred_arr, pred_arr[0][pred]
idq = int(np.random.random() * X_train.shape[0])
q = X_train[idq]
res = knn(X_train[:10001], q, y_train[:10001], k=5)
print(res)
print(y_train[idq])
plt.figure(0)
plt.imshow(q.reshape((28, 28)), cmap='gray')
plt.show()