Simple sales forecast using prophet in python

1. Import data

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

The screenshot of the data part is as follows:
1

2. Fit and view the results

Fit directly:

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

Print the time period of the forecast, the screenshot is as follows:
2.1
Take the constructed date data for forecasting:

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

After running the above code, the effect is as follows:
2.2
View the predicted image and specific data

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

After running the above code, the rendering is as follows:
2.3

3 Optimization direction

  • Add holidays
  • Add special points
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)))

Guess you like

Origin blog.csdn.net/qq_41780234/article/details/128567321