python fbprophet 初级 使用

"""

# 1 安装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
# 3 Prophet介绍
# https://www.cnblogs.com/bonelee/p/9577432.html
# https://facebook.github.io/prophet/docs/quick_start.html
# 可以预测数据,也可以给出趋势。时间序列预测上,充满专家经验:周期趋势、离群点、突变点、突变。
# 4 时间跨度
# make_future_dataframe中的fre是Offset aliases形式的,用的pandas时间跨度
# https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases
# 比如1hour1min可以表示时间跨度是61分钟

"""
import datetime

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


def get_all_time_stamp(num):
    """
    :return:
    """
    starttime = datetime.datetime(2020, 1, 1, 0, 0, 0)
    deltatime = datetime.timedelta(hours=1)
    time_list = []

    cnt = 0
    while 1:
        if cnt >= num:
            break
        else:
            time_list.append(starttime)
            starttime += deltatime
            cnt += 1

    return time_list


ts = get_all_time_stamp(1000)
value_list = [100 * math.sin(i) for i in range(1000)]

data_df = pd.DataFrame({
    
    'date_time': ts, 'value': value_list})

data_df.columns = ['ds', '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')  # 预测的设置 还没预测
forecast = m.predict(future)  # 开始预测
print(forecast.to_string())  # 预测结果显示

# fig1 = m.plot(forecast)  #预测结果绘图
plt.plot(forecast['ds'][950:1000], forecast['yhat'][950:1000], color='b')
plt.plot(forecast['ds'][1000:], forecast['yhat'][1000:], color='r')
plt.show()

猜你喜欢

转载自blog.csdn.net/x1131230123/article/details/114374459
今日推荐