Introduction to Time series with Prophet by Facebook

Chetan Hirapara
3 min readMar 29, 2023

Photo by Murray Campbell on Unsplash

Introduction

Time series forecasting is an important area of machine learning which has applications in multiple domains including finance, healthcare, weather, and energy. Time series forecasting is used to predict future values based on historical observations. In this blog, we will discuss how to perform time series forecasting using the Prophet library developed by Facebook.

Prophet

Prophet is a time series forecasting library developed by Facebook. It is an additive regression model which decomposes time series data into a trend, seasonality, and holiday components. A prophet can handle missing data and outliers, and can also model non-linear trends. Prophet is widely used for time series forecasting due to its simplicity, accuracy, and ease of use.

Example

In this example, we will use Prophet to forecast the number of daily page views for the Wikipedia page of “Machine learning” for the next 365 days. We will use Python and the following libraries:

  • Pandas: to load and manipulate data
  • Prophet: to perform time series forecasting
  • Matplotlib: to visualize the results

The dataset can be downloaded from https://bit.ly/3tafZ6N. The dataset contains the number of daily page views for the Wikipedia page on “Machine learning” from December 2007 to August 2021.

First, we need to load the data and prepare it for time series forecasting:

import pandas as pd
from fbprophet import Prophet
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv("data/machine_learning.csv")

# Convert the 'date' column to datetime format
df['date'] = pd.to_datetime(df['date'])

# Rename the columns to 'ds' and 'y'
df = df.rename(columns={'date': 'ds', 'value': 'y'})

# Split the data into training and testing sets
train_size = int(len(df) * 0.8)
train_df = df[:train_size]
test_df = df[train_size:]

Next, we will train the Prophet model on the training data and make predictions for the next 365 days:

# Create a new Prophet model
model = Prophet()

# Add yearly seasonality
model.add_seasonality(name='yearly', period=365, fourier_order=5)

# Fit the model to the training data
model.fit(train_df)

# Make predictions for the next 365 days
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)

Finally, we will visualize the results:

# Plot the forecast
fig, ax = plt.subplots(figsize=(10, 6))
model.plot(forecast, ax=ax)
ax.set_title("Machine Learning Wikipedia Page Views")
ax.set_xlabel("Date")
ax.set_ylabel("Page Views")
plt.show()

The output of the code will be a graph of the time series with the forecast for the next 365 days.

Conclusion

Prophet is a powerful library for time series forecasting that is easy to use and provides accurate predictions. In this blog, we showed how to use Prophet to forecast the number of daily page views for the Wikipedia page of “Machine learning” for the next 365 days. This example can be extended to other time series forecasting problems and datasets.

If you like the article and would like to support me make sure to:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Chetan Hirapara
Chetan Hirapara

Written by Chetan Hirapara

I am passionate data scientist/engineer

No responses yet

Write a response