Facebook Prophet Model with Python

Facebook Prophet Model tutorial with Python.

In this article, I will take you through how to forecast stock prices using Facebook Prophet Model with Python programming language, which is used for the tasks of time series forecasting.

Introduction to Facebook Prophet Model

The Facebook research team has come up with an easier implementation of time series forecasting with its new library called Prophet. An analyst capable of producing high-quality forecast data is rarely seen. This is one of the reasons why the Facebook research team has come up with an easily accessible way to use advanced concepts for time series forecasting. 

Also, Read – 100+ Machine Learning Projects Solved and Explained.

We as Python users can easily relate to this library because it uses an API which is similar to Scikit-Learn. The main goal of the Prophet team is to enable experts and non-experts to make high-quality forecasts that meet demand.

In my opinion, Prophet works best with datasets that are heavily influenced by seasonality e.g. utility bills, restaurant visitors, etc.

Facebook Prophet Model with Python

Now let’s analyze stock prices and use the Facebook Prophet model with Python programming language. I will start with importing the necessary python libraries and the dataset:

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
from fbprophet import Prophet
df = pd.read_csv('all_stocks_5yr.csv')

Here I will replace the column name from name to ticks:

df = df.rename(columns={'Name': 'Ticks'})

For this simple Facebook Prophet Model tutorial with Python, I’ll analyze the Amazon stock and see what the trend for the near future of that stock will look like based on past stock prices:

amzn = df.loc[df['Ticks'] == 'AMZN']

We need to make sure the date column is either a categorical type or a data type. In our case, the date column is a categorical data type, so we need to change it to DateTime. Before doing that, I will first create a copy of the data to avoid the SettingWarning:

amzn_df = amzn.copy()
# Change to datetime datatype.
amzn_df.loc[:, 'date'] = pd.to_datetime(amzn.loc[:,'date'], format="%Y/%m/%d")

Data Visualization

Now, let’s plot our data to see what we need to create a model for:

amazon stock prices

Using Facebook Prophet Model with Python

Steps to use the Facebook Prophet template:

  1. Be sure to substitute the close price for y and the date for ds.
  2. Fit this data frame to the Prophet to detect future patterns.
  3. Predict the prices above and below the closing price.

Now let’s see how to use the Facebook Prophet Model with Python programming language:

m = Prophet()

# Drop the columns
ph_df = amzn_df.drop(['open', 'high', 'low','volume', 'Ticks'], axis=1)
ph_df.rename(columns={'close': 'y', 'date': 'ds'}, inplace=True)

facebook prophet model
fig2 = m.plot_components(forecast)
seasonality affects
m = Prophet(changepoint_prior_scale=0.01).fit(ph_df)
future = m.make_future_dataframe(periods=12, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
plt.title("Monthly Prediction \n 1 year time frame")

monthly FB prophet model


From the visualizations above, we can observe that:

  1. Amazon’s stock price is showing signs of an uptrend every year.
  2. Amazon’s share price shows signs of an uptrend in January (December sales tend to boost Amazon’s share price)
  3. There is no weekly trend for stock prices.

I hope you liked this article on a tutorial on Facebook Prophet model with Python programming language. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Do NOT follow this link or you will be banned from the site!