-
Notifications
You must be signed in to change notification settings - Fork 1
/
ourknn.py
37 lines (34 loc) · 947 Bytes
/
ourknn.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
import math
import operator
def euclideanDistance(test, train, length):
distance = 0
for x in range(length):
distance += pow((test[x] - train[x]), 2)
return math.sqrt(distance)
def getMjVotes(neighbors, y):
classVotes = {}
nhbrs = y[neighbors]
for x in range(len(nhbrs)):
response = nhbrs[x]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedVotes[0][0]
def myKnn(testdata, traindata, y, k):
responses = []
for X1 in testdata:
distances = []
position = 0
for X2 in traindata:
dist = euclideanDistance(X1, X2, len(X1))
distances.append((position, dist))
position += 1
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
response = getMjVotes(neighbors,y)
responses.append(response)
return responses