Prophet in Python

Prophet in Python

$Prophet$ is a popular library developed by $Facebook$ for time series forecasting.

It’s particularly effective for data that has strong seasonal effects and multiple seasonality with daily observations.

Here’s a basic example of how to use $Prophet$ in $Python$:

Step-by-Step Example:

  1. Install Prophet (if you haven’t already):

    1
    pip install prophet
  2. Import Required Libraries:

    1
    2
    3
    import pandas as pd
    from prophet import Prophet
    import matplotlib.pyplot as plt
  3. Load Your Data:
    For this example, let’s create a simple time series dataset.

    1
    2
    3
    4
    5
    6
    # Create a simple time series dataset
    dates = pd.date_range(start='2022-01-01', periods=365)
    data = pd.DataFrame({
    'ds': dates,
    'y': 100 + (dates.dayofyear - 183) ** 2 / 100 + np.random.randn(365) * 5
    })
  4. Initialize and Fit the Prophet Model:

    1
    2
    3
    4
    5
    # Initialize the Prophet model
    model = Prophet()

    # Fit the model to the data
    model.fit(data)
  5. Make Predictions:
    You can make future predictions using the model by specifying the number of days into the future you want to forecast.

    1
    2
    3
    4
    5
    # Create a dataframe for future predictions
    future = model.make_future_dataframe(periods=30) # Predict 30 days into the future

    # Predict future values
    forecast = model.predict(future)
  6. Visualize the Forecast:
    $Prophet$ has a built-in plot function to visualize the forecasted data.

    1
    2
    3
    # Plot the forecast
    model.plot(forecast)
    plt.show()
  7. Plot Components:
    You can also plot the components (trend, weekly seasonality, yearly seasonality) of the forecast.

    1
    2
    3
    # Plot the forecast components
    model.plot_components(forecast)
    plt.show()

Full Code Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pandas as pd
import numpy as np
from prophet import Prophet
import matplotlib.pyplot as plt

# Step 1: Create a simple time series dataset
dates = pd.date_range(start='2022-01-01', periods=365)
data = pd.DataFrame({
'ds': dates,
'y': 100 + (dates.dayofyear - 183) ** 2 / 100 + np.random.randn(365) * 5
})

# Step 2: Initialize the Prophet model
model = Prophet()

# Step 3: Fit the model to the data
model.fit(data)

# Step 4: Create a dataframe for future predictions
future = model.make_future_dataframe(periods=30) # Predict 30 days into the future

# Step 5: Predict future values
forecast = model.predict(future)

# Step 6: Plot the forecast
model.plot(forecast)
plt.show()

# Step 7: Plot the forecast components
model.plot_components(forecast)
plt.show()

Explanation

  • ds: The column containing the dates.
  • y: The column containing the values to be forecasted.
  • fit: The method to train the model with your time series data.
  • make_future_dataframe: Prepares a dataframe to hold future predictions.
  • predict: Generates predictions for the given dates.
  • plot: Visualizes the forecast along with the observed data.
  • plot_components: Breaks down the forecast into its components (e.g., trend, weekly seasonality).

Result

Running this code will generate a plot of the time series data with the forecasted values and their uncertainty intervals, as well as a breakdown of the forecast components.