安装Prophet
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
Prophet介绍
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分钟
预测未来1
利用函数make_future_dataframe生成未来时间DataFrame对象future :
from fbprophet import Prophet
import pandas as pd
import matplotlib.pyplot as plt
import mathtimelist = 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) future = m.make_future_dataframe(periods=50, freq='H',include_history=False)
forecast = m.predict(future)
plt.plot(list(data_df['ds'][-50:])+list(forecast['ds']),list(data_df['y'][-50:])+list(forecast['yhat']), color='b')
plt.show()
预测未来2
未来任意时间:
from fbprophet import Prophet
import pandas as pd
import matplotlib.pyplot as plt
import mathtimelist = 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) 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(forecast['ds'], forecast['yhat'], color='r')
plt.show()
预测模型持久化存储