-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
38 lines (29 loc) · 1.47 KB
/
Copy pathtrain_model.py
File metadata and controls
38 lines (29 loc) · 1.47 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
# train_model.py
import numpy as np
from lightgbm import LGBMRegressor
from feature_engineering import extract_and_engineer_features
def run_model_forecasting():
df = extract_and_engineer_features()
# Select features for machine learning
features = ['day_of_week', 'month', 'is_weekend', 'sales_lag_7', 'sales_lag_14', 'rolling_mean_7', 'rolling_mean_14']
target = 'units_sold'
# Split into train set and validation set (most recent 30 days)
train_df = df.iloc[:-30]
val_df = df.iloc[-30:]
X_train, y_train = train_df[features], train_df[target]
X_val, y_val = val_df[features], val_df[target]
# Initialize and train LightGBM model
model = LGBMRegressor(n_estimators=100, learning_rate=0.05, random_state=42, verbose=-1)
model.fit(X_train, y_train)
# Evaluate performance accuracy
predictions = model.predict(X_val)
mape = np.mean(np.abs((y_val - predictions) / y_val)) * 100
accuracy = 100 - mape
print(f"📊 Model Training Performance: Model Accuracy is {accuracy:.2f}%")
# Calculate stable upcoming 14-day demand forecast window
avg_predicted_daily_sales = model.predict(df[features].tail(14)).mean()
total_projected_14_day_demand = int(avg_predicted_daily_sales * 14)
print(f"🔮 Forecast Phase Complete: Projected 14-day sales volume is {total_projected_14_day_demand} units.")
return total_projected_14_day_demand
if __name__ == '__main__':
run_model_forecasting()