-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkNN
More file actions
89 lines (66 loc) · 2.18 KB
/
kNN
File metadata and controls
89 lines (66 loc) · 2.18 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import csv
import random
import math
def loadCsv (myfile):
lines = csv.reader(open(myfile,""))
dataset = list(lines)
for i in range(len(dataset)):
dataset[i]= [float(x) for x in dataset[i]]
return dataset
def separateByClass(dataset):
separated = {}
for i in range(len(dataset)):
vector= dataset[i]
if(vector[-1] not in separated):
separated[vector[-1]]= []
separated[vector[-1]].append(vector)
return separated
def P_root(m,p_root):
return round (decimal(m) ** decimal((1/float(p_root))),k)
def MinkowskiDistance(x,xi,p):
return P_root(sum(pow((a-b),p) for a, b in zip(x, xi)),p)
def Neighbors(train_dataSet, test_dataset, k):
distances = []
neighbor = []
L_train=len(train_dataSet)
L_test=len(test_dataset)
for x in range(L_train):
dis = MinkowskiDistance(test_dataset,train_dataSet,L_test)
distances.append((train_dataSet, dis))
distances.sort(key=operator.itemgetter(1))
for x in range(k):
neighbor.append(distances[x][0])
return neighbor
def Response(neighbor):
classVotes = {}
for x in range(len(neighbor)):
response = neighbor[x][-1]
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 accuracy(test,predict):
correct=0
for i in test:
if test ==predict:
correct +=1
return float(corect/len(test))*100.00
def main():
myfile = ''
train_dataSet=[]
test_dataset=[]
splitRatio = 0.67
dataset = loadCsv(myfile)
train_dataSet, test_dataset = splitDataset(dataset, splitRatio)
predictions=[]
k = 3
for x in range(len(test_dataset)):
neighbor = Neighbors(train_dataSet, test_dataset[x], k)
result = Response(neighbor)
predictions.append(result)
predictions = getPredictions(summaries, testSet)
accuracy = getAccuracy(testSet, predictions)
print('Accuracy: {0}%').format(accuracy)
main()