python: 3.7.9
pystan: 2.19.0.0
pandas
fbprophet: 0.6.0
anaconda方式:
conda install pystan=2.19.0.0
conda install -c conda-forge fbprophet=0.6.0
https://www.cnblogs.com/bonelee/p/9577432.html
https://facebook.github.io/prophet/docs/quick_start.html
可以预测数据,也可以给出趋势。时间序列预测上,充满专家经验:周期趋势、离群点、突变点、突变。
make_future_dataframe中的fre是Offset aliases形式的,用的pandas时间跨度
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases
比如1hour1min可以表示时间跨度是61分钟
利用函数make_future_dataframe生成未来时间DataFrame对象future :
pythonfrom fbprophet import Prophet
import pandas as pd
import matplotlib.pyplot as plt
import math
timelist = list(pd.date_range(start='2021-01-01 00:00:00', end='2022-01-01 00:00:00', freq='H'))
y = [math.sin(data.hour) for k, data in enumerate(timelist)]
data_df = pd.DataFrame({'ds': timelist, 'y': y})
data_df['ds'] = data_df['ds'].astype('datetime64[ns]')
m = Prophet()
m.fit(data_df) # 训练模型m
future = m.make_future_dataframe(periods=50, freq='H',include_history=False) # 预测的设置 还没预测
forecast = m.predict(future) # 开始预测
# plt.plot(data_df['ds'][-50:], data_df['y'][-50:], color='b')
# plt.plot(forecast['ds'], forecast['yhat'], color='r')
# plt.show()
plt.plot(list(data_df['ds'][-50:])+list(forecast['ds']),
list(data_df['y'][-50:])+list(forecast['yhat']), color='b')
plt.show()
未来任意时间:
python# 1 取数据
# https://www.kesci.com/mw/dataset/5d64a35a8499bc002c07bb1b
from fbprophet import Prophet
import pandas as pd
import matplotlib.pyplot as plt
import math
timelist = list(pd.date_range(start='2021-01-01 00:00:00', end='2022-01-01 00:00:00', freq='H'))
y = [math.sin(data.hour) for k, data in enumerate(timelist)]
data_df = pd.DataFrame({'ds': timelist, 'y': y})
data_df['ds'] = data_df['ds'].astype('datetime64[ns]')
m = Prophet()
m.fit(data_df) # 训练模型m
future = pd.DataFrame({'ds': list(pd.date_range(start='2022-05-01 00:00:00', end='2022-05-05 00:00:00', freq='H'))}) # 预测的设置 还没预测
forecast = m.predict(future) # 开始预测
# plt.plot(data_df['ds'][-50:], data_df['y'][-50:], color='b')
plt.plot(forecast['ds'], forecast['yhat'], color='r')
plt.show()
# plt.plot(list(data_df['ds'][-50:])+list(forecast['ds']),
# list(data_df['y'][-50:])+list(forecast['yhat']), color='b')
# plt.show()
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!