-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchMark_Expedia_script.py
More file actions
153 lines (126 loc) · 5.16 KB
/
BenchMark_Expedia_script.py
File metadata and controls
153 lines (126 loc) · 5.16 KB
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
# code source: https://www.kaggle.com/zfturbo/expedia-hotel-recommendations/leakage-solution/code
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'
import datetime
from heapq import nlargest
from operator import itemgetter
from collections import defaultdict
train_file = "/home/ml1/Downloads/Kaggle-Expedia/AnalyzedData/train.csv"
test_file = "/home/ml1/Downloads/Kaggle-Expedia/AnalyzedData/test.csv"
output_path = "/home/ml1/Downloads/Kaggle-Expedia/AnalyzedData/"
def run_solution():
print('Preparing arrays...')
f = open(train_file, "r")
f.readline()
best_hotels_od_ulc = defaultdict(lambda: defaultdict(int))
best_hotels_search_dest = defaultdict(lambda: defaultdict(int))
best_hotels_search_dest1 = defaultdict(lambda: defaultdict(int))
best_hotel_country = defaultdict(lambda: defaultdict(int))
popular_hotel_cluster = defaultdict(int)
total = 0
# Calc counts
while 1:
line = f.readline().strip()
total += 1
if total % 10000000 == 0:
print('Read {} lines...'.format(total))
if line == '':
break
arr = line.split(",")
book_year = int(arr[0][:4])
user_location_city = arr[5]
orig_destination_distance = arr[6]
srch_destination_id = arr[16]
is_booking = int(arr[18])
hotel_country = arr[21]
hotel_market = arr[22]
hotel_cluster = arr[23]
append_1 = 3 + 17*is_booking
append_2 = 1 + 5*is_booking
if user_location_city != '' and orig_destination_distance != '':
best_hotels_od_ulc[(user_location_city, orig_destination_distance)][hotel_cluster] += 1
if srch_destination_id != '' and hotel_country != '' and hotel_market != '' and book_year == 2014:
best_hotels_search_dest[(srch_destination_id, hotel_country, hotel_market)][hotel_cluster] += append_1
if srch_destination_id != '':
best_hotels_search_dest1[srch_destination_id][hotel_cluster] += append_1
if hotel_country != '':
best_hotel_country[hotel_country][hotel_cluster] += append_2
popular_hotel_cluster[hotel_cluster] += 1
f.close()
print('Generate submission...')
now = datetime.datetime.now()
path = output_path + 'submission_' + str(now.strftime("%Y-%m-%d-%H-%M")) + '.csv'
out = open(path, "w")
f = open(test_file, "r")
f.readline()
total = 0
out.write("id,hotel_cluster\n")
topclasters = nlargest(5, sorted(popular_hotel_cluster.items()), key=itemgetter(1))
while 1:
line = f.readline().strip()
total += 1
if total % 1000000 == 0:
print('Write {} lines...'.format(total))
if line == '':
break
arr = line.split(",")
id = arr[0]
user_location_city = arr[6]
orig_destination_distance = arr[7]
srch_destination_id = arr[17]
hotel_country = arr[20]
hotel_market = arr[21]
out.write(str(id) + ',')
filled = []
s1 = (user_location_city, orig_destination_distance)
if s1 in best_hotels_od_ulc:
d = best_hotels_od_ulc[s1]
topitems = nlargest(5, sorted(d.items()), key=itemgetter(1))
for i in range(len(topitems)):
if topitems[i][0] in filled:
continue
if len(filled) == 5:
break
out.write(' ' + topitems[i][0])
filled.append(topitems[i][0])
s2 = (srch_destination_id, hotel_country, hotel_market)
if s2 in best_hotels_search_dest:
d = best_hotels_search_dest[s2]
topitems = nlargest(5, d.items(), key=itemgetter(1))
for i in range(len(topitems)):
if topitems[i][0] in filled:
continue
if len(filled) == 5:
break
out.write(' ' + topitems[i][0])
filled.append(topitems[i][0])
elif srch_destination_id in best_hotels_search_dest1:
d = best_hotels_search_dest1[srch_destination_id]
topitems = nlargest(5, d.items(), key=itemgetter(1))
for i in range(len(topitems)):
if topitems[i][0] in filled:
continue
if len(filled) == 5:
break
out.write(' ' + topitems[i][0])
filled.append(topitems[i][0])
if hotel_country in best_hotel_country:
d = best_hotel_country[hotel_country]
topitems = nlargest(5, d.items(), key=itemgetter(1))
for i in range(len(topitems)):
if topitems[i][0] in filled:
continue
if len(filled) == 5:
break
out.write(' ' + topitems[i][0])
filled.append(topitems[i][0])
for i in range(len(topclasters)):
if topclasters[i][0] in filled:
continue
if len(filled) == 5:
break
out.write(' ' + topclasters[i][0])
filled.append(topclasters[i][0])
out.write("\n")
out.close()
print('Completed!')
run_solution()