Friday, August 28, 2020

Time Series Forecasting using fbprophet in Python

Hello friends. Today I am going to talk about time series forecasting using one of the best tools that we have in open source projects i.e. fbprophet. You can read more about it with documentation at the hyperlink. 'fbprophet' implements a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality and on top of that gives flexibility for holiday effects.

So lets start, I have chosen a kaggle dataset to predict the sales of stores. You can probably download the dataset and practice along for a better understanding. 

Importing the necessary libraries: As always



But most importantly, it will ask you to install a few packages as 99% chances are, you wont have these packages installed. So you need to install in Ananconda prompt using pip install. For e.g.


Step 2: Plotting the data

plt.subplots(1, figsize = (16, 5))

plt.grid()

plt.title('Store 1 - All Sales')

for item in set(df_raw[df_raw['store'] == 1]['item']):

    plt.plot(df_raw[(df_raw['store'] == 1) & 

                    (df_raw['item'] == item)]['sales'].rolling(window=50).mean())



Step 3: Preparing the data into Fbprophet format

train_dataset = df_raw[(df_raw['item'] == 1) & df_raw['store'] == 1] 

train_dataset.reset_index(level=0, inplace=True)

train_dataset = train_dataset[['date', 'sales']]

train_dataset.columns = ["ds", "y"]

train_dataset = train_dataset.sample(5000)

prophet_basic = Prophet()

prophet_basic.fit(train_dataset)

Step 4: Forecasting 1 year ahead

future= prophet_basic.make_future_dataframe(periods=365)

future

Step 5: And here you go with the forecast

You can visualize the various components i.e. yearly and weekly trends

fig1 = prophet_basic.plot_components(forecast)


Visualize with even further additions to the plot.

So time series analysis is now simple and robust with fbprophet. You just need to have the right set of data.

Since we are talking a lot about visualization within data analytics, so maybe I will come up with a post on amazing Tableau visualizations next.


Thanks,

No comments:

Post a Comment