在fbprophet模型中使用几个自变量

时间:2018-08-31 15:38:56

标签: python python-3.x

我倾向于像这样使用fbprophet来单变量预测时间序列:

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

plt.style.use('fivethirtyeight')

stock = 'FB'

df = pd.read_csv('C:/Bla/' + stock + '.csv')
df['Date'] = pd.DatetimeIndex(df['Date'])

df = df.rename(columns={'Date': 'ds',
                        'Close': 'y'})

my_model = Prophet(
    weekly_seasonality=True
    , interval_width=0.95
)
my_model.fit(df)

future_dates = my_model.make_future_dataframe(periods=5, freq='B')
future_dates.tail()

forecast = my_model.predict(future_dates)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

my_model.plot(forecast, uncertainty=True)
plt.show()

我认为现在也可以使用多个自变量进行预测(使用add_regressor?)。有人这样做吗?如果是这样,您能否提供一些代码示例?让我们假设自变量在上述df中称为X1。谢谢!

1 个答案:

答案 0 :(得分:0)

除非在拟合过程中将其添加,否则无法使用其他回归器进行预测。如果您想将回归变量添加到模型中,则应遵循以下步骤(直接从文档中获取):

def nfl_sunday(ds):
    date = pd.to_datetime(ds)
    if date.weekday() == 6 and (date.month > 8 or date.month < 2):
         return 1

    else:
         return 0

df['nfl_sunday'] = df['ds'].apply(nfl_sunday)

m = Prophet()
m.add_regressor('nfl_sunday')
m.fit(df)

future['nfl_sunday'] = future['ds'].apply(nfl_sunday)

forecast = m.predict(future)
fig = m.plot_components(forecast)