Using exogenous variables and custom periodicity in the Prophet model

Using exogenous variables in the Prophet model

When using Prophet for time series forecasting, we may need to consider some factors that are related to the time series but not reflected in the time series, such as weather, special festivals, etc. These factors are called external regressors. After adding exogenous variables, the Prophet model becomes a multivariate time series model. If the exogenous variables are selected well, the performance of the model may be greatly improved.

1 add_regressorAdd exogenous variables with

Prophet provides add_regressormethods to add exogenous variables to the model to further improve the model's fit and predictive power. Specific steps are as follows:

  1. Preparation : Prepare the data of exogenous variables, including the name of the variable and the corresponding value. After adding exogenous variables, Prophet will automatically standardize them so that their values ​​fall within the range [0, 1].

  2. Add : Use the add_regressor method to add exogenous variables to the model and specify the name of the variable there. For example, suppose we want to add a temperature variable temp to the time series data, we can add it like this:

    # model是你实例化之后的
    model.add_regressor('temp')
    
  3. Training : When training the model, pass the data of exogenous variables into the fit method as a parameter of type DataFrame. For example, assuming the data for the temp variable is stored in a DataFrame named df , the model can be trained like this:

    # 将外生变量添加到训练集中
    df['temp'] = [15, 16, 17, 18, 19]
    # df中还有时间序列数据,如果是饱和预测,还有cap、floor
    model.fit(df)
    
  4. Predictionmake_future_dataframe : When making predictions, if the generated prediction window is used , make_future_dataframethe value of the exogenous variable needs to be specified in the obtained Dateframe; if the split test set is used, the value of the exogenous variable must also be included in the test set. For example, suppose we need to forecast time series data for the next five days:

    # 用make_future_dataframe生成预测窗口
    future = model.make_future_dataframe(periods=5)
    future['temp'] = [20, 21, 22, 23, 24]  # 添加外生变量 temp
    

The complete code for Prophet to add exogenous variables:

"""
利用 Prophet 模型对 example.csv 中的时间序列数据进行建模
同时添加了一个外生变量 temp
并生成了未来五天的预测数据
最后,使用 plot 和 plot_components 方法将预测结果和外生变量的影响进行可视化展示
"""

import pandas as pd
from fbprophet import Prophet  # 或from prophet import Prophet

# 导入数据
df = pd.read_csv('example.csv')
df['ds'] = pd.to_datetime(df['ds'])

# 实例化Prophet 此处我用的默认参数
model = Prophet()

# 添加外生变量
model.add_regressor('temp')

# 训练模型
model.fit(df)

# 生成预测数据 此处我用的make_future_dataframe
future = model.make_future_dataframe(periods=5)
future['temp'] = [20, 21, 22, 23, 24]  # 添加外生变量 temp
forecast = model.predict(future)

# 可视化预测结果和外生变量的影响
fig = model.plot(forecast)
fig = model.plot_components(forecast)

2 add_seasonalityAdd a custom periodicity with

In addition to using exogenous variables, we can also use add_seasonalitythe method to add custom periodic components to better model periodic changes in the time series. It should be noted that this is just to adjust the parameters of the model, not to add additional variables (features) to the model. Specific steps are as follows:

  1. Add : Use add_seasonalitythe method to add a custom periodic component, and you can specify parameters such as the length, name, and intensity of the cycle . For example, if we wanted to add an additional quadratic periodic component to the model , we could add it like this:

    # model就是实例化的Prophet
    model.add_seasonality('custom_period', period=14, fourier_order=2)
    

    Among them, ' custom_period' is the name of the periodic component, period=14 indicates that the period length is 14 (here can be adjusted according to specific needs), fourier_order=2 indicates that two Fourier terms are used to fit the component.

  2. Prediction : When making a forecast, as with the default periodic component, make_future_dataframeset the time interval to be forecasted in the method, and then call the predict method to make the forecast. For example:

    # 生成预测窗口
    future = model.make_future_dataframe(periods=60)
    # 预测
    forecast = model.predict(future)
    

Prophet adds custom periodic complete code:

"""
利用 Prophet 模型对 example.csv 中的时间序列数据进行建模
添加了一个额外的周期性成分(名称为 'custom_period',周期长度为 14,傅里叶项数为 2)
最后,我们使用 plot 方法可视化预测结果
"""

import pandas as pd
from fbprophet import Prophet  # from prophet import Prophet

# 导入数据
df = pd.read_csv('example.csv')
df['ds'] = pd.to_datetime(df['ds'])

# 创建 Prophet 对象
model = Prophet()

# 添加自定义二次周期性成分
model.add_seasonality('custom_period', period=14, fourier_order=2)

# 训练模型
model.fit(df)

# 生成预测数据
future = model.make_future_dataframe(periods=60)
forecast = model.predict(future)

# 可视化预测结果
fig = model.plot(forecast)

Summarize

For different time series, the feature engineering is different, and the parameter tuning optimization method and modeling process may also be quite different. Some adaptive adjustments should be made according to the actual situation. The above is just a methodology and is for reference only.

Guess you like

Origin blog.csdn.net/qq_42774234/article/details/130166037