-
Notifications
You must be signed in to change notification settings - Fork 1
/
knn.py
executable file
·35 lines (28 loc) · 1.06 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
#!/usr/bin/python
# Authors: Ayesha Bhimdiwala(aybhimdi), Umang Mehta(mehtau) & Vaishnavi Srinivasan(vsriniv)
# Please find the Report and Design Decisions listed in Report.pdf alongside.
import math
from Queue import PriorityQueue
import numpy as np
def knnTrain(trainFile, modelFile):
trainData = open(trainFile, "r")
modelAppend = open(modelFile, "w")
for line in trainData:
row = line[:-1].split(' ', 2)
modelAppend.write("%s|%s\n" % (row[1], row[2]))
trainData.close()
modelAppend.close()
def knnTest(testVector, trainOrient, trainVector, kValue, knn, knnDist):
distQueue = PriorityQueue()
for row in range(0,len(trainOrient),1):
vector = trainVector[row]
orient = int(trainOrient[row])
eucDist = math.sqrt(np.sum(np.power((vector - testVector), 2)))
distQueue.put((eucDist, orient))
k=kValue
for i in range(0, k, 1):
knnScore = distQueue.get()
knn[knnScore[1]] += 1
knnDist[knnScore[1]] += knnScore[0]
predictOrient = max(knn, key=knn.get)
return predictOrient