2021-12-22用Prophet库时间预测

https://www.jiqizhixin.com/articles/2020-10-09-3


Prophet库,由Facebook开发,用途是时间序列预测,这个库的接口在R和Python中可调用


基于可加模型的时间序列数据预测过程,拟合了年度、周度、日度的季节性周期变化及节假日效应的非线性趋势。 Prophet的设计初衷就是简单易用、完全自动,因此适合在公司内部场景中使用,例如预测销量、产能等。  


#安装  ,windows7环境下

pip install fbprophet 不行,可以下载,不能安装,显示failed to build fbprophet

conda install fbprophet 不行,找不到安装包

conda install -c conda-forge fbprophet Alternatively 也不行

安装fbprophet 办法  https://www.zhangweijiang.com/article/163.html


#使用

import fbprophet

from pandas import read_csv

from matplotlib import pyplot


# load data

path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/monthly-car-sales.csv'

df = read_csv(path, header=0)

# prepare expected column names

df.columns = ['ds', 'y']

df['ds']= to_datetime(df['ds'])

# create test dataset, remove last 12 months

train = df.drop(df.index[-12:])

print(train.tail())

# define the model

model = Prophet()

# fit the model

model.fit(train)

# define the period for which we want a prediction

future = list()

for i in range(1, 13):

date = '1968-%02d' % i

future.append([date])

future = DataFrame(future)

future.columns = ['ds']

future['ds'] = to_datetime(future['ds'])

# use the model to make a forecast

forecast = model.predict(future)

# calculate MAE between expected and predicted values for december

y_true = df['y'][-12:].values

y_pred = forecast['yhat'].values

mae = mean_absolute_error(y_true, y_pred)

print('MAE: %.3f' % mae)

# plot expected vs actual

pyplot.plot(y_true, label='Actual')

pyplot.plot(y_pred, label='Predicted')

pyplot.legend()

pyplot.show()




你可能感兴趣的:(2021-12-22用Prophet库时间预测)