-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider_loop_multi.py
230 lines (199 loc) · 6.04 KB
/
spider_loop_multi.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#input file as first argument
#output file as second argument
import numpy as np
from sklearn.neighbors import NearestNeighbors as NN
import sys
from random import random
from multiprocessing import Process, Queue
class attributes_data(object):
def __init__(self):
self.data = []
self.yesno = []
self.data_index = []
self.flags = [] #1 for safe, 0 for not-safe
f=open(sys.argv[1])
#k = int(input('specify value of k for KNN : '))
#relabel = int(input('Relabel ???\n1 for "Yes", 0 for "No" : '))
#ampl = int(input('Amplification ???\n0-No, 1-Weak, 2-Strong : '))
#change below parameters according to requirment
indices = [0,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] #numbers in indices list should represent columns of float type
YNcolumn = 35 #column containing Y/N. This column should be last column of csv file.
ReasonColumn = -1 #Set this variable -1 if there is no column for reason.
heading_present = 1 # 1 if heading is present in csv file, 0 if not present.
original = []
#DS = attributes_data()
if heading_present:
heading = f.readline()
for r in f.readlines():
original.append(r[:-1].split(','))
orig_len = len(original)
f.close()
def knn_training_test_same(dataset,k_knn):
data_numpy = np.array(dataset, dtype=np.float)
nbrs = NN(n_neighbors=k_knn+1, algorithm='ball_tree').fit(data_numpy)
distances,k_i = nbrs.kneighbors(data_numpy)
return k_i
def knn_training_test_diff(trainset,testset,k_knn):
train_numpy = np.array(trainset, dtype=np.float)
test_numpy = np.array(testset, dtype=np.float)
nbrs = NN(n_neighbors=k_knn+1, algorithm='ball_tree').fit(train_numpy)
distances,k_i = nbrs.kneighbors(test_numpy)
return k_i
def amplify(dest,src,example,example_index,k_knn):
k_i = knn_training_test_diff(src.data,[example],k_knn)
maj_n=0
min_n=0
for index in k_i[0][1:]:
if src.yesno[index]=='N':
maj_n += 1
else:
min_n += 1
n = maj_n-min_n+1
k_i = knn_training_test_diff(src.data,[example],n)
for i in range(0,n):
dest.data.append(example)
dest.yesno.append('Y')
dest.data_index.append(example_index)
def filewrite(file,group,synthetic_flag=0):
L=len(group.yesno)
for i in range(0,L):
if synthetic_flag and ReasonColumn != -1:
temp=original[group.data_index[i]][ReasonColumn]
original[group.data_index[i]][ReasonColumn]='Synthetic'
for j in range(0,YNcolumn):
if j in indices:
file.write(str(group.data[i][indices.index(j)])+',')
else:
file.write(original[group.data_index[i]][j]+',')
file.write(group.yesno[i]+'\n')
if synthetic_flag and ReasonColumn != -1:
original[group.data_index[i]][ReasonColumn]=temp
def mainprocess(k,relabel):
cpy = original
DS = attributes_data()
for i in range(0,orig_len):
temp = []
for index in indices:
temp.append(float(cpy[i][index]))
DS.data.append(temp)
DS.yesno.append(cpy[i][YNcolumn])
DS.data_index.append(i)
#flag setting code starts
knn_indices = knn_training_test_same(DS.data,k)
for i,element in enumerate(knn_indices):
positive = 0
negetive = 0
for e in element[1:]:
if DS.yesno[e] == DS.yesno[i]:
positive += 1
else:
negetive += 1
if positive >= negetive:
DS.flags.append(1)
else:
DS.flags.append(0)
#flag setting code ends
RS = attributes_data()
for i,d in enumerate(DS.data):
if DS.yesno[i]=='N' and DS.flags[i]==0:
RS.data.append(d)
RS.yesno.append('N')
RS.flags.append(0)
RS.data_index.append(DS.data_index[i])
if relabel:
for i,index in enumerate(RS.data_index):
DS.yesno[index]='Y'
if ReasonColumn != -1:
cpy[index][ReasonColumn]='Relabled'
else:
for i,index in enumerate(RS.data_index):
DS.data.pop(index-i)
DS.data_index.pop(index-i)
DS.yesno.pop(index-i)
DS.flags.pop(index-i)
DS.flags=[]
#flag setting code starts
knn_indices = knn_training_test_same(DS.data,k)
for i,element in enumerate(knn_indices):
positive = 0
negetive = 0
for e in element[1:]:
if DS.yesno[e] == DS.yesno[i]:
positive += 1
else:
negetive += 1
if positive >= negetive:
DS.flags.append(1)
else:
DS.flags.append(0)
#flag setting code ends
#ampl = 0
outfile = open('k%dr%da0.csv'%(k,relabel),'w')
if heading_present:
outfile.write(heading)
filewrite(outfile,DS,0)
print ('%d\textra entries added to top of file k%dr%da0.csv'%(0,k,relabel))
outfile.close()
#ampl = 1
extra = attributes_data()
outfile = open('k%dr%da1.csv'%(k,relabel),'w')
if heading_present:
outfile.write(heading)
L=len(DS.yesno)
for i in range(0,L):
if DS.yesno[i]=='Y' and DS.flags[i]==0:
amplify(extra,DS,DS.data[i],DS.data_index[i],k)
filewrite(outfile,extra,1)
filewrite(outfile,DS,0)
print ('%d\textra entries added to top of file k%dr%da1.csv'%(len(extra.yesno),k,relabel))
outfile.close()
#ampl = 2
extra = attributes_data()
outfile = open('k%dr%da2.csv'%(k,relabel),'w')
if heading_present:
outfile.write(heading)
L=len(DS.yesno)
for i in range(0,L):
if DS.yesno[i]=='Y' and DS.flags[i]==0:
knn_indices = knn_training_test_diff(DS.data,[DS.data[i]],k+2)
positive = 0
negetive = 0
for index in knn_indices[0][1:]:
if DS.yesno[index]=='Y':
positive += 1
else:
negetive += 1
if positive >= negetive:
amplify(extra,DS,DS.data[i],DS.data_index[i],k)
else:
amplify(extra,DS,DS.data[i],DS.data_index[i],k+2)
filewrite(outfile,extra,1)
filewrite(outfile,DS,0)
print ('%d\textra entries added to top of file k%dr%da2.csv'%(len(extra.yesno),k,relabel))
outfile.close()
if __name__=='__main__':
for K in range(105,201,8):
p1 = Process(target=mainprocess, args=(K,0))
p1.start()
p2 = Process(target=mainprocess, args=(K,1))
p2.start()
p3 = Process(target=mainprocess, args=(K+2,0))
p3.start()
p4 = Process(target=mainprocess, args=(K+2,1))
p4.start()
p5 = Process(target=mainprocess, args=(K+4,0))
p5.start()
p6 = Process(target=mainprocess, args=(K+4,1))
p6.start()
p7 = Process(target=mainprocess, args=(K+6,0))
p7.start()
p8 = Process(target=mainprocess, args=(K+6,1))
p8.start()
p1.join()
p2.join()
p3.join()
p4.join()
p5.join()
p6.join()
p7.join()
p8.join()