在python中使用prophet进行简单的销量预测

1.引入数据

path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/monthly-car-sales.csv'
df = pd.read_csv(path, header=0)
#进行数据处理,模型要求输入的数据必须包含以下两列:ds、y
df.columns=['ds','y']
df['ds']=pd.to_datetime(df['ds']+'-01')
df

数据部分截图,如下:
1

2.进行拟合并查看结果

直接进行拟合:

model = Prophet()
#拟合
model.fit(df)
# 构建待预测日期数据框,months代表要预测的月数
future = model.make_future_dataframe(periods=8,freq="M")
future.tail()

打印预测的时间周期,截图如下:
2.1
拿构建的日期数据进行预测:

# 预测数据集
forecast = model.predict(future)
model.plot_components(forecast);

运行上述代码后,效果如下:
2.2
查看预测的图像和具体数据

model.plot(forecast);
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())

运行以上代码后,效果图如下:
2.3

3 优化方向

  • 增加节假日
  • 增加特殊点
def mean_absolute_percentage_error(y_true, y_pred): 
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

max_date = df.ds.max()
y_true = df.y.values
y_pred_daily = forecast.loc[forecast['ds'] <= max_date].yhat.values
y_pred_daily_2 = forecast_2.loc[forecast_2['ds'] <= max_date].yhat.values

print('包含周、日季节性 MAPE: {}'.format(mean_absolute_percentage_error(y_true,y_pred_daily)))
print('不包含周、日季节性 MAPE: {}'.format(mean_absolute_percentage_error(y_true,y_pred_daily_2)))

猜你喜欢

转载自blog.csdn.net/qq_41780234/article/details/128567321
今日推荐