-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expedia Sampling.py
396 lines (258 loc) · 9.64 KB
/
Expedia Sampling.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# coding: utf-8
# In[ ]:
import pandas as pd
import numpy as np
train_original = pd.read_csv("/home/fabricc/Downloads/Data Mining VU data/training_set_VU_DM_2014.csv");
#train_original = pd.read_csv("/home/fabricc/Downloads/Data Mining VU data/test_set_VU_DM_2014.csv")
#train_original = pd.read_csv("/home/fabricc/Desktop/sample_coding_purposes.csv");
#train = train_original.sample(frac=0.01)
#train = train_original
# In[ ]:
train = train_original
#Winsorizing of price usd
import scipy.stats
import numpy as np
get_ipython().magic(u'matplotlib inline')
import matplotlib.pyplot as plt
#train.head
plt.figure()
train['price_usd'].plot(kind='box')
train['price_usd']=scipy.stats.mstats.winsorize(train['price_usd'], limits=0.005)
plt.figure()
train['price_usd'].plot(kind='box')
train.to_csv("/home/fabricc/Desktop/final_data/sampled_training_set.csv",Index=False)
# In[ ]:
train_original.sort_values(by=['srch_id','booking_bool', 'click_bool'], axis=0, ascending=[True,False, False], inplace=True)
#train_original.sort_values(by=['srch_id'], axis=0, ascending=[True], inplace=True)
# In[ ]:
#Data Preparation - Missing Values
train['prop_review_score'].fillna(train['prop_review_score'].min(), inplace=True)
train['orig_destination_distance'].fillna(train['orig_destination_distance'].median(), inplace=True)
train['prop_location_score2'].fillna(train['prop_location_score2'].min(), inplace=True)
min_srch_query_affinity_score = train['srch_query_affinity_score'].dropna().min()
train['srch_query_affinity_score'].fillna(0, inplace=True)
# In[ ]:
#Scaling
to_scale = train[['prop_review_score','prop_location_score1','prop_starrating']]
to_scale = (to_scale - to_scale.min()) / (to_scale.max() - to_scale.min())
train[['prop_review_score','prop_location_score1','prop_starrating']] = to_scale
#Standardization
to_std = train['price_usd']
to_std = (to_std - to_std.mean())/(to_std.std())
train['price_usd'] = to_std
# In[ ]:
train.drop('gross_bookings_usd', axis=1, inplace=True)
train.drop('position', axis=1, inplace=True)
# In[4]:
ratio = 5
temp_ratio = 0
picked = []
current_srch_id = -1
for item in train.iterrows():
current_srch_id = item[1].srch_id
if(item[1].booking_bool):
temp_ratio = ratio
picked.append(1)
continue
if (item[1].click_bool ):
picked.append(1)
elif(temp_ratio > 0):
picked.append(1)
temp_ratio = temp_ratio -1
else:
picked.append(0)
train['picked']=picked
train = train[(train.picked==1)]
# In[5]:
train.drop('picked', axis=1, inplace=True)
# In[14]:
import pandas as pd
import numpy as np
train = pd.read_csv("/home/fabricc/Desktop/final_data/sampled_training_set.csv")
train.to_csv("/home/fabricc/Desktop/final_data/sampled_training_set.csv",index=False)
# In[ ]:
#Data Preparation - Sampling by ID
#import random
#ids = bookings.srch_id.unique().tolist()
#num_ids = len(ids)
#size_sample = (num_ids * 40 ) / 100
#ids_sample = []
#for i in range(0,size_sample):
# selected_id = ids[random.randrange(0,num_ids)]
# ids_sample.append(selected_id)
# ids.remove(selected_id)
# num_ids = num_ids - 1
#train = train_original.loc[train_original['srch_id'].isin(ids_sample)]
# In[2]:
#Data Preparation - Feature Extraction 1
#Diff Star Rating
visitor_hist_starrating = train['visitor_hist_starrating']
prop_starrating = train['prop_starrating']
starrating_diff_list = []
for history, actual in zip(visitor_hist_starrating, prop_starrating):
if(np.isnan(history)):
starrating_diff_list.append(np.nan)
else:
starrating_diff_list.append(abs(history - actual))
starrating_diff = pd.Series(starrating_diff_list, index = visitor_hist_starrating.index.values)
train['starrating_diff']=starrating_diff
#Diff gross booking usd
visitor_hist_adr_usd = train['visitor_hist_adr_usd']
price_usd = train['price_usd']
usd_diff_list = []
for history, actual in zip(visitor_hist_adr_usd, price_usd):
if(np.isnan(history) or np.isnan(actual)):
usd_diff_list.append(np.nan)
else:
usd_diff_list.append(abs(history - actual))
usd_diff = pd.Series(usd_diff_list, index = visitor_hist_adr_usd.index.values)
train['usd_diff']=usd_diff
train.drop('visitor_hist_starrating', axis=1, inplace=True)
train.drop('visitor_hist_adr_usd', axis=1, inplace=True)
# In[3]:
#Data Preparation - Feature Extraction 1
#Date time decomposition
import datetime
#print train['date_time'][0]
breakfast_time = datetime.time(6,0,0)
lunch_time = datetime.time(12,0,0)
dinner_time = datetime.time(18,0,0)
week_day = []
month_day = []
month = []
year = []
time = []
for date_item in train['date_time'].iteritems():
date = datetime.datetime.strptime(date_item[1], '%Y-%m-%d %H:%M:%S')
week_day.append(date.weekday())
month_day.append(date.day)
month.append(date.month)
year.append(date.year)
t = date.time()
if(breakfast_time > t):
time_range = 3 #night
elif (lunch_time > t):
time_range = 0 #morning
elif (dinner_time > t):
time_range = 1 #afternoon
else:
time_range = 2 #evening
time.append(time_range)
d={'week_of_the_day':week_day,'month_day':month_day,'month':month,'year':year,'time':time}
f = pd.DataFrame(data=d, index=train['date_time'].index.values)
train = train.join(f)
train.drop('date_time', axis=1, inplace=True)
# In[5]:
#Merging Click and Booking
click_book = []
for click,book in zip(train["click_bool"],train["booking_bool"]):
if(book):
click_book.append(5)
elif (click):
click_book.append(1)
else: click_book.append(0)
column = pd.Series(click_book, index = train["click_bool"].index.values)
train['click_or_book']= column
# In[6]:
#Data Preparation
#Competitors attributes shrinking
cheaper_comp_count_list = []
for index, row in train.iterrows():
count = 0
count_nan = 0
for x in range(1,9):
comp_rate = "comp{0}_rate".format(x)
comp_inv = "comp{0}_inv".format(x)
if (not np.isnan(row[comp_rate]) and not row[comp_inv]):
if(row[comp_inv]==0 and row[comp_rate]==1): count=count+1
else: count_nan = count_nan +1
if(count_nan < 8):
cheaper_comp_count_list.append(count)
else: cheaper_comp_count_list.append(np.nan)
train['cheaper_comp_count'] = pd.Series(cheaper_comp_count_list, index = train.index.values)
# In[7]:
#Filling missing data in derived attributes
train['cheaper_comp_count'].fillna(-1, inplace=True)
train['starrating_diff'].fillna(-1, inplace=True)
train['usd_diff'].fillna(-1, inplace=True)
# In[8]:
#train.drop('year', axis=1, inplace=True)
#train.drop('prop_id', axis=1, inplace=True)
#train.drop('click_bool', axis=1, inplace=True)
#train.drop('booking_bool', axis=1, inplace=True)
for x in range(1,9):
train.drop("comp{0}_rate".format(x), axis=1, inplace=True)
train.drop("comp{0}_inv".format(x), axis=1, inplace=True)
train.drop("comp{0}_rate_percent_diff".format(x), axis=1, inplace=True)
# In[9]:
train.to_csv("/home/fabricc/Desktop/final_data/sampled_training_set.csv",index=False)
# In[ ]:
split = np.array_split(train, 5)
i = 1
for s in split:
s.to_csv("/home/fabricc/Desktop/TestCleaning/Splits/subtest{0}.csv".format(i))
i = i + 1
# In[ ]:
import pandas as pd
import numpy as np
i = 1
for s in range(0,5):
train = pd.read_csv("/home/fabricc/Desktop/TestCleaning/Splits/subtest{0}.csv".format(i))
print train.columns
i = i + 1
break
# In[ ]:
import pandas as pd
import numpy as np
i = 1
for s in range(0,5):
print "start processing subset {0}".format(i)
train = pd.read_csv("/home/fabricc/Desktop/TestCleaning/Splits/subtest{0}.csv".format(i))
#Data Preparation
#Competitors attributes shrinking
cheaper_comp_count_list = []
for index, row in train.iterrows():
count = 0
count_nan = 0
for x in range(1,9):
comp_rate = "comp{0}_rate".format(x)
comp_inv = "comp{0}_inv".format(x)
if (not np.isnan(row[comp_rate]) and not row[comp_inv]):
if(row[comp_inv]==0 and row[comp_rate]==1): count=count+1
else: count_nan = count_nan +1
if(count_nan < 8):
cheaper_comp_count_list.append(count)
else: cheaper_comp_count_list.append(np.nan)
train['cheaper_comp_count'] = pd.Series(cheaper_comp_count_list, index = train.index.values)
train.drop('date_time', axis=1, inplace=True)
train.drop('visitor_hist_starrating', axis=1, inplace=True)
train.drop('visitor_hist_adr_usd', axis=1, inplace=True)
#train.drop('year', axis=1, inplace=True)
#train.drop('prop_id', axis=1, inplace=True)
#train.drop('click_bool', axis=1, inplace=True)
#train.drop('booking_bool', axis=1, inplace=True)
train.drop('gross_bookings_usd', axis=1, inplace=True)
train.drop('position', axis=1, inplace=True)
train.drop('Unnamed: 0', axis=1, inplace=True)
train.drop('Unnamed: 0.1', axis=1, inplace=True)
train.drop('Unnamed: 0.1.1', axis=1, inplace=True)
for x in range(1,9):
train.drop("comp{0}_rate".format(x), axis=1, inplace=True)
train.drop("comp{0}_inv".format(x), axis=1, inplace=True)
train.drop("comp{0}_rate_percent_diff".format(x), axis=1, inplace=True)
#Filling missing data in derived attributes
train['cheaper_comp_count'].fillna(-1, inplace=True)
train['starrating_diff'].fillna(-1, inplace=True)
train['usd_diff'].fillna(-1, inplace=True)
train.to_csv("/home/fabricc/Desktop/TestCleaning/Splits/subtest_complete{0}.csv".format(i),index=False)
print "stop processing subset {0}".format(i)
i = i + 1
# In[ ]:
import pandas as pd
import numpy as np
i = 1
for s in range(0,5):
train = pd.read_csv("/home/fabricc/Desktop/TestCleaning/Splits/subtest{0}.csv".format(i))
print train.columns
i = i + 1
break