-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
466 lines (342 loc) · 12.2 KB
/
app.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from flask import Flask , render_template, request
from sklearn.preprocessing import StandardScaler
import pandas as pd
import pickle
import datetime as dt
import Call as cl
import adr_operations as de
app =Flask(__name__)
B_model = pickle.load(open('RF_model.pkl','rb'))
S_model = pickle.load(open('RF_Seg_model.pkl','rb'))
output = cl.GetDetailsOfCluster("cltData.csv",1200000,'custom',2)
########################################################################################
################################Segment Comparison Charts####################################
#Getting output dictionary from classification.py
output = cl.GetDetailsOfCluster("correct_cluster.csv",1200000,'all',0)
output3 = cl.GetDetailsOfCluster("correct_cluster.csv",1200000,'custom',3)
#######################################TOTAL REVENUE - BAR CHART
trlabels = [2015,2016,2017]
#Segment 01
trvalues = []
trvalues.append(output[0]['2015_total_revenue'])
trvalues.append(output[0]['2016_total_revenue'])
trvalues.append(output[0]['2017_total_revenue'])
#Segment 02
trvalues2 = []
trvalues2.append(output[1]['2015_total_revenue'])
trvalues2.append(output[1]['2016_total_revenue'])
trvalues2.append(output[1]['2017_total_revenue'])
#Segment 03
trvalues3 = []
trvalues3.append(output3[3]['2015_total_revenue'])
trvalues3.append(output3[3]['2016_total_revenue'])
trvalues3.append(output3[3]['2017_total_revenue'])
######################################COUNTRIES - PIE CHART
#Segment 1
cdlabels = []
cdvalues = []
for i,j in output[0]['country'].items():
cdlabels.append(i)
cdvalues.append(j)
#Segment 2
cdlabels2 = []
cdvalues2 = []
for i,j in output[1]['country'].items():
cdlabels2.append(i)
cdvalues2.append(j)
#Segment 3
cdlabels3 = []
cdvalues3 = []
for i,j in output3[3]['country'].items():
cdlabels3.append(i)
cdvalues3.append(j)
##########################################SEASONAL MONTHS - BAR CHART
smlabels = [
'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December']
#Segment 1
smvalues = []
for i in smlabels:
if i not in output[0]['month_name'].keys():
smvalues.append(0)
else:
smvalues.append(output[0]['month_name'][i])
#Segment 2
smvalues2 = []
for i in smlabels:
if i not in output[1]['month_name'].keys():
smvalues2.append(0)
else:
smvalues2.append(output[1]['month_name'][i])
#Segment 3
smvalues3 = []
for i in smlabels:
if i not in output3[3]['month_name'].keys():
smvalues3.append(0)
else:
smvalues3.append(output3[3]['month_name'][i])
##########################################LOYALTY - PIE CHART
#Segment 1
lolabels = []
lovalues = []
loyal = []
for i,j in output[0]['loyalty'].items():
lolabels.append(i)
loyal.append(j)
for k in loyal:
lovalues.append(k*100/sum(loyal))
#Segment 2
lolabels2 = []
lovalues2 = []
loyal2 = []
for i,j in output[1]['loyalty'].items():
lolabels2.append(i)
loyal2.append(j)
for k in loyal2:
lovalues2.append(k*100/sum(loyal2))
#Segment 3
lolabels3 = []
lovalues3 = []
loyal3 = []
for i,j in output3[3]['loyalty'].items():
lolabels3.append(i)
loyal3.append(j)
for k in loyal3:
lovalues3.append(k*100/sum(loyal3))
###########################################ROI VALUES - BAR CHART
roilabels = [2015,2016,2017]
#Segment 1
roivalues = []
roivalues.append(output[0]['ROI_percentage_2015'])
roivalues.append(output[0]['ROI_percentage_2016'])
roivalues.append(output[0]['ROI_percentage_2017'])
#Segment 2
roivalues2 = []
roivalues2.append(output[1]['ROI_percentage_2015'])
roivalues2.append(output[1]['ROI_percentage_2016'])
roivalues2.append(output[1]['ROI_percentage_2017'])
#Segment 3
roivalues3 = []
roivalues3.append(output3[3]['ROI_percentage_2015'])
roivalues3.append(output3[3]['ROI_percentage_2016'])
roivalues3.append(output3[3]['ROI_percentage_2017'])
##############################FOOOD CATEGORIES - HORIZONTAL BAR CHART
#Segment 1
foodlabels = []
foodvalues = []
for i,j in output[0]['food'].items():
if i == 'Special Requests':
foodlabels.append('SR')
else:
foodlabels.append(i)
foodvalues.append(j)
#Segment 2
foodlabels2 = []
foodvalues2 = []
for i,j in output[1]['food'].items():
if i == 'Special Requests':
foodlabels2.append('SR')
else:
foodlabels2.append(i)
foodvalues2.append(j)
#Segment 3
foodlabels3 = []
foodvalues3 = []
for i,j in output3[3]['food'].items():
if i == 'Special Requests':
foodlabels3.append('SR')
else:
foodlabels3.append(i)
foodvalues3.append(j)
##########################################EMPLOYEE SKILL
#Segment 1
dic1 = output[0]['employee_skill']
#Segment 2
dic12 = output[1]['employee_skill']
#Segment 3
dic13 = output3[3]['employee_skill']
##########################################CANCELLED DATES
#Segment 1
cdates = output[0]['canceldetails'][0]
#Segment 1
cdates2 = output[1]['canceldetails'][0]
#Segment 3
cdates3 = output3[3]['canceldetails'][0]
######################################
#######################################FUNCTION IMPLEMENTATION
#yuvins code
labels = []
loopVar = de.dictionryOutput()[0]
count = 0
for keys, values in loopVar.items():
for i in values.keys():
# print(i)
if i == "June":
labels.append(i)
count += 1
break
else:
labels.append(i)
if count > 0:
break
colors = [
"#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA",
"#ABCDEF", "#DDDDDD", "#ABCABC", "#4169E1",
"#C71585", "#FF4500", "#FEDCBA", "#46BFBD"]
season1 = list( de.financialYearPattern()[0]["season1"].values() )
season2 = list( de.financialYearPattern()[0]["season2"].values() )
season3 = list( de.financialYearPattern()[0]["season3"].values() )
season1Total = list( de.financialYearPattern()[1]["season1"].values())
season2Total = list( de.financialYearPattern()[1]["season2"].values())
season3Total = list( de.financialYearPattern()[1]["season3"].values())
avgValues = list(de.annualDailyRateAvg().values())
avgTotal = list(de.annualTotalRevAvg().values())
yearlist = de.yearList()
#yuvin code end
@app.route('/')
@app.route('/Workspace')
def index():
return render_template('welcome.html')
@app.route("/predict", methods=['POST','GET'])
def predict():
x_pred=[]
featurs=['lead_time','previous_cancellations','previous_bookings_not_canceled','booking_changes','agent','days_in_waiting_list','adr','required_car_parking_spaces','total_of_special_requests']
for i in featurs:
x_pred.append(float(request.form.get(i)))
x=pd.DataFrame([x_pred])
ariving_date = int(request.form.get('lead_time'))
adults=int(request.form.get('adults'))
babys = int(request.form.get('babys'))
children = int(request.form.get('children'))
date = request.form.get('booking_date')
number_of_nights=int(request.form.get('number_of_days'))
adr=float(request.form.get('adr'))
date_time_obj = dt.datetime.strptime(date, '%Y-%m-%d')
Recency=(getLatestTime(2018,1,1)-date_time_obj).days
Frequency=babys+children+adults
Monetary=adr*number_of_nights
B_pred=B_model.predict(x)
B_output= round(B_pred[0])
x = pd.DataFrame([[Recency, Frequency, Monetary]])
S_pred = S_model.predict(x)
S_output = round(S_pred[0])
output = cl.GetDetailsOfCluster("cltData.csv", 1200000, 'custom', S_output)
details={}
details['food']=max(output[S_output]['food'],key=output[S_output]['food'].get)
details['room']=max(output[S_output]['roomcount'],key=output[S_output]['roomcount'].get)
output=cl.FinancialOfferCalculations([2015,2017],['January','August','July'],40)['segment_discount_percentage']
details['discount']=''
if (S_output in output.keys()):
details['discount']=str(round(output[S_output]))+'%'
else:
details['discount']='No Discount'
details['lead_time']=ariving_date
if(ariving_date>25 ):
details['text']='The Guest passed the maximum No of arrival days in this segment ,Try to book before 25 no of days'
else:
details['text']=''
if B_pred ==1:
return render_template('bad.html', out=details)
else:
return render_template('good.html', out=details, child=children, adu=adults)
@app.route('/Dashbord')
def showLineChart():
line_labels = labels
print(line_labels)
line_avg_total = avgTotal
season1_line = ''
line_avg = avgValues
season1_total = season1Total
season2_total = season2Total
season3_total = season3Total
season1_line = season1
season2_line = season2
season3_line = season3
var_yearlist = ['F-Year 1', 'F-Year 2', 'F-Year 3']
return render_template('charts.html', title='Bitcoin Monthly Price in USD', max=200, labels=line_labels,
values2 = season1_line, values3 = season2_line, values4 = season3_line,
avgValues = line_avg, avgTotal = line_avg_total, yearlist = var_yearlist,
season1Total = season1_total, season2Total = season2_total, season3Total = season3_total )
@app.route('/Discount')
def getDiscount():
return render_template('Discount_meter.html')
@app.route('/Customer_Segmentation')
def Countrypie():
#Segment 1
bar_label_1 = trlabels
bar_value_1 = trvalues
bar_label_2 = smlabels
bar_value_2 = smvalues
bar_label_3 = roilabels
bar_value_3 = roivalues
hor_label_1 = foodlabels
hor_value_1 = foodvalues
pie_label_1 = cdlabels
pie_value_1 = cdvalues
pie_label_2 = lolabels
pie_value_2 = lovalues
cdays = cdates
dic2 = dic1
#Segment 2
bar_label_12 = trlabels
bar_value_12 = trvalues2
bar_label_22 = smlabels
bar_value_22 = smvalues2
bar_label_32 = roilabels
bar_value_32 = roivalues2
hor_label_12 = foodlabels2
hor_value_12 = foodvalues2
pie_label_12 = cdlabels2
pie_value_12 = cdvalues2
pie_label_22 = lolabels2
pie_value_22 = lovalues2
cdays2 = cdates2
dic22 = dic12
#Segment 3
bar_label_13 = trlabels
bar_value_13 = trvalues3
bar_label_23 = smlabels
bar_value_23 = smvalues3
bar_label_33 = roilabels
bar_value_33 = roivalues3
hor_label_13 = foodlabels3
hor_value_13 = foodvalues3
pie_label_13 = cdlabels3
pie_value_13 = cdvalues3
pie_label_23 = lolabels3
pie_value_23 = lovalues3
cdays3 = cdates3
dic23 = dic13
return render_template('seg_charts.html', title='Segment Chart Description', max=13500,
#Segment 01
clabels=pie_label_1, cvalues = pie_value_1,
tlabels=bar_label_1, tvalues = bar_value_1,
smlabels=bar_label_2, smvalues = bar_value_2,
loylabels=pie_label_2, loyvalues = pie_value_2,
rolabels=bar_label_3, rovalues = bar_value_3,
flabels=hor_label_1, fvalues = hor_value_1,
cdays = cdays, dic3 = dic2,
#Segment 2
clabels2=pie_label_12, cvalues2=pie_value_12,
tlabels2=bar_label_12, tvalues2=bar_value_12,
smlabels2=bar_label_22, smvalues2=bar_value_22,
loylabels2=pie_label_22, loyvalues2=pie_value_22,
rolabels2=bar_label_32, rovalues2=bar_value_32,
flabels2=hor_label_12, fvalues2=hor_value_12,
cdays2=cdays2, dic32=dic22,
# Segment 03
clabels3=pie_label_13, cvalues3=pie_value_13,
tlabels3=bar_label_13, tvalues3=bar_value_13,
smlabels3=bar_label_23, smvalues3=bar_value_23,
loylabels3=pie_label_23, loyvalues3=pie_value_23,
rolabels3=bar_label_33, rovalues3=bar_value_33,
flabels3=hor_label_13, fvalues3=hor_value_13,
cdays3=cdays3, dic33=dic23,
)
def getLatestTime(year, month, day):
# get the newest date
newdate = dt.datetime(year, month, day)
return newdate
if __name__ == "__main__":
app.run(debug=True)