Spaces:
Runtime error
Runtime error
Commit
·
20c54a1
1
Parent(s):
08ca89b
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,7 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objs as go
|
| 6 |
-
from
|
| 7 |
-
from statsforecast.models import auto_arima
|
| 8 |
from utils import calc_seasonality
|
| 9 |
|
| 10 |
@st.cache
|
|
@@ -19,9 +18,9 @@ if uploaded_file is not None:
|
|
| 19 |
st.write(dataframe)
|
| 20 |
series = st.text_input("Write the name of the variable you want to forecast")
|
| 21 |
date = st.text_input('Write the first date')
|
| 22 |
-
#
|
| 23 |
freq = st.selectbox('Select the frequency',
|
| 24 |
-
('Y', 'Q', 'M', 'W', 'D'))
|
| 25 |
|
| 26 |
if series is not None and freq is not None:
|
| 27 |
|
|
@@ -31,27 +30,43 @@ if uploaded_file is not None:
|
|
| 31 |
|
| 32 |
|
| 33 |
series_train = pd.DataFrame(
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
fig = px.line(series_train, x='ds', y='y', title=f'{series}')
|
| 42 |
st.plotly_chart(fig)
|
| 43 |
with st.spinner('Wait for it...'):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
st.success('Done!')
|
| 52 |
|
| 53 |
st.write(forecasts)
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
csv = convert_df(forecasts)
|
| 56 |
|
| 57 |
st.download_button(
|
|
@@ -74,7 +89,7 @@ if uploaded_file is not None:
|
|
| 74 |
go.Scatter(
|
| 75 |
name=f'auto_arima_season_length-{seasonality}_mean',
|
| 76 |
x=forecasts['ds'],
|
| 77 |
-
y=forecasts[
|
| 78 |
mode='lines',
|
| 79 |
marker=dict(color='green'),
|
| 80 |
line=dict(width=1),
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objs as go
|
| 6 |
+
from pmdarima import auto_arima
|
|
|
|
| 7 |
from utils import calc_seasonality
|
| 8 |
|
| 9 |
@st.cache
|
|
|
|
| 18 |
st.write(dataframe)
|
| 19 |
series = st.text_input("Write the name of the variable you want to forecast")
|
| 20 |
date = st.text_input('Write the first date')
|
| 21 |
+
# freq = st.text_input('Write the frequency')
|
| 22 |
freq = st.selectbox('Select the frequency',
|
| 23 |
+
('Y', 'Q', 'M', 'W', 'D', 'NULL'))
|
| 24 |
|
| 25 |
if series is not None and freq is not None:
|
| 26 |
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
series_train = pd.DataFrame(
|
| 33 |
+
{
|
| 34 |
+
'ds': pd.date_range(start=date, periods=dataframe.shape[0], freq=freq),
|
| 35 |
+
'y': dataframe[series].values
|
| 36 |
+
},
|
| 37 |
+
index=pd.Index([0] * dataframe.shape[0], name='unique_id')
|
| 38 |
+
)
|
| 39 |
|
| 40 |
fig = px.line(series_train, x='ds', y='y', title=f'{series}')
|
| 41 |
st.plotly_chart(fig)
|
| 42 |
with st.spinner('Wait for it...'):
|
| 43 |
+
if freq != 'NULL':
|
| 44 |
+
|
| 45 |
+
fcst = auto_arima(
|
| 46 |
+
series_train,
|
| 47 |
+
seasonality=True,
|
| 48 |
+
freq=freq,
|
| 49 |
+
n_jobs=1,
|
| 50 |
+
max_p=12,
|
| 51 |
+
max_q=12
|
| 52 |
+
)
|
| 53 |
+
else:
|
| 54 |
+
fcst = auto_arima(
|
| 55 |
+
series_train,
|
| 56 |
+
seasonality=False,
|
| 57 |
+
n_jobs=1,
|
| 58 |
+
max_p=12,
|
| 59 |
+
max_q=12
|
| 60 |
+
)
|
| 61 |
+
fcst.fit(series_train)
|
| 62 |
+
forecasts, ci_05 = fcst.predict(horizons, alpha=0.05)
|
| 63 |
+
_, ci_10 = fcst.predict(horizons, alpha=0.1)
|
| 64 |
st.success('Done!')
|
| 65 |
|
| 66 |
st.write(forecasts)
|
| 67 |
+
forecasts_df = pd.DataFrame({'ds':pd.date_range(start=serie['ds']+pd.to_timedelta(horizons, units=freq), periods=horizons, freq=freq),
|
| 68 |
+
'mean':forecasts, 'low_ci_05':ci_05[:,0], 'low_ci_10':ci_10[:,0],
|
| 69 |
+
'hi_ci_05':ci_05[:,1], 'hi_ci_10':ci_10[:,1]})
|
| 70 |
csv = convert_df(forecasts)
|
| 71 |
|
| 72 |
st.download_button(
|
|
|
|
| 89 |
go.Scatter(
|
| 90 |
name=f'auto_arima_season_length-{seasonality}_mean',
|
| 91 |
x=forecasts['ds'],
|
| 92 |
+
y=forecasts['mean'],
|
| 93 |
mode='lines',
|
| 94 |
marker=dict(color='green'),
|
| 95 |
line=dict(width=1),
|