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:
Install Prophet (if you haven’t already):
1
pip install prophet
Import Required Libraries:
1
2
3import pandas as pd
from prophet import Prophet
import matplotlib.pyplot as pltLoad 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
})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)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)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()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 | import pandas as pd |
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.