Skip to content

Commit bef7635

Browse files
committed
Reorganizing code and adding unstaged code
Former-commit-id: de014d962df43aec20626c3e07cb058adf68970a
1 parent d9667b4 commit bef7635

File tree

324 files changed

+2239111
-993298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+2239111
-993298
lines changed

#I just work with main CSV

Lines changed: 0 additions & 23 deletions
This file was deleted.

1

Lines changed: 0 additions & 8 deletions
This file was deleted.

AlgorithmCode/kNN

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import csv
2+
import random
3+
import math
4+
5+
def loadCsv (myfile):
6+
lines = csv.reader(open(myfile,""))
7+
dataset = list(lines)
8+
for i in range(len(dataset)):
9+
dataset[i]= [float(x) for x in dataset[i]]
10+
return dataset
11+
12+
13+
14+
def separateByClass(dataset):
15+
separated = {}
16+
for i in range(len(dataset)):
17+
vector= dataset[i]
18+
if(vector[-1] not in separated):
19+
separated[vector[-1]]= []
20+
separated[vector[-1]].append(vector)
21+
return separated
22+
23+
24+
def P_root(m,p_root):
25+
26+
return round (decimal(m) ** decimal((1/float(p_root))),k)
27+
28+
29+
30+
def MinkowskiDistance(x,xi,p):
31+
return P_root(sum(pow((a-b),p) for a, b in zip(x, xi)),p)
32+
33+
34+
def Neighbors(train_dataSet, test_dataset, k):
35+
distances = []
36+
neighbor = []
37+
L_train=len(train_dataSet)
38+
L_test=len(test_dataset)
39+
for x in range(L_train):
40+
dis = MinkowskiDistance(test_dataset,train_dataSet,L_test)
41+
distances.append((train_dataSet, dis))
42+
distances.sort(key=operator.itemgetter(1))
43+
44+
for x in range(k):
45+
neighbor.append(distances[x][0])
46+
47+
return neighbor
48+
49+
def Response(neighbor):
50+
classVotes = {}
51+
for x in range(len(neighbor)):
52+
response = neighbor[x][-1]
53+
if response in classVotes:
54+
classVotes[response] += 1
55+
else:
56+
classVotes[response] = 1
57+
sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)
58+
59+
return sortedVotes[0][0]
60+
61+
def accuracy(test,predict):
62+
correct=0
63+
for i in test:
64+
if test ==predict:
65+
correct +=1
66+
67+
return float(corect/len(test))*100.00
68+
69+
70+
def main():
71+
myfile = ''
72+
train_dataSet=[]
73+
test_dataset=[]
74+
splitRatio = 0.67
75+
dataset = loadCsv(myfile)
76+
train_dataSet, test_dataset = splitDataset(dataset, splitRatio)
77+
predictions=[]
78+
k = 3
79+
for x in range(len(test_dataset)):
80+
neighbor = Neighbors(train_dataSet, test_dataset[x], k)
81+
result = Response(neighbor)
82+
predictions.append(result)
83+
84+
predictions = getPredictions(summaries, testSet)
85+
accuracy = getAccuracy(testSet, predictions)
86+
print('Accuracy: {0}%').format(accuracy)
87+
88+
89+
main()

0 commit comments

Comments
 (0)