我的数据如下所示,我正在使用 facebookFbProphet
进行预测。接下来我想SMAPE
为我的数据框中的每个组计算。我在这里找到了 Kaggle 用户描述的功能但我不确定如何在我当前的代码中实现。这样SMAPE
就可以为每个组计算。另外,我知道 fbProphet 具有验证功能,但我想SMAPE
为每个组计算。
注意:我是 python 新手,请提供代码解释。
数据集
import pandas as pd
data = {'Date':['2017-01-01', '2017-01-01','2017-01-01','2017-01-01','2017-01-01','2017-01-01','2017-01-01','2017-01-01',
'2017-02-01', '2017-02-01','2017-02-01','2017-02-01','2017-02-01','2017-02-01','2017-02-01','2017-02-01'],'Group':['A','A','B','B','C','C','D','D','A','A','B','B','C','C','D','D'],
'Amount':['12.1','13.2','15.1','10.7','12.9','9.0','5.6','6.7','4.3','2.3','4.0','5.6','7.8','2.3','5.6','8.9']}
df = pd.DataFrame(data)
print (df)
到目前为止的代码...
def get_prediction(df):
prediction = {}
df = df.rename(columns={'Date': 'ds','Amount': 'y', 'Group': 'group'})
df=df.groupby(['ds','group'])['y'].sum()
df=pd.DataFrame(df).reset_index()
list_articles = df.group.unique()
for group in list_articles:
article_df = df.loc[df['group'] == group]
# set the uncertainty interval to 95% (the Prophet default is 80%)
my_model = Prophet(weekly_seasonality= True, daily_seasonality=True,seasonality_prior_scale=1.0)
my_model.fit(article_df)
future_dates = my_model.make_future_dataframe(periods=6, freq='MS')
forecast = my_model.predict(future_dates)
prediction[group] = forecast
my_model.plot(forecast)
return prediction