-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap2.py
58 lines (50 loc) · 1.78 KB
/
chap2.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
# coding: utf-8
# ## k−N Nfor MNIST
import numpy
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from sklearn.metrics import f1_score
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
X, y = shuffle(mnist.data, mnist.target)
X = X / 255.0
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2)
train_X, dev_X , train_y, dev_y = train_test_split(train_X, train_y, test_size=0.2)
# len: X=70000, train=44800, dev=11200, test=14000
# normalization
norm = numpy.linalg.norm(train_X, ord=2, axis=1)
normalized_train_X = train_X / norm[:,numpy.newaxis]
norm = numpy.linalg.norm(dev_X, ord=2, axis=1)
normalized_dev_X = dev_X / norm[:,numpy.newaxis]
# homework
# ### prediction for test data by k-NN
from collections import defaultdict, Counter
import heapq
ks = defaultdict(lambda: 0)
count = 0
for i in range(len(normalized_dev_X)):
score = []
count += 1
if (count % 100 == 0): print "count:" + str(count)
for j in normalized_train_X:
score.append(numpy.dot(normalized_dev_X[i],j))
# partial sort using heapq: faster than sorted() for this data size
most = heapq.nlargest(30, zip(score,train_y))
# counting numbers for each k (= num+1)
for num in range(len(most)):
ans = Counter([x[1] for x in most[:num+1]]).most_common()
if ans[0][0] == dev_y[i]:
ks[num+1] += 1
print ks.items()
fix_k = max(ks.items(), key=lambda x: x[1])[0]
print "learned k:" + str(fix_k)
pred_y =[]
for i in test_X:
score = []
for j in train_X:
score.append(numpy.dot(i,j))
most = heapq.nlargest(fix_k, zip(score,train_y))
ans = Counter([x[1] for x in most]).most_common()
pred_y.append(ans[0][0])
f1_pred = f1_score(test_y, pred_y, average='micro')
print f1_pred