Spaces:
Running
Running
Circhastic
commited on
Commit
·
6812a18
1
Parent(s):
e788eaa
Version 1 hotfix #11 for merge
Browse files
app.py
CHANGED
@@ -180,18 +180,19 @@ def sales_growth(dataframe, fittedValues):
|
|
180 |
return sales_growth
|
181 |
|
182 |
@st.cache_data
|
183 |
-
def merge_forecast_data(
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
188 |
future = future.to_frame()
|
|
|
189 |
future.rename(columns={future.columns[0]: "Future Forecasted Sales"}, inplace=True)
|
190 |
-
historical.head()
|
191 |
-
test.head()
|
192 |
-
future.head()
|
193 |
-
merged_dataframe = pd.merge_ordered(historical, pd.merge_ordered(test, future, fill_method="ffill", left_by="Predicted Sales"), fill_method="ffill", left_by="Actual Sales")
|
194 |
|
|
|
|
|
195 |
return merged_dataframe
|
196 |
|
197 |
# TAPAS Model
|
@@ -300,6 +301,7 @@ if (st.session_state.uploaded):
|
|
300 |
train = train_test(df)
|
301 |
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
302 |
train_test_model = test_fitting(df, training_X, training_y)
|
|
|
303 |
n_periods = round(len(df) * 0.2)
|
304 |
future_n_periods = forecast_period + n_periods
|
305 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
@@ -341,19 +343,21 @@ if (st.session_state.uploaded):
|
|
341 |
future_sales_growth = sales_growth(df, future_fitted_series)
|
342 |
future_sales_growth = future_sales_growth.iloc[n_periods:]
|
343 |
df = dates_df(future_sales_growth)
|
|
|
|
|
|
|
344 |
|
345 |
col = st.columns(2)
|
346 |
with col[0]:
|
347 |
col[0].header("Sales Forecast")
|
348 |
merged_data = merge_forecast_data(df['Sales'], fitted_series, future_fitted_series)
|
349 |
col[0].line_chart(merged_data)
|
|
|
350 |
with col[1]:
|
351 |
col[1].subheader(f"Forecasted sales in the next {period} days")
|
352 |
col[1].write(df)
|
353 |
st.session_state.forecasted = True
|
354 |
|
355 |
-
test_y, predictions = np.array(test_y), np.array(fitted)
|
356 |
-
forecast_accuracy(predictions, test_y)
|
357 |
|
358 |
with st.form("question_form"):
|
359 |
question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
|
|
|
180 |
return sales_growth
|
181 |
|
182 |
@st.cache_data
|
183 |
+
def merge_forecast_data(actual, predicted, future):
|
184 |
+
actual = actual.to_frame()
|
185 |
+
actual.rename(columns={actual.columns[0]: "Actual Sales"}, inplace=True)
|
186 |
+
|
187 |
+
predicted = predicted.to_frame()
|
188 |
+
predicted.rename(columns={predicted.columns[0]: "Predicted Sales"}, inplace=True)
|
189 |
+
|
190 |
future = future.to_frame()
|
191 |
+
future = future.rename_axis('Date')
|
192 |
future.rename(columns={future.columns[0]: "Future Forecasted Sales"}, inplace=True)
|
|
|
|
|
|
|
|
|
193 |
|
194 |
+
merged_dataframe = pd.merge_ordered(actual, predicted, on='Date', suffixes=('_Actual', '_Predicted'))
|
195 |
+
merged_dataframe = pd.merge_ordered(merged_dataframe, future, on='Date')
|
196 |
return merged_dataframe
|
197 |
|
198 |
# TAPAS Model
|
|
|
301 |
train = train_test(df)
|
302 |
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
303 |
train_test_model = test_fitting(df, training_X, training_y)
|
304 |
+
|
305 |
n_periods = round(len(df) * 0.2)
|
306 |
future_n_periods = forecast_period + n_periods
|
307 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
|
|
343 |
future_sales_growth = sales_growth(df, future_fitted_series)
|
344 |
future_sales_growth = future_sales_growth.iloc[n_periods:]
|
345 |
df = dates_df(future_sales_growth)
|
346 |
+
|
347 |
+
test_y, predictions = np.array(test_y), np.array(fitted)
|
348 |
+
acc = forecast_accuracy(predictions, test_y)
|
349 |
|
350 |
col = st.columns(2)
|
351 |
with col[0]:
|
352 |
col[0].header("Sales Forecast")
|
353 |
merged_data = merge_forecast_data(df['Sales'], fitted_series, future_fitted_series)
|
354 |
col[0].line_chart(merged_data)
|
355 |
+
col[0].write(f"MAPE score: {acc['mape']} (lower is better)")
|
356 |
with col[1]:
|
357 |
col[1].subheader(f"Forecasted sales in the next {period} days")
|
358 |
col[1].write(df)
|
359 |
st.session_state.forecasted = True
|
360 |
|
|
|
|
|
361 |
|
362 |
with st.form("question_form"):
|
363 |
question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
|