Spaces:
Running
Running
Commit
Β·
d7ac35b
1
Parent(s):
ab69cc2
Update app
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from datetime import datetime
|
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
import pmdarima as pm
|
|
|
|
| 7 |
from pmdarima import auto_arima
|
| 8 |
|
| 9 |
import torch
|
|
@@ -92,7 +93,8 @@ def group_to_three(dataframe):
|
|
| 92 |
return dataframe
|
| 93 |
|
| 94 |
# SARIMAX Model
|
| 95 |
-
def train_test(dataframe
|
|
|
|
| 96 |
training_y = dataframe.iloc[:-n,0]
|
| 97 |
test_y = dataframe.iloc[-n:,0]
|
| 98 |
test_y_series = pd.Series(test_y, index=dataframe.iloc[-n:, 0].index)
|
|
@@ -101,6 +103,7 @@ def train_test(dataframe, n):
|
|
| 101 |
future_X = dataframe.iloc[0:,1:]
|
| 102 |
return (training_y, test_y, test_y_series, training_X, test_X, future_X)
|
| 103 |
|
|
|
|
| 104 |
def model_fitting(dataframe, Exo):
|
| 105 |
futureModel = pm.auto_arima(dataframe['Sales'], X=Exo, start_p=1, start_q=1,
|
| 106 |
test='adf',min_p=1,min_q=1,
|
|
@@ -200,7 +203,7 @@ def get_converted_answer(table, query):
|
|
| 200 |
# Web Application
|
| 201 |
|
| 202 |
st.title("Sales Forecasting Dashboard π")
|
| 203 |
-
st.
|
| 204 |
|
| 205 |
# Session States
|
| 206 |
if 'uploaded' not in st.session_state:
|
|
@@ -233,6 +236,9 @@ with st.sidebar:
|
|
| 233 |
if (st.session_state.uploaded):
|
| 234 |
st.line_chart(df)
|
| 235 |
|
|
|
|
|
|
|
|
|
|
| 236 |
forecast_button = st.button(
|
| 237 |
'Start Forecasting',
|
| 238 |
key='forecast_button',
|
|
@@ -259,7 +265,7 @@ if (st.session_state.uploaded):
|
|
| 259 |
train_test_model = test_fitting(df, training_X, training_y)
|
| 260 |
|
| 261 |
# Forecast (testing)
|
| 262 |
-
n_periods =
|
| 263 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
| 264 |
index_of_fc = test_y_series.index
|
| 265 |
|
|
@@ -272,8 +278,23 @@ if (st.session_state.uploaded):
|
|
| 272 |
test_y, predictions = np.array(test_y), np.array(fitted)
|
| 273 |
forecast_accuracy(predictions, test_y)
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
# Forecast (actual)
|
| 276 |
-
n_periods =
|
| 277 |
freq='3D'
|
| 278 |
future_fitted, confint = future_model.predict(X=df.iloc[-n_periods:,1:], n_periods=n_periods, return_conf_int=True, freq=freq)
|
| 279 |
future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods = n_periods, freq=freq)
|
|
@@ -284,12 +305,17 @@ if (st.session_state.uploaded):
|
|
| 284 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 285 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
# st.line_chart(df['Sales'][-50:])
|
| 291 |
-
# st.line_chart(future_fitted_series, use_container_width=True)
|
| 292 |
-
# st.area_chart(pd.concat([future_lower_series, future_upper_series], axis=1), color="#808080")
|
| 293 |
|
| 294 |
auto_sales_growth = sales_growth(df, future_fitted_series)
|
| 295 |
df = auto_sales_growth
|
|
@@ -300,10 +326,7 @@ if (st.session_state.uploaded):
|
|
| 300 |
st.write("Forecasted sales in the next 3 months")
|
| 301 |
st.write(df)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
query_button = st.form_submit_button(label='Generate Answer')
|
| 306 |
-
|
| 307 |
-
if query_button:
|
| 308 |
answer = get_converted_answer(df, question)
|
| 309 |
st.write("The answer is:", answer)
|
|
|
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
import pmdarima as pm
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
from pmdarima import auto_arima
|
| 9 |
|
| 10 |
import torch
|
|
|
|
| 93 |
return dataframe
|
| 94 |
|
| 95 |
# SARIMAX Model
|
| 96 |
+
def train_test(dataframe):
|
| 97 |
+
n = round(len(dataframe) * 0.2)
|
| 98 |
training_y = dataframe.iloc[:-n,0]
|
| 99 |
test_y = dataframe.iloc[-n:,0]
|
| 100 |
test_y_series = pd.Series(test_y, index=dataframe.iloc[-n:, 0].index)
|
|
|
|
| 103 |
future_X = dataframe.iloc[0:,1:]
|
| 104 |
return (training_y, test_y, test_y_series, training_X, test_X, future_X)
|
| 105 |
|
| 106 |
+
|
| 107 |
def model_fitting(dataframe, Exo):
|
| 108 |
futureModel = pm.auto_arima(dataframe['Sales'], X=Exo, start_p=1, start_q=1,
|
| 109 |
test='adf',min_p=1,min_q=1,
|
|
|
|
| 203 |
# Web Application
|
| 204 |
|
| 205 |
st.title("Sales Forecasting Dashboard π")
|
| 206 |
+
st.subheader("Welcome User, start using the application by uploading your file in the sidebar!")
|
| 207 |
|
| 208 |
# Session States
|
| 209 |
if 'uploaded' not in st.session_state:
|
|
|
|
| 236 |
if (st.session_state.uploaded):
|
| 237 |
st.line_chart(df)
|
| 238 |
|
| 239 |
+
period = st.slider('How many days would you like to forecast?', min_value=30, max_value=90)
|
| 240 |
+
forecast_period = round(period / 3)
|
| 241 |
+
|
| 242 |
forecast_button = st.button(
|
| 243 |
'Start Forecasting',
|
| 244 |
key='forecast_button',
|
|
|
|
| 265 |
train_test_model = test_fitting(df, training_X, training_y)
|
| 266 |
|
| 267 |
# Forecast (testing)
|
| 268 |
+
n_periods = round(len(df) * 0.2)
|
| 269 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
| 270 |
index_of_fc = test_y_series.index
|
| 271 |
|
|
|
|
| 278 |
test_y, predictions = np.array(test_y), np.array(fitted)
|
| 279 |
forecast_accuracy(predictions, test_y)
|
| 280 |
|
| 281 |
+
st.title("Forecasted Sales")
|
| 282 |
+
|
| 283 |
+
plt.figure(figsize=(12,8))
|
| 284 |
+
plt.plot(training_y[-80:])
|
| 285 |
+
plt.plot(test_y, color = 'red', label = 'Actual Sales')
|
| 286 |
+
plt.plot(fitted_series, color='darkgreen', label = 'Predicted Sales')
|
| 287 |
+
plt.fill_between(lower_series.index,
|
| 288 |
+
lower_series,
|
| 289 |
+
upper_series,
|
| 290 |
+
color='k', alpha=.15)
|
| 291 |
+
|
| 292 |
+
plt.title("SARIMAX - Forecast of Retail Sales VS Actual Sales")
|
| 293 |
+
plt.legend(loc='upper left', fontsize=8)
|
| 294 |
+
plt.show()
|
| 295 |
+
|
| 296 |
# Forecast (actual)
|
| 297 |
+
n_periods = forecast_period
|
| 298 |
freq='3D'
|
| 299 |
future_fitted, confint = future_model.predict(X=df.iloc[-n_periods:,1:], n_periods=n_periods, return_conf_int=True, freq=freq)
|
| 300 |
future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods = n_periods, freq=freq)
|
|
|
|
| 305 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 306 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 307 |
|
| 308 |
+
# Plot
|
| 309 |
+
plt.figure(figsize=(12,8))
|
| 310 |
+
plt.plot(df['Sales'][-50:])
|
| 311 |
+
plt.plot(future_fitted_series, color='darkgreen')
|
| 312 |
+
plt.fill_between(future_lower_series.index,
|
| 313 |
+
future_lower_series,
|
| 314 |
+
future_upper_series,
|
| 315 |
+
color='k', alpha=.15)
|
| 316 |
|
| 317 |
+
plt.title("SARIMA - Final Forecast of Retail Sales")
|
| 318 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
auto_sales_growth = sales_growth(df, future_fitted_series)
|
| 321 |
df = auto_sales_growth
|
|
|
|
| 326 |
st.write("Forecasted sales in the next 3 months")
|
| 327 |
st.write(df)
|
| 328 |
|
| 329 |
+
question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
|
| 330 |
+
if question:
|
|
|
|
|
|
|
|
|
|
| 331 |
answer = get_converted_answer(df, question)
|
| 332 |
st.write("The answer is:", answer)
|