-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNIST_KNN_Classifier.py
170 lines (149 loc) · 5.61 KB
/
MNIST_KNN_Classifier.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
import mnist_loader_KNN as DS_loader
import matplotlib.pyplot as plt
import random
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
from collections import defaultdict
import seaborn as sns
from struct import unpack
#!gzip -d data/*.gz
# URLs for the train image and label data
url_train_image = 'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz'
url_train_labels = 'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz'
num_train_samples = 60000
print("Downloading train data")
x_train = DS_loader.try_download_x(url_train_image, url_train_labels, num_train_samples)
y_train= DS_loader.try_download_y(url_train_image, url_train_labels, num_train_samples)
# URLs for the test image and label data
url_test_image = 'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz'
url_test_labels = 'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz'
num_test_samples = 10000
print("Downloading test data")
temp=random.randrange(9999)
x_test = DS_loader.try_download_x(url_test_image, url_test_labels, num_test_samples)
y_test = DS_loader.try_download_y(url_test_image, url_test_labels, num_test_samples)
print(y_test.shape)
sample=x_test[temp,:].reshape(28,28)
# plt.imshow(sample,cmap=plt.cm.gray)
# plt.show()
# print("Image Label: ", y_test[temp,])
#
# subset of train data
# x_temp=x_train[:5001,:]
# y_temp=y_train[:5001,:]
# plt.figure(figsize=(20,4))
# for index,(image,label) in enumerate(zip(x_train[0:5,:],y_train[0:5,])):
# plt.subplot(1,5,index+1)
# plt.imshow(np.reshape(image,(28,28)),cmap=plt.cm.gray)
# plt.title('Training: %i\n'%label,fontsize=20)
#plt.show()
# instantiating classifier
# knnclassifier=KNeighborsClassifier(n_neighbors=5,metric='euclidean',algorithm='ball_tree')
#
# print((x_train.shape))
#
# print((test.shape))
#
# knnclassifier.fit(x_train,y_train.ravel())
#
# image_indices = []
# for i in range(0,10):
# distances, indices = knnclassifier.kneighbors(x_test[i].reshape(1,-1))
# for test_data in indices:
# for index in test_data:
# image_indices.append(index)
#
# # plt.figure(figsize=(20,40))
# img_plot_index = 0
# for img_index in image_indices:
# img = x_train[img_index,:].reshape(28,28)
# img_plot_index = img_plot_index+1
# plt.subplot(10, 5, img_plot_index)
# plt.imshow(img,cmap=plt.cm.gray)
# #plt.title('Training: %i\n'%label,fontsize=20)
# plt.show()
#
index_error=1;
error_score=[]
for i in range(1,10001):
knnclassifier=KNeighborsClassifier(n_neighbors=i,metrics='minkowski',p=1,algorithm='ball_tree')
knnclassifier.fit(x_train,y_train.ravel())
# knnclassifier.predict(x_test[0].reshape(1,-1))
#predictions = knnclassifier.predict(x_test[0:10].reshape())
predictions= knnclassifier.predict(x_test)
score=knnclassifier.score(x_test,y_test)
print(score)
print(predictions)
error_score[index_error]=metrics.mean_squared_error(y_test,predictions, multioutput='raw_values')
print(error_score[index_error])
i=i+200
index_error+=1
#confusion matrix
# cm = metrics.confusion_matrix(y_test, predictions)
# #using the saborn plot
# plt.figure(figsize=(9,9))
# sns.heatmap(cm, annot=True, fmt=".3f", linewidths=.5, square = True, cmap = 'Blues_r')
# plt.ylabel('Actual label')
# plt.xlabel('Predicted label')
# all_sample_title = 'Accuracy Score: {0}'.format(score)
# plt.title(all_sample_title, size = 15)
# plt.show()
#
# def euclidean_distance(img_a, img_b):
# '''Finds the distance between 2 images: img_a, img_b'''
# # element-wise computations are automatically handled by numpy
# return sum((img_a - img_b) ** 2)
#
#
# def find_majority(labels):
# '''Finds the majority class/label out of the given labels'''
# # defaultdict(type) is to automatically add new keys without throwing error.
# counter = defaultdict(int)
# for label in labels:
# counter[label] += 1
#
# # Finding the majority class.
# majority_count = max(counter.values())
# for key, value in counter.items():
# if value == majority_count:
# return key
#
#
#
#
# # Setting up dataset as numpy array for faster mathematical operations.
# # Only 5000 for faster prediction - but this results into little inaccurate results.
# # Try to use all train data for accurate results.
# train_images = x_train
# train_labels = y_train
# test_images = x_test
# test_labels = y_test
#
# def predict(k, train_images, train_labels, test_images):
# '''
# Predicts the new data-point's category/label by
# looking at all other training labels
# '''
# # distances contains tuples of (distance, label)
# distances = [(euclidean_distance(test_image, image), label)
# for (image, label) in zip(train_images, train_labels)]
# # sort the distances list by distances
# by_distances = sorted(distances, key=lambda distance : distance)
# # extract only k closest labels
# k_labels = [label for (_, label) in by_distances[:k]]
# # return the majority voted label
# return find_majority(k_labels)
#
# # Predicting and printing the accuracy
# ii = 0
# total_correct = 0
# for test_image in test_images:
# pred = predict(5, train_images, train_labels, test_image)
# if pred == test_labels[ii]:
# total_correct += 1
# acc = (total_correct / (ii+1)) * 100
# print('test image['+str(ii)+']', '\tpred:', pred, '\torig:', test_labels[ii], '\tacc:', str(round(acc, 2))+'%')
# ii += 1