Time Series Forecasting with Prophet

Subash Palvel
2 min readSep 12, 2023

--

Time series forecasting is a technique used to predict future values based on historical data. It has various applications in finance, sales forecasting, weather prediction, and more. In this post, we will explore how to use Prophet, a powerful open-source library developed by Facebook, for time series forecasting.

What is Prophet?

Prophet is a forecasting library implemented in Python and R. It was developed by Facebook’s Core Data Science team and is designed to handle time series data with multiple seasonalities, missing values, and outliers. Prophet is widely used due to its simplicity, flexibility, and ability to provide accurate forecasts.

Installation

To get started with Prophet, you need to install it using pip:

pip install prophet

Data Preparation

Before we can start forecasting, we need to prepare our data. Prophet expects a specific format for the input data: a DataFrame with two columns — “ds” (datestamp) and “y” (the value we want to forecast).

Let’s assume we have a CSV file named “data.csv” with the following structure:

date,value
2021-01-01,10
2021-01-02,15
2021-01-03,20
...

To load this data into a DataFrame, we can use the pandas library:

import pandas as pd
data = pd.read_csv("data.csv")

Next, we need to convert the “ds” column to a datetime type:

data["ds"] = pd.to_datetime(data["date"])

Finally, we can drop the unnecessary columns and rename the “value” column to “y”:

data = data[["ds", "value"]]
data = data.rename(columns={"value": "y"})

Model Training and Forecasting

Now that our data is prepared, we can train the Prophet model and make forecasts. The basic workflow involves creating a Prophet object, fitting it to the data, and then making predictions.

from prophet import Prophet
model = Prophet()
model.fit(data)
future = model.make_future_dataframe(periods=365) # Forecast for the next 365 days
forecast = model.predict(future)

The make_future_dataframe function creates a DataFrame with future dates for which we want to make predictions. The periods parameter specifies the number of future periods to forecast.

Visualizing the Forecast

Prophet provides built-in visualization capabilities to help us understand the forecasted data. We can plot the forecasted values, along with the historical data, using the plot method:

fig = model.plot(forecast)

Additionally, we can visualize the components of the forecast, such as the trend and seasonality, using the plot_components method:

fig = model.plot_components(forecast)

Conclusion

In this post, we explored how to use Prophet for time series forecasting. We learned about its installation, data preparation, model training, and visualization capabilities. Prophet is a powerful tool that simplifies the process of forecasting and provides accurate predictions. It is highly recommended for anyone working with time series data.

Happy forecasting!

Follow me at LinkedIn:

https://www.linkedin.com/in/subashpalvel/

Follow me at Medium:

https://subashpalvel.medium.com/

--

--

No responses yet