Skip to content

Instantly share code, notes, and snippets.

@dttruong-dius
Created August 29, 2018 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dttruong-dius/882deed373a10648dce3795512cc35e2 to your computer and use it in GitHub Desktop.
Save dttruong-dius/882deed373a10648dce3795512cc35e2 to your computer and use it in GitHub Desktop.
time_series_forecasting_with_fbprophet
import pandas as pd
from fbprophet import Prophet
from matplotlib import pyplot as plt
fig, ax = plt.subplots(figsize=(15, 5))
forecast['weekday'] = forecast['ds'].dt.weekday
df['weekday'] = df['ds'].dt.weekday
colors = ['r', 'g', 'yellow', 'pink', 'purple', 'cyan', 'blue']
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for wd in [0, 6]:
fc_wd = forecast[forecast['weekday'] == wd]
ax.plot(
fc_wd['ds'], fc_wd['yhat'],
c=colors[wd], marker='o', ms=5, linestyle='None',
label=f'Forecast-{weekdays[wd]}')
df_wd = df[df['weekday'] == wd]
ax.plot(
df_wd['ds'], df_wd['y'],
c=colors[wd], marker='^', ms=5, linestyle='None',
label=f'Actual-{weekdays[wd]}'
)
ax.legend()
ax.set_xlabel('Date')
ax.set_ylabel('Sales');
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(
df['ds'], df['y'],
c='black', marker='o', ms=3, linestyle='None',
label=f'Actual'
)
forecast['weekday'] = forecast['ds'].dt.weekday
colors = ['r', 'g', 'blue', 'pink', 'purple', 'cyan', 'orange']
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for wd in range(7):
fc_wd = forecast[forecast['weekday'] == wd]
ax.plot(
fc_wd['ds'], fc_wd['yhat'],
c=colors[wd], marker='o', ms=3, linestyle='None',
label=f'Forecast-{weekdays[wd]}', alpha=0.8)
ax.legend()
ax.set_xlabel('Date')
ax.set_ylabel('Sales');
y_true = df_test['y']
y_forecast = forecast[-n_tests:]['yhat']
smape = ((y_true - y_forecast).abs() / (y_true.abs() + y_forecast.abs())).mean() * 200
print('The SMAPE error is:', smape)
train = pd.read_csv('../input/train.csv')
train['date'] = pd.to_datetime(train['date'])
df = train[(train['item'] == 1) & (train['store'] == 1)][['date', 'sales']]
df.rename(columns={'date': 'ds', 'sales': 'y'}, inplace=True)
df.describe()
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(df['ds'], df['y'], linestyle='None', marker='o')
ax.set_xlabel('Date')
ax.set_ylabel('Sales');
n_tests = 180
df_train = df[:-n_tests]
df_test = df[-n_tests:]
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(df_train['ds'], df_train['y'], linestyle='None', marker='o', color='black', label='Train')
ax.plot(df_test['ds'], df_test['y'], linestyle='None', marker='o', color='red', label='Test')
ax.legend()
ax.set_xlabel('Date')
ax.set_ylabel('Sales');
model = Prophet(
daily_seasonality=False,
weekly_seasonality=False,
yearly_seasonality=False,
changepoint_prior_scale=0.05,
)
model.add_seasonality(
name='weekly',
period=7,
fourier_order=4,
)
model.add_seasonality(
name='yearly',
period=365.25,
fourier_order=2,
)
model.fit(df_train);
forecast = model.predict(df)
forecast[['ds', 'yhat']].head()
model.plot_components(forecast);
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(df_train['ds'], df_train['y'], c='black', marker='o', ms=3, linestyle='None', label='Train')
ax.plot(df_test['ds'], df_test['y'], c='r', marker='o', ms=3, linestyle='None', label='Test')
ax.plot(forecast['ds'], forecast['yhat'], c='b', marker='o', ms=3, linestyle='None', label='Forecast', alpha=0.5)
ax.legend()
ax.set_xlabel('Date')
ax.set_ylabel('Sales');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment