参数:
注意df的列名必须是["holiday","ds","lower_window","upper_window"]
# 以30分钟为间隔
model = Prophet(growth="linear", changepoint_range=0.8, changepoint_prior_scale=0.5) # 默认的增长趋势为linear
df["cap"] = 24 # 如果使用growth="logistic",则需要显示指定cap, logistic默认的最小饱和值是0,当然也可以自己指定
df["floor"] = 1.5 # 最小饱和值
model.fit(df) # 训练模型
future = model.make_future_dataframe(periods=3, freq="D") # 预测未来三天的结果,freq默认为天
future["cap"] = 24 # 与训练集保持一致
future["floor"] = 1.5
pred = model.predict(future)
fig = model.plot(pred) # 画出预测情况
# fig = m.plot_components(pred) # 画出预测值的各个组成部分
from fbprophet.plot import add_changepoints_to_plot
a = add_changepoints_to_plot(fig.gca(), model, pred) # 画出改变点的位置
# 季节趋势
# prophet默认拟合周和年周期,但是可以加入其他周期
model = Prophet(weekly_seasonality=False)
model.add_seasonality(name="monthly", period=30.5, fourier_order=5, prior_scale=0.1) # period以天为单位
pred = model.fit(df).predict(future)
fig = model.plot_components(pred)
# 划分训练集与测试集
split_date = '01-Jan-2015'
pjme_train = pjme.loc[pjme.index <= split_date].copy()
pjme_test = pjme.loc[pjme.index > split_date].copy()
# Format data for prophet model using ds and y
pjme_train.reset_index().rename(columns={'Datetime':'ds', 'PJME_MW':'y'}).head()
# Setup and train model
model = Prophet()
model.fit(pjme_train.reset_index().rename(columns={'Datetime':'ds', 'PJME_MW':'y'}))
# Predict on training set with model
pjme_test_fcst = model.predict(df=pjme_test.reset_index().rename(columns={'Datetime':'ds'}))
# Plot the forecast
f, ax = plt.subplots(1)
f.set_figheight(5)
f.set_figwidth(15)
fig = model.plot(pjme_test_fcst, ax=ax)
# Plot the components
fig = model.plot_components(pjme_test_fcst)
# Plot the forecast with the actuals
f, ax = plt.subplots(1)
f.set_figheight(5)
f.set_figwidth(15)
ax.scatter(pjme_test.index, pjme_test['PJME_MW'], color='r')
fig = model.plot(pjme_test_fcst, ax=ax)
# Plot the forecast with the actuals
f, ax = plt.subplots(1)
f.set_figheight(5)
f.set_figwidth(15)
ax.scatter(pjme_test.index, pjme_test['PJME_MW'], color='r')
fig = model.plot(pjme_test_fcst, ax=ax)
ax.set_xbound(lower='01-01-2015', upper='02-01-2015')
ax.set_ylim(0, 60000)
plot = plt.suptitle('January 2015 Forecast vs Actuals')
# Plot the forecast with the actuals
f, ax = plt.subplots(1)
f.set_figheight(5)
f.set_figwidth(15)
ax.scatter(pjme_test.index, pjme_test['PJME_MW'], color='r')
fig = model.plot(pjme_test_fcst, ax=ax)
ax.set_xbound(lower='01-01-2015', upper='01-08-2015')
ax.set_ylim(0, 60000)
plot = plt.suptitle('First Week of January Forecast vs Actuals')
mean_squared_error(y_true=pjme_test['PJME_MW'],
y_pred=pjme_test_fcst['yhat'])
mean_absolute_error(y_true=pjme_test['PJME_MW'],
y_pred=pjme_test_fcst['yhat']
# Adding Holidays
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar
# 美国联邦假期日历函数
cal = calendar()
train_holidays = cal.holidays(start=pjme_train.index.min(), end=pjme_train.index.max())
test_holidays = cal.holidays(start=pjme_test.index.min(), end=pjme_test.index.max())
# Create a dataframe with holiday, ds columns
pjme['date'] = pjme.index.date
pjme['is_holiday'] = pjme.date.isin([d.date() for d in cal.holidays()])
holiday_df = pjme.loc[pjme['is_holiday']].reset_index().rename(columns={'Datetime':'ds'})
holiday_df['holiday'] = 'USFederalHoliday'
holiday_df = holiday_df.drop(['PJME_MW','date','is_holiday'], axis=1)
holiday_df.head()
# Setup and train model with holidays
model_with_holidays = Prophet(holidays=holiday_df)
model_with_holidays.fit(pjme_train.reset_index().rename(columns={'Datetime':'ds', 'PJME_MW':'y'}))
fig2 = model_with_holidays.plot_components(pjme_test_fcst)
# Predict on training set with model
pjme_test_fcst_with_hols = model_with_holidays.predict(df=pjme_test.reset_index().rename(columns={'Datetime':'ds'}))