Skip to content

Instantly share code, notes, and snippets.

@ucaiado
Created April 14, 2020 03:43
Show Gist options
  • Save ucaiado/d1ad6c54ad5ce6204f10640145909e4c to your computer and use it in GitHub Desktop.
Save ucaiado/d1ad6c54ad5ce6204f10640145909e4c to your computer and use it in GitHub Desktop.
# source: https://facebook.github.io/prophet/docs/quick_start.html
from fbprophet import Prophet
COUNTRY = 'Italy'
# filter data
df_plot = df[df.countriesAndTerritories.isin([COUNTRY])]
df_plot = (df_plot.set_index('dateRep'))['cases'].sort_index()
m = Prophet(yearly_seasonality=False, daily_seasonality=True)
m.add_seasonality(name='monthly', period=30.5, fourier_order=3)
# transform data
df_plot = pd.DataFrame(np.log(df_plot[df_plot>0]))
df_plot.columns = ['y']
df_plot.index.name = 'ds'
# fit model
m.fit(df_plot.reset_index())
future = m.make_future_dataframe(periods=60)
forecast = m.predict(future)
forecast[['yhat', 'yhat_lower', 'yhat_upper']] = forecast[
['yhat', 'yhat_lower', 'yhat_upper']].apply(lambda x: np.exp(x))
ax = np.exp(df_plot['y']).plot(color='#334f8d', fontsize=11, zorder=2)
forecast.set_index('ds')['yhat'].plot(
color='#334f8d', alpha=0.3, fontsize=11, zorder=2, ax=ax);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment