Spaces:
Running
Running
Commit
·
0aa2056
1
Parent(s):
b470a83
Update app
Browse files
app.py
CHANGED
|
@@ -112,8 +112,7 @@ def series_to_df_exogenous(series):
|
|
| 112 |
return dataframe
|
| 113 |
|
| 114 |
@st.cache_data
|
| 115 |
-
def
|
| 116 |
-
dataframe = series.to_frame()
|
| 117 |
dataframe = dataframe.reset_index()
|
| 118 |
dataframe['Date'] = dataframe['Date'].dt.strftime('%B %d, %Y')
|
| 119 |
dataframe[df.columns] = dataframe[df.columns].astype(str)
|
|
@@ -263,7 +262,6 @@ with st.sidebar:
|
|
| 263 |
st.write("Your uploaded data:")
|
| 264 |
st.write(df)
|
| 265 |
|
| 266 |
-
# Data pre-processing
|
| 267 |
df = drop(df)
|
| 268 |
df = date_format(df)
|
| 269 |
merge_sort(df)
|
|
@@ -274,7 +272,7 @@ with st.sidebar:
|
|
| 274 |
st.download_button("Download our sample CSV", f, file_name='sample.csv')
|
| 275 |
|
| 276 |
if (st.session_state.uploaded):
|
| 277 |
-
st.line_chart(series)
|
| 278 |
|
| 279 |
MIN_DAYS = 30
|
| 280 |
MAX_DAYS = 90
|
|
@@ -289,73 +287,37 @@ if (st.session_state.uploaded):
|
|
| 289 |
if (forecast_button):
|
| 290 |
df = series_to_df_exogenous(series)
|
| 291 |
|
| 292 |
-
|
| 293 |
-
training_y, test_y, test_y_series, training_X, test_X, future_X =
|
| 294 |
-
|
| 295 |
-
# Auto_arima to fit the model to forecast future sales
|
| 296 |
-
future_model = model_fitting(df, future_X)
|
| 297 |
-
# Auto_arima to check the accuracy of the train test split
|
| 298 |
train_test_model = test_fitting(df, training_X, training_y)
|
| 299 |
|
| 300 |
-
# Forecast (testing)
|
| 301 |
n_periods = round(len(df) * 0.2)
|
|
|
|
|
|
|
| 302 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
|
|
|
| 303 |
index_of_fc = test_y_series.index
|
| 304 |
|
| 305 |
# make series for plotting purpose
|
| 306 |
fitted_series = pd.Series(fitted)
|
| 307 |
-
fitted_series.index
|
| 308 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 309 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
# plt.figure(figsize=(12,8))
|
| 316 |
-
# plt.plot(training_y[-80:])
|
| 317 |
-
# plt.plot(test_y, color = 'red', label = 'Actual Sales')
|
| 318 |
-
# plt.plot(fitted_series, color='darkgreen', label = 'Predicted Sales')
|
| 319 |
-
# plt.fill_between(lower_series.index,
|
| 320 |
-
# lower_series,
|
| 321 |
-
# upper_series,
|
| 322 |
-
# color='k', alpha=.15)
|
| 323 |
-
|
| 324 |
-
# plt.title("SARIMAX - Forecast of Retail Sales VS Actual Sales")
|
| 325 |
-
# plt.legend(loc='upper left', fontsize=8)
|
| 326 |
-
# plt.show()
|
| 327 |
|
| 328 |
-
#
|
| 329 |
-
|
| 330 |
-
# Forecast (actual)
|
| 331 |
-
n_periods = forecast_period
|
| 332 |
-
freq='3D'
|
| 333 |
-
future_fitted, confint = future_model.predict(X=df.iloc[-n_periods:,1:], n_periods=n_periods, return_conf_int=True, freq=freq)
|
| 334 |
-
future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods = n_periods, freq=freq)
|
| 335 |
-
|
| 336 |
-
# make series for plotting purpose
|
| 337 |
future_fitted_series = pd.Series(future_fitted)
|
| 338 |
-
future_fitted_series.index=future_index_of_fc
|
| 339 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 340 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 341 |
|
| 342 |
-
|
| 343 |
-
# plt.figure(figsize=(12,8))
|
| 344 |
-
# plt.plot(df['Sales'][-50:])
|
| 345 |
-
# plt.plot(future_fitted_series, color='darkgreen')
|
| 346 |
-
# plt.fill_between(future_lower_series.index,
|
| 347 |
-
# future_lower_series,
|
| 348 |
-
# future_upper_series,
|
| 349 |
-
# color='k', alpha=.15)
|
| 350 |
-
|
| 351 |
-
# plt.title("SARIMA - Final Forecast of Retail Sales")
|
| 352 |
-
# plt.show()
|
| 353 |
-
|
| 354 |
-
# Create traces for each line and fill_between
|
| 355 |
-
|
| 356 |
-
auto_sales_growth = sales_growth(df, future_fitted_series)
|
| 357 |
|
| 358 |
-
df =
|
| 359 |
st.write("Forecasted sales in the next 3 months")
|
| 360 |
st.write(df)
|
| 361 |
|
|
@@ -363,6 +325,6 @@ if (st.session_state.uploaded):
|
|
| 363 |
question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
|
| 364 |
query_button = st.form_submit_button(label='Generate Answer')
|
| 365 |
|
| 366 |
-
if query_button:
|
| 367 |
answer = get_converted_answer(df, question)
|
| 368 |
st.write("The answer is:", answer)
|
|
|
|
| 112 |
return dataframe
|
| 113 |
|
| 114 |
@st.cache_data
|
| 115 |
+
def dates_df(dataframe):
|
|
|
|
| 116 |
dataframe = dataframe.reset_index()
|
| 117 |
dataframe['Date'] = dataframe['Date'].dt.strftime('%B %d, %Y')
|
| 118 |
dataframe[df.columns] = dataframe[df.columns].astype(str)
|
|
|
|
| 262 |
st.write("Your uploaded data:")
|
| 263 |
st.write(df)
|
| 264 |
|
|
|
|
| 265 |
df = drop(df)
|
| 266 |
df = date_format(df)
|
| 267 |
merge_sort(df)
|
|
|
|
| 272 |
st.download_button("Download our sample CSV", f, file_name='sample.csv')
|
| 273 |
|
| 274 |
if (st.session_state.uploaded):
|
| 275 |
+
st.line_chart(series)
|
| 276 |
|
| 277 |
MIN_DAYS = 30
|
| 278 |
MAX_DAYS = 90
|
|
|
|
| 287 |
if (forecast_button):
|
| 288 |
df = series_to_df_exogenous(series)
|
| 289 |
|
| 290 |
+
train = train_test(df)
|
| 291 |
+
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
train_test_model = test_fitting(df, training_X, training_y)
|
| 293 |
|
|
|
|
| 294 |
n_periods = round(len(df) * 0.2)
|
| 295 |
+
future_n_periods = forecast_period + n_periods
|
| 296 |
+
|
| 297 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
| 298 |
+
|
| 299 |
index_of_fc = test_y_series.index
|
| 300 |
|
| 301 |
# make series for plotting purpose
|
| 302 |
fitted_series = pd.Series(fitted)
|
| 303 |
+
fitted_series.index=index_of_fc
|
| 304 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 305 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 306 |
|
| 307 |
+
#Future predictions
|
| 308 |
+
frequency = '3D'
|
| 309 |
+
future_fitted, confint = train_test_model.predict(X=df.iloc[-future_n_periods:,1:], n_periods=future_n_periods, return_conf_int=True, freq=frequency)
|
| 310 |
+
future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods = future_n_periods, freq=frequency)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
# make series for future plotting purpose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
future_fitted_series = pd.Series(future_fitted)
|
| 314 |
+
future_fitted_series.index = future_index_of_fc
|
| 315 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 316 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 317 |
|
| 318 |
+
future_sales_growth = sales_growth(df, future_fitted_series)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
+
df = dates_df(future_sales_growth)
|
| 321 |
st.write("Forecasted sales in the next 3 months")
|
| 322 |
st.write(df)
|
| 323 |
|
|
|
|
| 325 |
question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
|
| 326 |
query_button = st.form_submit_button(label='Generate Answer')
|
| 327 |
|
| 328 |
+
if query_button or question:
|
| 329 |
answer = get_converted_answer(df, question)
|
| 330 |
st.write("The answer is:", answer)
|