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') df.head()
date | open | high | low | close | volume | Name | |
---|---|---|---|---|---|---|---|
0 | 2013-02-08 | 15.07 | 15.12 | 14.63 | 14.75 | 8407500 | AAL |
1 | 2013-02-11 | 14.89 | 15.01 | 14.26 | 14.46 | 8882000 | AAL |
2 | 2013-02-12 | 14.45 | 14.51 | 14.10 | 14.27 | 8126000 | AAL |
3 | 2013-02-13 | 14.30 | 14.94 | 14.25 | 14.66 | 10259500 | AAL |
4 | 2013-02-14 | 14.94 | 14.96 | 13.16 | 13.99 | 31879900 | AAL |
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:

Using Facebook Prophet Model with Python
Steps to use the Facebook Prophet template:
- Be sure to substitute the close price for y and the date for ds.
- Fit this data frame to the Prophet to detect future patterns.
- 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) ph_df.head()
ds | y | |
---|---|---|
46387 | 2013-02-08 | 261.95 |
46388 | 2013-02-11 | 257.21 |
46389 | 2013-02-12 | 258.70 |
46390 | 2013-02-13 | 269.47 |
46391 | 2013-02-14 | 269.24 |

fig2 = m.plot_components(forecast) plt.show()

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") plt.show()

Observations:
From the visualizations above, we can observe that:
- Amazon’s stock price is showing signs of an uptrend every year.
- Amazon’s share price shows signs of an uptrend in January (December sales tend to boost Amazon’s share price)
- 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.