-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwknn.py
97 lines (81 loc) · 2.56 KB
/
wknn.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
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
90
91
92
93
94
95
96
import csv
import math
inputDict = {}
import os
def getInput(dict_wknn_value,dict_wknn_coo,filename):
with open(filename, mode ='r')as file:
csvFile = csv.reader(file)
for lines in csvFile:
# print(lines[0],lines[1])
# row = lines[0].split(',')
# print(row)
inputDict[lines[0]] = float(lines[1])
def euclideanDist(dict_wknn_value,dict_wknn_coo,dict):
sqsum = 0
for mac,rssi in inputDict.items():
x = 0
if mac in dict.keys():
x = dict[mac]
sqsum = sqsum + pow(rssi-x ,2)
for mac,avgRssi in dict.items():
if mac not in inputDict.keys():
sqsum = sqsum + pow(avgRssi-x, 2)
return math.sqrt(sqsum)
def matching(dict_wknn_value,dict_wknn_coo,filenames):
i=0
for filename in filenames:
dict = {}
with open('data/avg/'+filename, mode ='r')as file:
csvFile = csv.reader(file)
for lines in csvFile:
row = lines[0].split(',')
dict[lines[0]] = float(lines[1])
dict_wknn_value[i] = euclideanDist(dict_wknn_value,dict_wknn_coo,dict)
# print(dict_wknn_value[i])
i=i+1
def wknn(dict_wknn_value,dict_wknn_coo,k):
sorted_dic = sorted(dict_wknn_value.items(), reverse = True,key=lambda x:x[1])
dict_wknn = dict(sorted_dic)
x=0
y=0
sum=0
i=0
for indx,val in dict_wknn_value.items():
if i==k:
break
st = dict_wknn_coo[indx]
coo = st.split(',')
x += float(coo[0]) * val
y += float(coo[1]) * val
sum+=val
i=+1
x=x/sum
y=y/sum
lis=[x,y]
print("xxxxxxxxxx",lis)
return lis
def apply_wknn():
k=4
dict_wknn_value = {}
dict_wknn_coo = {}
dir_list = os.listdir("data/avg")
# filenames = ['lib_(1,1).csv','lib_(1,2).csv','lib_(2,1).csv','lib_(2,2).csv']
# filenames = ['lib_(1,1).csv','lib_(1,2).csv','lib_(2,1).csv','lib_(2,2).csv']
filenames = os.listdir("data/avg")
inputFilename='input.csv'
i=0
for n in filenames:
s = n.index('(')
e = n.index(')')
coo = n[s+1:e]
# print(coo)
dict_wknn_coo[i] = coo
i=i+1
getInput(dict_wknn_value,dict_wknn_coo,inputFilename)
matching(dict_wknn_value,dict_wknn_coo,filenames)
sorted_dic = sorted(dict_wknn_value.items(),reverse=True ,key=lambda x:x[1])
dict_wknn_value = dict(sorted_dic)
# print(dict_wknn_value)
# print(dict_wknn_coo)
return wknn(dict_wknn_value,dict_wknn_coo,k)
# apply_wknn()