Prajwal3009 commited on
Commit
83ecf66
·
verified ·
1 Parent(s): b57abf9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import pickle
6
+ import os
7
+ from statsmodels.tsa.arima.model import ARIMA
8
+ from sklearn.tree import DecisionTreeRegressor
9
+ from xgboost import XGBRegressor
10
+ st.set_option('deprecation.showPyplotGlobalUse', False)
11
+ # Paths for model directories
12
+ arima_models_folder = 'models/arima_Models'
13
+ dt_models_folder = 'models/decision_tree_Models'
14
+ xgb_models_folder = 'models/xgb_Models'
15
+
16
+ merged_data = pd.read_csv('./data/merged_data.csv')
17
+
18
+ # Prepare weekly sales data for top 10 products
19
+ top_10_products = merged_data.groupby('StockCode')['Quantity'].sum().nlargest(10).index
20
+ weekly_sales = merged_data.groupby(['StockCode', 'Year', 'Week'])['Quantity'].sum().reset_index()
21
+
22
+ # Load product codes (as an example, you can modify this to load from your source)
23
+ product_codes = ['85123A', '85099B', '22197', '84879', '23084',
24
+ '21181', '22423', '21212', '20713', '21915']
25
+
26
+ # Title of the app
27
+ st.title("Sales Forecasting with ARIMA, Decision Tree, and XGBoost")
28
+
29
+ # Model selection dropdown
30
+ model_option = st.selectbox("Select Model", ["ARIMA", "Decision Tree", "XGBoost"])
31
+
32
+ # Product selection dropdown
33
+ selected_product = st.selectbox("Select Product", product_codes)
34
+
35
+ # Initialize session state to store forecasts
36
+ if 'arima_forecast' not in st.session_state:
37
+ st.session_state.arima_forecast = None
38
+ if 'dt_forecast' not in st.session_state:
39
+ st.session_state.dt_forecast = None
40
+ if 'xgb_forecast' not in st.session_state:
41
+ st.session_state.xgb_forecast = None
42
+
43
+ # Forecasting button
44
+ if st.button("Forecast"):
45
+ if model_option == "ARIMA":
46
+ # Load ARIMA model
47
+ model_filename = os.path.join(arima_models_folder, f'arima_model_product_{selected_product}.pkl')
48
+ with open(model_filename, 'rb') as file:
49
+ arima_model = pickle.load(file)
50
+
51
+ # Forecasting with ARIMA
52
+ st.session_state.arima_forecast = arima_model.forecast(steps=15)
53
+ st.write(f"ARIMA Forecast for Product {selected_product}:")
54
+ st.write(st.session_state.arima_forecast)
55
+
56
+ # Plot ARIMA results
57
+ plt.figure(figsize=(10, 6))
58
+ plt.plot(st.session_state.arima_forecast, label='ARIMA Forecast', color='blue')
59
+ plt.title(f'ARIMA Forecast for Product {selected_product}')
60
+ plt.xlabel('Weeks')
61
+ plt.ylabel('Sales Quantity')
62
+ plt.legend()
63
+ st.pyplot()
64
+
65
+ elif model_option == "Decision Tree":
66
+ # Load Decision Tree model
67
+ dt_model_filename = os.path.join(dt_models_folder, f'decision_tree_model_product_{selected_product}.pkl')
68
+ with open(dt_model_filename, 'rb') as file:
69
+ dt_model = pickle.load(file)
70
+
71
+ # Prepare input data for Decision Tree
72
+ future_weeks = np.array([[2023, week] for week in range(40, 55)]) # Future weeks for prediction
73
+ st.session_state.dt_forecast = dt_model.predict(future_weeks)
74
+
75
+ st.write(f"Decision Tree Forecast for Product {selected_product}:")
76
+ st.write(st.session_state.dt_forecast)
77
+
78
+ # Plot Decision Tree results
79
+ plt.figure(figsize=(10, 6))
80
+ plt.plot(st.session_state.dt_forecast, label='Decision Tree Forecast', color='green')
81
+ plt.title(f'Decision Tree Forecast for Product {selected_product}')
82
+ plt.xlabel('Weeks')
83
+ plt.ylabel('Sales Quantity')
84
+ plt.legend()
85
+ st.pyplot()
86
+
87
+ elif model_option == "XGBoost":
88
+ # Load XGBoost model
89
+ xgb_model_filename = os.path.join(xgb_models_folder, f'xgb_model_product_{selected_product}.pkl')
90
+ with open(xgb_model_filename, 'rb') as file:
91
+ xgb_model = pickle.load(file)
92
+
93
+ # Prepare input data for XGBoost
94
+ future_weeks = np.array([[2023, week] for week in range(40, 55)]) # Future weeks for prediction
95
+ st.session_state.xgb_forecast = xgb_model.predict(future_weeks)
96
+
97
+ st.write(f"XGBoost Forecast for Product {selected_product}:")
98
+ st.write(st.session_state.xgb_forecast)
99
+
100
+ # Plot XGBoost results
101
+ plt.figure(figsize=(10, 6))
102
+ plt.plot(st.session_state.xgb_forecast, label='XGBoost Forecast', color='orange')
103
+ plt.title(f'XGBoost Forecast for Product {selected_product}')
104
+ plt.xlabel('Weeks')
105
+ plt.ylabel('Sales Quantity')
106
+ plt.legend()
107
+ st.pyplot()
108
+
109
+ # Comparison button
110
+ if st.button("Compare Models"):
111
+ # Load and forecast with ARIMA
112
+ model_filename_arima = os.path.join(arima_models_folder, f'arima_model_product_{selected_product}.pkl')
113
+ with open(model_filename_arima, 'rb') as file:
114
+ arima_model = pickle.load(file)
115
+
116
+ # Prepare input data for ARIMA (you may need to adjust this based on your data structure)
117
+ product_sales_series = weekly_sales[weekly_sales['StockCode'] == selected_product].set_index(['Year', 'Week'])['Quantity']
118
+ arima_result = ARIMA(product_sales_series, order=(1, 1, 1)).fit()
119
+ st.session_state.arima_forecast = arima_result.forecast(steps=15)
120
+
121
+ # Load and forecast with Decision Tree
122
+ dt_model_filename = os.path.join(dt_models_folder, f'decision_tree_model_product_{selected_product}.pkl')
123
+ with open(dt_model_filename, 'rb') as file:
124
+ dt_model = pickle.load(file)
125
+
126
+ # Prepare input data for Decision Tree
127
+ future_data = np.array([[2023, week] for week in range(40, 55)]) # Adjust weeks based on your need
128
+ st.session_state.dt_forecast = dt_model.predict(future_data)
129
+
130
+ # Load and forecast with XGBoost
131
+ xgb_model_filename = os.path.join(xgb_models_folder, f'xgb_model_product_{selected_product}.pkl')
132
+ with open(xgb_model_filename, 'rb') as file:
133
+ xgb_model = pickle.load(file)
134
+
135
+ # Forecast using XGBoost
136
+ st.session_state.xgb_forecast = xgb_model.predict(future_data)
137
+
138
+ # Now plot all the forecasts
139
+ plt.figure(figsize=(10, 6))
140
+
141
+ # ARIMA forecast
142
+ if st.session_state.arima_forecast is not None:
143
+ plt.plot(st.session_state.arima_forecast, label='ARIMA Forecast', color='blue')
144
+
145
+ # Decision Tree forecast
146
+ if st.session_state.dt_forecast is not None:
147
+ plt.plot(st.session_state.dt_forecast, label='Decision Tree Forecast', color='green')
148
+
149
+ # XGBoost forecast
150
+ if st.session_state.xgb_forecast is not None:
151
+ plt.plot(st.session_state.xgb_forecast, label='XGBoost Forecast', color='orange')
152
+
153
+ plt.title(f'Comparison of Forecasts for Product {selected_product}')
154
+ plt.xlabel('Weeks')
155
+ plt.ylabel('Sales Quantity')
156
+ plt.legend()
157
+ st.pyplot()
158
+