Source url Data corresponds to retail_sales example csv
In [1]:
retail_datafile = '../datasets/example_retail_sales.csv'
In [2]:
from fbprophet import Prophet
import numpy as np
import pandas as pd
%matplotlib inline
In [3]:
sales_df = pd.read_csv(retail_datafile)
sales_df.head()
Out[3]:
In [4]:
sales_df['y_orig'] = sales_df['y'] # to save a copy of the original data..you'll see why shortly.
# log-transform y
sales_df['y'] = np.log(sales_df['y'])
sales_df.tail()
Out[4]:
In [5]:
sales_df.y_orig.hist(bins=30)
Out[5]:
In [6]:
sales_df.y.hist(bins=30)
Out[6]:
In [7]:
model = Prophet()
model.fit(sales_df)
Out[7]:
In [8]:
future_data = model.make_future_dataframe(periods=6, freq = 'm')
In [9]:
forecast_data = model.predict(future_data)
forecast_data.tail()
Out[9]:
In [13]:
model.plot(forecast_data)
Out[13]:
In [14]:
model.plot_components(forecast_data)
Out[14]:
From the trend and seasonality, we can see that the trend is a playing a large part in the underlying time series and seasonality comes into play more toward the beginning and the end of the year.
So far so good. With the above info, we’ve been able to quickly model and forecast some data to get a feel for what might be coming our way in the future from this particular data set.
Here is a little tip for getting your forecast plot to display your ‘original’ data so you can see the forecast in ‘context’ and in the original scale rather than the log-transformed data. You can do this by using np.exp() to get our original data back.
In [15]:
forecast_data_orig = forecast_data # make sure we save the original forecast data
forecast_data_orig['yhat'] = np.exp(forecast_data_orig['yhat'])
forecast_data_orig['yhat_lower'] = np.exp(forecast_data_orig['yhat_lower'])
forecast_data_orig['yhat_upper'] = np.exp(forecast_data_orig['yhat_upper'])
In [16]:
model.plot(forecast_data_orig)
Out[16]:
In [17]:
sales_df['y_log']=sales_df['y'] #copy the log-transformed data to another column
sales_df['y']=sales_df['y_orig'] #copy the original data to 'y'
In [18]:
model.plot(forecast_data_orig)
Out[18]:
In [ ]: