近日在尝试使用fbprophet算法替换原来的ARIMA做时序预测,安装过程中踩过的坑先不提(后面有时间会另起一篇说明),此处单说一个很莫名其妙的问题。
在跑官方示例的时候:
import pandas as pd
import numpy as np
from fbprophet import Prophet
import matplotlib.pyplot as plt
df = pd.read_excel('example_wp_peyton_manning.xlsx')
df['y'] = np.log(df['y'])
playoffs = pd.DataFrame({
'holiday': 'playoff',
'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16',
'2010-01-24', '2010-02-07', '2011-01-08',
'2013-01-12', '2014-01-12', '2014-01-19',
'2014-02-02', '2015-01-11', '2016-01-17',
'2016-01-24', '2016-02-07']),
'lower_window': 0,
'upper_window': 1,
})
superbowls = pd.DataFrame({
'holiday': 'superbowl',
'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),
'lower_window': 0,
'upper_window': 1,
})
holidays = pd.concat((playoffs, superbowls)) # 季后赛和超级碗比赛特别日期
m = Prophet(holidays=holidays) # 指定节假日参数,其它参数以默认值进行训练
m.fit(df) # 对过去数据进行训练
future = m.make_future_dataframe(freq='D',periods=365) # 建立数据预测框架,数据粒度为天,预测步长为一年
forecast =m.predict(future)
m.plot(forecast) # 绘制预测效果图
plt.show()
m.plot_components(forecast) # 绘制成分趋势图
plt.show()
有可能会遇到这样一个报错:
Traceback (most recent call last):
File "e:/predict/nu_prodict_prophet.py", line 40, in
main()
File "e:/predict/nu_prodict_prophet.py", line 33, in main
forecast = m.predict(future)
File "D:Continuumanaconda3libsite-packagesfbprophetforecaster.py", line 1175, in predict
seasonal_components = self.predict_seasonal_components(df)
File "D:Continuumanaconda3libsite-packagesfbprophetforecaster.py", line 1290, in predict_seasonal_components
self.make_all_seasonality_features(df)
File "D:Continuumanaconda3libsite-packagesfbprophetforecaster.py", line 744, in make_all_seasonality_features
holidays = self.construct_holiday_dataframe(df['ds'])
File "D:Continuumanaconda3libsite-packagesfbprophetforecaster.py", line 467, in construct_holiday_dataframe
all_holidays = all_holidays.drop(index_to_drop)
File "D:Continuumanaconda3libsite-packagespandascoreframe.py", line 3694, in drop
errors=errors)
File "D:Continuumanaconda3libsite-packagespandascoregeneric.py", line 3108, in drop
obj = obj._drop_axis(labels, axis, level=level, errors=errors)
File "D:Continuumanaconda3libsite-packagespandascoregeneric.py", line 3158, in _drop_axis
raise KeyError('{} not found in axis'.format(labels))
KeyError: '[] not found in axis'
这个错并不是一定出现,至少在我百度到关于fbprophet的博客中,几乎都没有遇到同样问题的
后来还是在fbprophet的github上看到有人提了这个issue
这个报错只有在特定的python、pandas、fbprophet版本同时满足会出现(我的版本:python:3.6.5,pandas:0.23.0,fbprophet:0.5,所以我是有多幸运…………)
至于解决方案,issue中也给出了:https://github.com/facebook/prophet/issues/821#issuecomment-461996281
Just reset the index on the holidays dataframe before passing it in to fbprophet.
holiday_dataframe = holiday_dataframe.reset_index() # THE FIX
待续,后面的坑以后再记录