-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_models.py
More file actions
357 lines (288 loc) · 12 KB
/
ml_models.py
File metadata and controls
357 lines (288 loc) · 12 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
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
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
import joblib
import os
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import seaborn as sns
from io import BytesIO
import base64
class BookingPredictor:
def __init__(self):
self.model_path = 'instance/booking_predictor.joblib'
self.scaler_path = 'instance/booking_scaler.joblib'
self.model = None
self.scaler = None
# Try to load existing model
if os.path.exists(self.model_path) and os.path.exists(self.scaler_path):
self.load_model()
else:
# Initialize new model
self.model = RandomForestRegressor(n_estimators=100, random_state=42)
self.scaler = StandardScaler()
def load_model(self):
"""Load the trained model and scaler from disk"""
try:
self.model = joblib.load(self.model_path)
self.scaler = joblib.load(self.scaler_path)
return True
except Exception as e:
print(f"Error loading model: {e}")
return False
def save_model(self):
"""Save the trained model and scaler to disk"""
try:
joblib.dump(self.model, self.model_path)
joblib.dump(self.scaler, self.scaler_path)
return True
except Exception as e:
print(f"Error saving model: {e}")
return False
def prepare_data(self, bookings):
"""Convert booking data to features for prediction"""
if not bookings:
return None, None
# Convert bookings to DataFrame
data = []
for booking in bookings:
booking_date = datetime.strptime(booking['date'], '%Y-%m-%d')
booking_time = booking['time_slot'].split(' - ')[0]
hour = int(booking_time.split(':')[0])
# Extract features
day_of_week = booking_date.weekday()
month = booking_date.month
day = booking_date.day
data.append({
'day_of_week': day_of_week,
'month': month,
'day': day,
'hour': hour,
'booking_count': 1 # Each row represents one booking
})
df = pd.DataFrame(data)
# Aggregate by date and hour
features_df = df.groupby(['day_of_week', 'month', 'day', 'hour']).sum().reset_index()
X = features_df[['day_of_week', 'month', 'day', 'hour']]
y = features_df['booking_count']
return X, y
def train(self, bookings):
"""Train the model with booking data"""
X, y = self.prepare_data(bookings)
if X is None or len(X) < 5: # Need minimum data to train
return False
# Scale features
X_scaled = self.scaler.fit_transform(X)
# Train model
self.model.fit(X_scaled, y)
# Save model
self.save_model()
return True
def predict_future_bookings(self, days=14):
"""Predict bookings for the next N days"""
if self.model is None:
return None
# Generate future dates
future_dates = []
today = datetime.now()
# Create feature set for prediction
future_X = []
for i in range(days):
future_date = today + timedelta(days=i)
for hour in range(6, 23): # 6 AM to 10 PM
future_X.append([
future_date.weekday(), # day of week
future_date.month, # month
future_date.day, # day
hour # hour
])
future_dates.append(future_date.strftime('%Y-%m-%d') + f" {hour}:00")
future_X = np.array(future_X)
# Scale features
future_X_scaled = self.scaler.transform(future_X)
# Make predictions
predictions = self.model.predict(future_X_scaled)
# Round predictions and ensure non-negative
predictions = np.round(predictions).astype(int)
predictions = np.maximum(predictions, 0)
# Create result dictionary
result = {
'dates': future_dates,
'predictions': predictions.tolist()
}
return result
def generate_heatmap(self, days=7):
"""Generate a heatmap of predicted bookings"""
predictions = self.predict_future_bookings(days)
if not predictions:
return None
# Reshape data for heatmap
dates = [d.split(' ')[0] for d in predictions['dates']]
hours = [int(d.split(' ')[1].split(':')[0]) for d in predictions['dates']]
unique_dates = sorted(list(set(dates)))
unique_hours = sorted(list(set(hours)))
# Create matrix for heatmap
heatmap_data = np.zeros((len(unique_hours), len(unique_dates)))
for i, (date, hour, pred) in enumerate(zip(dates, hours, predictions['predictions'])):
date_idx = unique_dates.index(date)
hour_idx = unique_hours.index(hour)
heatmap_data[hour_idx, date_idx] = pred
# Generate plot
plt.figure(figsize=(12, 8))
sns.heatmap(heatmap_data, annot=True, fmt='d', cmap='YlGnBu',
xticklabels=[d.split('-')[2] + '/' + d.split('-')[1] for d in unique_dates],
yticklabels=[f"{h}:00" for h in unique_hours])
plt.title('Predicted Booking Heatmap')
plt.xlabel('Date')
plt.ylabel('Hour')
plt.tight_layout()
# Convert plot to base64 for embedding in HTML
buffer = BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
image_png = buffer.getvalue()
buffer.close()
graphic = base64.b64encode(image_png).decode('utf-8')
return graphic
def get_popular_times(self):
"""Identify the most popular booking times"""
predictions = self.predict_future_bookings()
if not predictions:
return None
# Convert to DataFrame for analysis
df = pd.DataFrame({
'date': predictions['dates'],
'prediction': predictions['predictions']
})
# Extract day of week and hour
df['date_obj'] = pd.to_datetime(df['date'].str.split(' ').str[0])
df['hour'] = df['date'].str.split(' ').str[1].str.split(':').str[0].astype(int)
df['day_of_week'] = df['date_obj'].dt.dayofweek
# Get average bookings by day of week
day_avg = df.groupby('day_of_week')['prediction'].mean().reset_index()
day_avg = day_avg.sort_values('prediction', ascending=False)
# Get average bookings by hour
hour_avg = df.groupby('hour')['prediction'].mean().reset_index()
hour_avg = hour_avg.sort_values('prediction', ascending=False)
# Map day of week to name
day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day_avg['day_name'] = day_avg['day_of_week'].apply(lambda x: day_names[x])
return {
'popular_days': day_avg[['day_name', 'prediction']].head(3).to_dict('records'),
'popular_hours': hour_avg[['hour', 'prediction']].head(3).to_dict('records')
}
class RevenuePredictor:
def __init__(self):
self.model_path = 'instance/revenue_predictor.joblib'
self.model = None
# Try to load existing model
if os.path.exists(self.model_path):
self.load_model()
else:
# Initialize new model
self.model = LinearRegression()
def load_model(self):
"""Load the trained model from disk"""
try:
self.model = joblib.load(self.model_path)
return True
except Exception as e:
print(f"Error loading model: {e}")
return False
def save_model(self):
"""Save the trained model to disk"""
try:
joblib.dump(self.model, self.model_path)
return True
except Exception as e:
print(f"Error saving model: {e}")
return False
def prepare_data(self, bookings, booking_price=50):
"""Convert booking data to features for prediction"""
if not bookings:
return None, None
# Convert bookings to DataFrame
data = []
for booking in bookings:
booking_date = datetime.strptime(booking['date'], '%Y-%m-%d')
# Extract features
day_of_week = booking_date.weekday()
month = booking_date.month
data.append({
'day_of_week': day_of_week,
'month': month,
'revenue': booking_price # Each booking contributes to revenue
})
df = pd.DataFrame(data)
# Aggregate by date
df['date_key'] = df['month'].astype(str) + '_' + df['day_of_week'].astype(str)
revenue_by_date = df.groupby('date_key').agg({
'day_of_week': 'first',
'month': 'first',
'revenue': 'sum'
}).reset_index()
X = revenue_by_date[['day_of_week', 'month']]
y = revenue_by_date['revenue']
return X, y
def train(self, bookings, booking_price=50):
"""Train the model with booking data"""
X, y = self.prepare_data(bookings, booking_price)
if X is None or len(X) < 5: # Need minimum data to train
return False
# Train model
self.model.fit(X, y)
# Save model
self.save_model()
return True
def predict_future_revenue(self, days=30, booking_price=50):
"""Predict revenue for the next N days"""
if self.model is None:
return None
# Generate future dates
future_dates = []
future_X = []
today = datetime.now()
for i in range(days):
future_date = today + timedelta(days=i)
future_dates.append(future_date.strftime('%Y-%m-%d'))
future_X.append([
future_date.weekday(), # day of week
future_date.month # month
])
future_X = np.array(future_X)
# Make predictions
predictions = self.model.predict(future_X)
# Ensure non-negative
predictions = np.maximum(predictions, 0)
# Create result dictionary
result = {
'dates': future_dates,
'predictions': predictions.tolist()
}
return result
def generate_revenue_chart(self, days=30, booking_price=50):
"""Generate a chart of predicted revenue"""
predictions = self.predict_future_revenue(days, booking_price)
if not predictions:
return None
# Generate plot
plt.figure(figsize=(12, 6))
plt.plot(range(len(predictions['dates'])), predictions['predictions'], marker='o')
plt.title('Predicted Revenue for Next 30 Days')
plt.xlabel('Days from Now')
plt.ylabel('Predicted Revenue ($)')
plt.grid(True, linestyle='--', alpha=0.7)
# Add date labels for every 5th day
tick_indices = range(0, len(predictions['dates']), 5)
plt.xticks(tick_indices, [predictions['dates'][i] for i in tick_indices], rotation=45)
plt.tight_layout()
# Convert plot to base64 for embedding in HTML
buffer = BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
image_png = buffer.getvalue()
buffer.close()
graphic = base64.b64encode(image_png).decode('utf-8')
return graphic