Harnessing Facebook Prophet for Time Series Forecasting
Written on
In this guide, we will explore the world of time series forecasting with Facebook Prophet, a user-friendly tool designed to simplify the prediction of future data points based on historical trends. Time series analysis is a fascinating area within data science that many enthusiasts are eager to delve into. The core concept involves learning from past data to anticipate future occurrences—applications can range from predicting weather patterns to sales trends. But how does Prophet fit into this picture?
This tutorial aims to eliminate barriers for beginners, guiding you through a hands-on experience with Facebook Prophet in Google Colab, which is freely accessible. For those who wish to dive right in, you may skip ahead to the tutorial section after a brief introduction to the principles of time series analysis and an overview of Prophet. Let’s get started!
This article is organized into three key sections: 1. Overview of Time Series Analysis Principles 2. Introduction to Facebook Prophet 3. A Practical Tutorial on Utilizing Prophet in Google Colab (for free)
# 1 Overview of Time Series Analysis Principles
Imagine you're managing a retail store, and you need to forecast product demand to optimize supply management. A suitable approach would be to conduct time series analysis, which encompasses understanding and modeling sequential data to make predictions.
The graphic below depicts a simulated progression of historical product demand (represented by the dark-blue line) over time, which can help us identify time series patterns. Our objective is to predict (the red-dotted line) future demand (illustrated by the light-blue line) as accurately as possible:
A time series can generally be broken down into three fundamental components:
- Trend: The long-term trajectory or direction within the data.
- Seasonality: Regular fluctuations or patterns that recur at consistent intervals.
- Residual/Error: The remaining variation in the data after accounting for trend and seasonality.
Decomposing a time series into these components—often termed additive or multiplicative decomposition—enables analysts to grasp the underlying structure and patterns. This understanding is crucial for selecting appropriate forecasting models and ensuring accurate predictions based on historical data.
# 2 Introduction to Facebook Prophet
Prophet is an open-source tool developed by Facebook's Data Science team that facilitates time series forecasting using an additive model, which accommodates non-linear trends alongside seasonal effects and holiday influences. Its design allows for parameter adjustments without requiring extensive statistical expertise, making it accessible for teams with varying levels of experience.
Prophet is particularly effective for business forecasting and has gained traction due to its user-friendly nature and its capability to manage diverse time series data. However, it's important to note that the effectiveness of Prophet compared to other models depends on the unique characteristics of the data and the objectives of the analysis. While Prophet is powerful, it may not always outperform other forecasting methods. Still, it includes useful features, such as accounting for seasonal changes during and after the COVID pandemic or treating lockdowns as unique holidays.
For a deeper introduction directly from Meta (formerly Facebook), check out the video linked below.
In the subsequent tutorial, we will implement and utilize Prophet with Python, although you are welcome to use R as well!
# 3 A Practical Tutorial on Utilizing Prophet
For those with limited coding experience or access to a coding environment, I recommend using Google Colaboratory (“Colab”), which is essentially a “free Jupyter notebook environment that runs entirely in the cloud.” While this platform boasts simplicity and convenience, it does have limitations, such as reduced computational power compared to more robust cloud environments. Nevertheless, Colab is an excellent starting point for experimenting with Prophet.
To set up a basic environment for Time Series Analysis in Colab, follow these two steps: 1. Visit https://colab.research.google.com/ and create a free account. 2. Start a new notebook in Colab. 3. Install and import the prophet package:
pip install prophet
from prophet import Prophet
Loading and Preparing Data
I have uploaded a small sample dataset representing the monthly passenger counts for a local bus service (2012–2023). You can access the data here on GitHub.
Initially, we will load the data using pandas and create two separate datasets: one for training (years 2012 to 2022) and one for testing (year 2023). We will train our time series model on the training data and aim to forecast passenger counts for 2023, using the testing data to validate our predictions later.
import pandas as pd
df_data = pd.read_csv("https://raw.githubusercontent.com/jonasdieckmann/prophet_tutorial/main/passengers.csv")
df_data_train = df_data[df_data["Month"] < "2023-01"] df_data_test = df_data[df_data["Month"] >= "2023-01"]
display(df_data_train)
The output will display a dataset with two columns: a year-month combination and a numerical column representing passenger counts for that month. Prophet is primarily designed for daily (or even hourly) data, but we will adapt it for monthly patterns.
Decomposing Training Data
To gain insights into the time series components of our sample data, we will perform a quick decomposition. We will use the method from the statsmodels library to decompose our dataset, choosing an additive model and specifying that one period comprises 12 elements (months).
from statsmodels.tsa.seasonal import seasonal_decompose
decompose = seasonal_decompose(df_data_train.Passengers, model='additive', extrapolate_trend='freq', period=12)
decompose.plot().show()
This code snippet provides a visual representation of the time series, highlighting trends, seasonal effects, and residuals:
We can observe a significant upward trend over the last decade, along with a recognizable seasonal pattern each year. Based on this information, we would anticipate that the model will predict an increase in passenger counts, aligned with seasonal peaks in the summer of the upcoming year. Let's proceed to apply some machine learning!
Model Fitting with Facebook Prophet
To fit models in Prophet, it is essential to have columns labeled ‘ds’ (date) and ‘y’ (value to forecast). We will rename our columns to reflect this requirement.
df_train_prophet = df_data_train
# Rename the date column to "ds" for Prophet df_train_prophet = df_train_prophet.rename(columns={"Month": "ds"})
# Rename the target column to "y" for Prophet df_train_prophet = df_train_prophet.rename(columns={"Passengers": "y"})
Now, the fitting process can commence. The procedure to fit the model is quite straightforward. For an extensive overview of the many options and parameters we can adjust, please refer to the documentation. For simplicity, we will fit a basic model without further modifications—yet remember, real-world data is rarely perfect, and parameter tuning will be necessary in future applications.
model_prophet = Prophet() model_prophet.fit(df_train_prophet)
That’s all it takes to fit the model. Next, let’s make some predictions!
Making Predictions
To generate predictions, we need a table with a ‘ds’ column that contains the dates for which we seek forecasts. We can create this table using the make_future_dataframe method, which will automatically incorporate historical dates. This allows us to assess how well the model fits past data while predicting future values. Since we are working with monthly data, we will specify the frequency as “freq=12” and request a forecast horizon of 12 months (“periods=12”).
df_future = model_prophet.make_future_dataframe(periods=12, freq='MS') display(df_future)
This new dataset will include both the training period and the additional 12 months we wish to predict:
To make predictions, we simply invoke the predict method from Prophet with the future dataset. The resulting output will be a comprehensive dataset with various columns, but we will focus on the predicted value (yhat) and the uncertainty intervals (yhat_lower and yhat_upper).
forecast_prophet = model_prophet.predict(df_future) forecast_prophet[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].round().tail()
The table below illustrates how the output is generated and stored. For August 2023, the model predicts a passenger count of 532. The uncertainty interval (default set at 80%) indicates that we can expect a likely passenger range between 508 and 556 for that month.
Finally, we aim to visualize the results to gain a clearer understanding of the predictions and their intervals.
Visualizing Results
To create plots, we can utilize Prophet’s built-in visualization tools. With the plot method, we can display the original time series data alongside the forecasted values.
import matplotlib.pyplot as plt
# Plot the time series forecast_plot = model_prophet.plot(forecast_prophet)
# Add a vertical line to mark the end of the training period axes = forecast_plot.gca() last_training_date = forecast_prophet['ds'].iloc[-12] axes.axvline(x=last_training_date, color='red', linestyle='--', label='Training End')
# Plot actual test data for the period after the red line df_data_test['Month'] = pd.to_datetime(df_data_test['Month']) plt.plot(df_data_test['Month'], df_data_test['Passengers'], 'ro', markersize=3, label='True Test Data')
# Show the legend for clarity plt.legend()
The plot showcases the overall time series data, with a dotted line marking the end of the training period and the beginning of the prediction phase. We have also included the actual test dataset for comparison.
The model's performance appears satisfactory, as most actual passenger counts fall within the predicted uncertainty intervals. However, we observe some underestimations during the summer months, a trend evident in prior years. This indicates an opportunity to fine-tune the parameters and features we can utilize with Prophet.
In our example, the seasonality is not merely an additive factor, as it appears to grow alongside the trend over time. Therefore, we might consider adjusting the seasonality_mode from “additive” to “multiplicative” during the fitting process.
This tutorial will conclude here, allowing you time to explore the extensive features Prophet has to offer. To review the complete code together, I have compiled the snippets in this Python file. You can also upload this notebook directly to Colab and run it yourself. I would love to hear how it goes for you!
# Conclusion
Prophet is a robust tool for forecasting future values in time series data, especially when dealing with recurring patterns, such as monthly or yearly cycles. Its user-friendly design allows for quick and accurate predictions tailored to your specific data. However, being aware of its limitations is crucial; if your data lacks clear patterns or experiences significant changes unseen by the model, Prophet might not yield optimal results.
The encouraging news is that experimenting with Prophet on your datasets is highly encouraged! Each dataset is unique, and adjusting settings while trying various approaches can help you identify the best strategies for your specific circumstances. So, dive in, explore, and discover how Prophet can enhance your time series forecasting capabilities.
Additional Resources
Jonas Dieckmann - Medium
Read writing from Jonas Dieckmann on Medium, an analytics manager and product owner at Philips, passionate about data science.
Feel free to connect with me on LinkedIn at https://www.linkedin.com/in/jonas-dieckmann/ and follow me here on Medium.
Recommended Articles
- How To Use ChatGPT API for Direct Interaction From Colab or Databricks
- How to get started with TensorFlow using Keras API and Google Colab
References
[1] Shumway, Robert H.; Stoffer, David S. (2017): Time Series Analysis and Its Applications. Cham: Springer International Publishing. [2] Brownlee, Jason (2017): Introduction to Time Series Forecasting With Python. [3] Rafferty, Greg (2021): Forecasting Time Series Data with Facebook Prophet. [4] https://facebook.github.io/prophet/docs/quick_start.html