-
Notifications
You must be signed in to change notification settings - Fork 1
/
KNN_MNIST.py
49 lines (29 loc) · 945 Bytes
/
KNN_MNIST.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
# coding: utf-8
# In[1]:
#coding:utf-8
import numpy as np
import time
from tensorflow.examples.tutorials.mnist import input_data
import struct
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn.neighbors import KNeighborsClassifier as knn
def main():
mnist=input_data.read_data_sets("MNIST_data",one_hot=False)
x_train = mnist.train.images
y_train = mnist.train.labels
x_test = mnist.test.images
y_test = mnist.test.labels
start_time=time.clock()
# Train the model
clf= knn(n_neighbors=5)
clf.fit(x_train,y_train)
# Test the test examples
prediction = clf.predict(x_test)
accuracy=np.sum(np.equal(prediction,y_test))/len(y_test)
print("Accuracy:",accuracy)
end_time=time.clock()
print("Time cost:",(end_time-start_time)/60,"minutes")
return accuracy,(end_time-start_time)/60
if __name__=="__main__":
main()