Created
August 29, 2018 00:35
-
-
Save dttruong-dius/882deed373a10648dce3795512cc35e2 to your computer and use it in GitHub Desktop.
time_series_forecasting_with_fbprophet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from fbprophet import Prophet | |
from matplotlib import pyplot as plt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train = pd.read_csv('../input/train.csv') | |
train['date'] = pd.to_datetime(train['date']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = train[(train['item'] == 1) & (train['store'] == 1)][['date', 'sales']] | |
df.rename(columns={'date': 'ds', 'sales': 'y'}, inplace=True) | |
df.describe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
forecast = model.predict(df) | |
forecast[['ds', 'yhat']].head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.plot_components(forecast); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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