-
Notifications
You must be signed in to change notification settings - Fork 6
/
preprocessing.py
240 lines (219 loc) · 8.17 KB
/
preprocessing.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
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 14 17:24:21 2018
@author: yueningli
"""
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 10:17:43 2017
@author: yueningli
"""
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 14 17:24:21 2018
@author: yueningli
"""
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 10:17:43 2017
@author: yueningli
"""
import pandas as pd
import numpy as np
from sklearn.utils import shuffle
columns_list = ['STATUS', 'OPERATING_SYSTEM', 'DEVICE_TYPE', 'BROWSER', 'client_connectionType',
'APPLICATION_NAME', 'client_country', 'client_city', 'client_isp', 'client_organization']
def preprocessing(data,topnkey,num):
user =data[data['prsId']==topnkey[num]]
length=len(user)
col= len(columns_list)
predict=np.zeros((length,col))
category=np.zeros((length,col))
for feature in columns_list:
p, c = generic_preprocessing(user, feature)
predict[:, columns_list.index(feature)] = p
category[:, columns_list.index(feature)] = c
feat_data = pd.DataFrame(predict, columns=columns_list)
return category, user, feat_data
def generic_preprocessing(user, feature):
length = len(user)
pred = np.zeros((length))
cat = np.zeros((length))
mu,sigma=0,0.00001
deviceCount={}
for dt in user[feature]:
deviceCount[dt]=deviceCount.get(dt,0.0)+1.0
for i in range(0,length):
if pd.isnull(user[feature][user.index[i]]):
user.fillna(method='bfill')
pred[i]=deviceCount[user[feature][user.index[i]]]/length
dtlen=len(deviceCount)
dttmp=list(deviceCount)
for i in range(0,length):
for j in range(0,dtlen):
if user[feature][user.index[i]]==dttmp[j]:
sa=np.random.normal(mu,sigma)
cat[i]=j+1+sa
return pred, cat
'''
def preprocessing(data,topnkey,num):
user =data[data['prsId']==topnkey[num]]
length=len(user)
col=10
mu,sigma=0,0.00001
predict=np.zeros((length,col))
category=np.zeros((length,col))
sa=np.random.normal(mu,sigma,length)
for i in range(0,length):
if user['STATUS'][user.index[i]]!='SUCCESS':
predict[i][0]=0
else: predict[i][0]=1
category[:,0]=1+sa
osCount={}
for os in user['OPERATING_SYSTEM']:
osCount[os]=osCount.get(os,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['OPERATING_SYSTEM'][user.index[i]]):
user.fillna(method='bfill')
predict[i][1]=osCount[user['OPERATING_SYSTEM'][user.index[i]]]/length
oslen=len(osCount)
ostmp=list(osCount)
for i in range(0,length):
for j in range(0,oslen):
if user['OPERATING_SYSTEM'][user.index[i]]==ostmp[j]:
sa=np.random.normal(mu,sigma)
category[i][1]=j+1+sa
deviceCount={}
for dt in user['DEVICE_TYPE']:
deviceCount[dt]=deviceCount.get(dt,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['DEVICE_TYPE'][user.index[i]]):
user.fillna(method='bfill')
predict[i][2]=deviceCount[user['DEVICE_TYPE'][user.index[i]]]/length
dtlen=len(deviceCount)
dttmp=list(deviceCount)
for i in range(0,length):
for j in range(0,dtlen):
if user['DEVICE_TYPE'][user.index[i]]==dttmp[j]:
sa=np.random.normal(mu,sigma)
category[i][2]=j+1+sa
browserCount={}
for bc in user['BROWSER']:
browserCount[bc]=browserCount.get(bc,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['BROWSER'][user.index[i]]):
user.fillna(method='bfill')
predict[i][3]=browserCount[user['BROWSER'][user.index[i]]]/length
bclen=len(browserCount)
bctmp=list(browserCount)
for i in range(0,length):
for j in range(0,bclen):
if user['BROWSER'][user.index[i]]==bctmp[j]:
sa=np.random.normal(mu,sigma)
category[i][3]=j+1+sa
connectionType={}
for ct in user['client_connectionType']:
connectionType[ct]=connectionType.get(ct,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['client_connectionType'][user.index[i]]):
user.fillna(method='bfill')
predict[i][4]=connectionType[user['client_connectionType'][user.index[i]]]/length
ctlen=len(connectionType)
cttmp=list(connectionType)
for i in range(0,length):
for j in range(0,ctlen):
if user['client_connectionType'][user.index[i]]==cttmp[j]:
sa=np.random.normal(mu,sigma)
category[i][4]=j+1+sa
appCount={}
for an in user['APPLICATION_NAME']:
appCount[an]=appCount.get(an,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['APPLICATION_NAME'][user.index[i]]):
user.fillna(method='bfill')
predict[i][5]=appCount[user['APPLICATION_NAME'][user.index[i]]]/length
aclen=len(appCount)
acnum=np.linspace(1,aclen,aclen)
actmp=list(appCount)
for i in range(0,length):
for j in range(0,aclen):
if user['APPLICATION_NAME'][user.index[i]]==actmp[j]:
sa=np.random.normal(mu,sigma)
category[i][5]=j+1+sa
clientCountry={}
for ccoun in user['client_country']:
clientCountry[ccoun]=clientCountry.get(ccoun,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['client_country'][user.index[i]]):
user.fillna(method='bfill')
predict[i][6]=clientCountry[user['client_country'][user.index[i]]]/length
ccounlen=len(clientCountry)
ccountmp=list(clientCountry)
for i in range(0,length):
for j in range(0,ccounlen):
if user['client_country'][user.index[i]]==ccountmp[j]:
sa=np.random.normal(mu,sigma)
category[i][6]=j+1+sa
clientCity={}
for ccity in user['client_city']:
clientCity[ccity]=clientCity.get(ccity,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['client_city'][user.index[i]]):
user.fillna(method='bfill')
predict[i][7]=clientCity[user['client_city'][user.index[i]]]/length
ccitylen=len(clientCity)
ccitytmp=list(clientCity)
for i in range(0,length):
for j in range(0,ccitylen):
if user['client_city'][user.index[i]]==ccitytmp[j]:
sa=np.random.normal(mu,sigma)
category[i][7]=j+1+sa
clientISP={}
for cisp in user['client_isp']:
clientISP[cisp]=clientISP.get(cisp,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['client_isp'][user.index[i]]):
user.fillna(method='bfill')
predict[i][8]=clientISP[user['client_isp'][user.index[i]]]/length
cisplen=len(clientISP)
cisptmp=list(clientISP)
for i in range(0,length):
for j in range(0,cisplen):
if user['client_isp'][user.index[i]]==cisptmp[j]:
sa=np.random.normal(mu,sigma)
category[i][8]=j+1+sa
clientOrganization={}
for co in user['client_organization']:
clientOrganization[co]=clientOrganization.get(co,0.0)+1.0
for i in range(0,length):
if pd.isnull(user['client_organization'][user.index[i]]):
user.fillna(method='bfill')
predict[i][9]=clientOrganization[user['client_organization'][user.index[i]]]/length
colen=len(clientOrganization)
cotmp=list(clientOrganization)
for i in range(0,length):
for j in range(0,colen):
if user['client_organization'][user.index[i]]==cotmp[j]:
sa=np.random.normal(mu,sigma)
category[i][9]=j+1+sa
feat_data = pd.DataFrame(predict, columns=columns_list)
return category,user, feat_data
'''
def add_flag(data_frame):
os_list = np.array(data_frame['OPERATING_SYSTEM'].values)
length = len(os_list)
if length == 0:
return data_frame
os_list = np.sort(os_list)
os_threshold = os_list[length//2]
flags = np.zeros((length))
for i in range(length):
if data_frame['OPERATING_SYSTEM'][data_frame.index[i]] >= os_threshold:
flags[i] = 1
data_frame = data_frame.assign(flag=flags)
#data_frame.to_csv('sorted_sheet.csv', sep='\t')
return shuffle(data_frame)