-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.py
35 lines (33 loc) · 1.09 KB
/
knn.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
import numpy as np
from scipy.spatial.distance import cdist
from pre_process import readfile, getlabels
from collections import Counter
def test_knn(features, y_expected, knn_features, type_of_data):
if type_of_data == 1:
labels, label_lines = readfile('traininglabels', 1)
k = 3
else:
labels, label_lines = readfile('facedatatrainlabels', 2)
k = 19
labels = getlabels(labels)
D = cdist(features, knn_features)
D_index = np.argsort(D, axis=1)
nearestNeighbour = D_index[:, 0:k]
Ypred = []
for i in range(len(nearestNeighbour)):
temp = []
for j in range(k):
temp.append(labels[nearestNeighbour[i, j]])
Ypred.append(temp)
newYpred = []
for row in Ypred:
cnt = Counter(row).most_common(1)
newYpred.append(cnt[0][0])
error = 0
for i in range(len(y_expected)):
print newYpred[i], y_expected[i]
if newYpred[i] == y_expected[i]:
continue
else:
error += 1
print "Prediction accuracy = ", (1 - error / float(len(y_expected))) * 100, "%"