Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,211 +0,0 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import numpy as np
|
3 |
-
from neuralprophet import NeuralProphet
|
4 |
-
from neuralprophet import set_random_seed
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
import streamlit as st
|
7 |
-
from datetime import datetime as dt
|
8 |
-
# import os
|
9 |
-
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
10 |
-
|
11 |
-
st.title('Time Series Forecasting with Neural Prophet')
|
12 |
-
option=st.selectbox('Choose from the following',['Forecasting without events','Forecasting with events'])
|
13 |
-
|
14 |
-
# try:
|
15 |
-
uploaded_file = st.sidebar.file_uploader("Upload your CSV file", type=["csv"])
|
16 |
-
@st.cache_data
|
17 |
-
def read_file(uploaded_file):
|
18 |
-
data2=pd.read_csv(uploaded_file)
|
19 |
-
return data2
|
20 |
-
|
21 |
-
df=read_file(uploaded_file)
|
22 |
-
|
23 |
-
##################################### Option 1 #####################################
|
24 |
-
if option=='Forecasting without events':
|
25 |
-
|
26 |
-
daily_seasonality_btn = st.sidebar.select_slider('Daily Seasonality',options=[True, False],value=False)
|
27 |
-
weekly_seasonality_btn = st.sidebar.select_slider('Weekly Seasonality',options=[True, False],value=True)
|
28 |
-
yearly_seasonality_btn = st.sidebar.select_slider('Yearly Seasonality',options=[True, False],value=True)
|
29 |
-
n_hist_pred_btn=st.sidebar.number_input('No. of Historical Data Points',0,360,30)
|
30 |
-
epochs_btn=st.sidebar.number_input('Epochs',1,20,5)
|
31 |
-
n_hidden_layers_btn=st.sidebar.number_input('No. of Hidden Layers',1,5,1)
|
32 |
-
loss_fn_btn=st.sidebar.selectbox('Loss Function',['MAE','MSE','Huber'])
|
33 |
-
seasonality_mode_btn=st.sidebar.selectbox('Seasonality Mode',['Additive','Multiplicative'])
|
34 |
-
n_change_points_btn=st.sidebar.number_input('No. of Trend Change Points',0,360,30)
|
35 |
-
|
36 |
-
with st.expander("Select Date & Observed Value",expanded=True):
|
37 |
-
c1, c2 = st.columns((1, 1))
|
38 |
-
x=c1.selectbox('Date',df.columns)
|
39 |
-
ycols=[cols for cols in df.columns if cols!=df.columns[0] and df.dtypes[cols]!='object']
|
40 |
-
y=c2.selectbox('Observed Value',ycols)
|
41 |
-
|
42 |
-
with st.expander("Choose the Forecast Period with its Frequency"):
|
43 |
-
c8, c9 = st.columns((1, 1))
|
44 |
-
periods=int(c8.number_input('Forecast Period',0,365,60))
|
45 |
-
freq=c9.selectbox('Frequency',["D","M","Y","s","min","H"])
|
46 |
-
|
47 |
-
df1=df[[x,y]]
|
48 |
-
df['ds'],df['y']=df[x],df[y]
|
49 |
-
df=df[['ds','y']]
|
50 |
-
df.dropna(inplace=True)
|
51 |
-
df.drop_duplicates(subset=['ds'],inplace=True)
|
52 |
-
df['ds']=pd.to_datetime(df['ds'])
|
53 |
-
df.sort_values(by=['ds'],inplace=True)
|
54 |
-
df=df.reset_index(drop=True)
|
55 |
-
|
56 |
-
st.header('Dataset')
|
57 |
-
st.dataframe(df1.head())
|
58 |
-
rmp=st.radio('Run Model',['n','y'])
|
59 |
-
|
60 |
-
if rmp=='y':
|
61 |
-
set_random_seed(40)
|
62 |
-
m = NeuralProphet(n_changepoints=n_change_points_btn,daily_seasonality=daily_seasonality_btn,weekly_seasonality=weekly_seasonality_btn,yearly_seasonality=yearly_seasonality_btn,seasonality_mode=seasonality_mode_btn,num_hidden_layers=n_hidden_layers_btn,loss_func=loss_fn_btn,epochs=epochs_btn,)
|
63 |
-
# split into train & test dataset
|
64 |
-
df_train, df_test = m.split_df(df, freq=freq,valid_p=0.2)
|
65 |
-
train_metrics = m.fit(df_train, freq=freq,)
|
66 |
-
test_metrics = m.test(df_test,)
|
67 |
-
|
68 |
-
import warnings
|
69 |
-
warnings.filterwarnings("ignore")
|
70 |
-
future = m.make_future_dataframe(df=df, n_historic_predictions=n_hist_pred_btn,periods=periods)
|
71 |
-
forecast = m.predict(df=future)
|
72 |
-
final_train_metrics=train_metrics.iloc[len(train_metrics)-1:len(train_metrics)].reset_index(drop=True)
|
73 |
-
final_test_metrics=test_metrics.iloc[len(test_metrics)-1:len(test_metrics)].reset_index(drop=True)
|
74 |
-
|
75 |
-
fig = m.plot(forecast)
|
76 |
-
fig_comp = m.plot_components(forecast)
|
77 |
-
fig_param = m.plot_parameters()
|
78 |
-
|
79 |
-
st.header('Train Dataset Metrics')
|
80 |
-
st.dataframe(final_train_metrics)
|
81 |
-
st.header('Test Dataset Metrics')
|
82 |
-
st.dataframe(final_test_metrics)
|
83 |
-
|
84 |
-
st.header('Forecast Values')
|
85 |
-
st.pyplot(fig)
|
86 |
-
|
87 |
-
st.header('Trend & Seasonality')
|
88 |
-
st.pyplot(fig_param)
|
89 |
-
st.dataframe(forecast)
|
90 |
-
|
91 |
-
@st.cache_data
|
92 |
-
def convert_df(df):
|
93 |
-
return df.to_csv(index=False).encode('utf-8')
|
94 |
-
|
95 |
-
# try:
|
96 |
-
forecast_df = convert_df(forecast)
|
97 |
-
if forecast_df is not None:
|
98 |
-
st.download_button(label="Download data as CSV",data=forecast_df,file_name='NeuralProphet_with_events_results.csv',mime='text/csv',)
|
99 |
-
# except:
|
100 |
-
# st.warning('Choose Something')
|
101 |
-
|
102 |
-
##################################### Option 2 #####################################
|
103 |
-
if option=='Forecasting with events':
|
104 |
-
|
105 |
-
daily_seasonality_btn = st.sidebar.select_slider('Daily Seasonality',options=[True, False],value=False)
|
106 |
-
weekly_seasonality_btn = st.sidebar.select_slider('Weekly Seasonality',options=[True, False],value=True)
|
107 |
-
yearly_seasonality_btn = st.sidebar.select_slider('Yearly Seasonality',options=[True, False],value=True)
|
108 |
-
n_hist_pred=st.sidebar.number_input('No. of Historical Data Points',0,360,30)
|
109 |
-
epochs_btn=st.sidebar.number_input('Epochs',1,20,5)
|
110 |
-
n_hidden_layers_btn=st.sidebar.number_input('No. of Hidden Layers',1,5,1)
|
111 |
-
loss_fn_btn=st.sidebar.selectbox('Loss Function',['MAE','MSE','Huber'])
|
112 |
-
n_change_points_btn=st.sidebar.number_input('No. of Trend Change Points',0,360,30)
|
113 |
-
|
114 |
-
with st.expander("Select Date & Observed Value",expanded=True):
|
115 |
-
c1, c2 = st.columns((1, 1))
|
116 |
-
x=c1.selectbox('Date',df.columns)
|
117 |
-
ycols=[cols for cols in df.columns if cols!=df.columns[0] and df.dtypes[cols]!='object']
|
118 |
-
y=c2.selectbox('Observed Value',ycols)
|
119 |
-
|
120 |
-
with st.expander("Select Event Names & their Dates"):
|
121 |
-
c3, c4 = st.columns((1, 1))
|
122 |
-
events1=c3.text_input(label='Event 1 Name',value='New Year Eve')
|
123 |
-
eventd1=c3.date_input(label='Event 1 Date Range: ',value=(dt(year=1900, month=1, day=1),
|
124 |
-
dt(year=2030, month=1, day=30)),)
|
125 |
-
events2=c4.text_input(label='Event 2 Name',value='Christmas')
|
126 |
-
eventd2=c4.date_input(label='Event 2 Date Range: ',value=(dt(year=1900, month=1, day=1),
|
127 |
-
dt(year=2030, month=1, day=30)),)
|
128 |
-
|
129 |
-
with st.expander("Select the Lower & Upper Window for the Events & Seasonality Factor"):
|
130 |
-
c5, c6, c7 = st.columns((1, 1, 1))
|
131 |
-
lw=c5.number_input('Lower Window',-10,0,-1)
|
132 |
-
uw=c6.number_input('Upper Window',0,10,0)
|
133 |
-
mode=c7.selectbox('Seasonality',['Additive','Multiplicative'])
|
134 |
-
|
135 |
-
with st.expander("Choose the Forecast Period with its Frequency"):
|
136 |
-
c8, c9 = st.columns((1, 1))
|
137 |
-
periods=int(c8.number_input('Forecast Period',0,365,60))
|
138 |
-
freq=c9.selectbox('Frequency',["D","M","Y","s","min","H"])
|
139 |
-
|
140 |
-
df1=df[[x,y]]
|
141 |
-
df['ds'],df['y']=df[x],df[y]
|
142 |
-
df=df[['ds','y']]
|
143 |
-
df.dropna(inplace=True)
|
144 |
-
df.drop_duplicates(subset=['ds'],inplace=True)
|
145 |
-
df['ds']=pd.to_datetime(df['ds'])
|
146 |
-
df.sort_values(by=['ds'],inplace=True)
|
147 |
-
df=df.reset_index(drop=True)
|
148 |
-
|
149 |
-
st.header('Dataset')
|
150 |
-
st.dataframe(df1.head())
|
151 |
-
rmp=st.radio('Run Model',['n','y'])
|
152 |
-
|
153 |
-
if rmp=='y':
|
154 |
-
set_random_seed(40)
|
155 |
-
m = NeuralProphet(n_changepoints=n_change_points_btn,daily_seasonality=daily_seasonality_btn,weekly_seasonality=weekly_seasonality_btn,yearly_seasonality=yearly_seasonality_btn,num_hidden_layers=n_hidden_layers_btn,loss_func=loss_fn_btn,epochs=epochs_btn,)
|
156 |
-
event1 = pd.DataFrame({'event': events1,'ds': pd.to_datetime(eventd1).date})
|
157 |
-
event2 = pd.DataFrame({'event': events2,'ds': pd.to_datetime(eventd2).date})
|
158 |
-
if events2=='':
|
159 |
-
enames=[events1]
|
160 |
-
events_df = pd.concat([event1])
|
161 |
-
else:
|
162 |
-
enames=[events1,events2]
|
163 |
-
events_df = pd.concat([event1,event2])
|
164 |
-
|
165 |
-
events_df=events_df[events_df['event']!='']
|
166 |
-
for i in range(len(enames)):
|
167 |
-
if enames[i]!='':
|
168 |
-
m=m.add_events([enames[i]],lower_window=lw,upper_window=uw,mode=mode)
|
169 |
-
history_df = m.create_df_with_events(df, events_df)
|
170 |
-
metrics=m.fit(history_df, freq=freq,)
|
171 |
-
import warnings
|
172 |
-
warnings.filterwarnings("ignore")
|
173 |
-
future = m.make_future_dataframe(df=history_df, events_df=events_df,n_historic_predictions=n_hist_pred,periods=periods)
|
174 |
-
forecast = m.predict(df=future)
|
175 |
-
fig = m.plot(forecast)
|
176 |
-
fig_comp = m.plot_components(forecast)
|
177 |
-
fig_param = m.plot_parameters()
|
178 |
-
|
179 |
-
final_metrics=metrics.iloc[len(metrics)-1:len(metrics)].reset_index(drop=True)
|
180 |
-
|
181 |
-
st.header('Model Metrics')
|
182 |
-
st.dataframe(final_metrics)
|
183 |
-
|
184 |
-
st.header('Forecast Values')
|
185 |
-
st.pyplot(fig)
|
186 |
-
|
187 |
-
st.header('Trend & Seasonality')
|
188 |
-
st.pyplot(fig_param)
|
189 |
-
st.dataframe(forecast)
|
190 |
-
|
191 |
-
@st.cache_data
|
192 |
-
def convert_df(df):
|
193 |
-
return df.to_csv(index=False).encode('utf-8')
|
194 |
-
|
195 |
-
# try:
|
196 |
-
forecast_df = convert_df(forecast)
|
197 |
-
if forecast_df is not None:
|
198 |
-
st.download_button(label="Download data as CSV",data=forecast_df,file_name='NeuralProphet_with_events_results.csv',mime='text/csv',)
|
199 |
-
# except:
|
200 |
-
# st.warning('Choose Something')
|
201 |
-
|
202 |
-
#####################################################
|
203 |
-
# except:
|
204 |
-
# st.warning('Choose Something')
|
205 |
-
|
206 |
-
st.sidebar.write('### **About**')
|
207 |
-
st.sidebar.info(
|
208 |
-
"""
|
209 |
-
Created by:
|
210 |
-
[Parthasarathy Ramamoorthy](https://www.linkedin.com/in/parthasarathyr97/) (Data Scientist @ Walmart Global Tech)
|
211 |
-
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|