gpt4 book ai didi

python-3.x - 如何在Python中计算时间序列数据组的SMAPE?

转载 作者:行者123 更新时间:2023-12-02 17:25:19 25 4
gpt4 key购买 nike

我的数据如下所示,我使用 facebook FbProphet 进行预测。接下来,我想计算数据框中每个组的 SMAPE 。我找到了Kaggle用户here描述的功能但我不确定如何在我当前的代码中实现。这样 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

最佳答案

您仍然可以使用 fbprophet 自己的 cross_validation 函数,但使用您自己的评分。这是 uber 的一篇不错的博客,介绍了他们如何进行回溯测试(滑动窗口和扩展窗口):https://eng.uber.com/forecasting-introduction/

fbprophet 的 cv 函数在滑动窗口上运行。如果可以的话,您可以将其与自定义评分函数结合使用。我认为一个很好的方法是扩展 Prophet 并实现 .score() 方法。

这里是一个示例实现:

from fbprophet import Prophet
from fbprophet.diagnostics import cross_validation
import numpy as np

class ProphetEstimator(Prophet):
"""
Wrapper with custom scoring
"""

def __init__(self, *args, **kwargs):
super(ProphetEstimator, self).__init__(*args, **kwargs)

def score(self):
# cross val score reusing prophets own cv implementation
df_cv = cross_validation(self, horizon='6 days')
# Here decide how you want to calculate SMAPE.
# Here each sliding window is summed up,
# and the SMAPE is calculated over the sum of periods, for all windows.
df_cv = df_cv.groupby('cutoff').agg({
"yhat": "sum",
'y': "sum"
})
smape = self.calc_smape(df_cv['yhat'], df_cv['y'])
return smape

def calc_smape(self, y_hat, y):
return 100/len(y) * np.sum(2 * np.abs(y_hat - y) / (np.abs(y) + np.abs(y_hat)))


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 = ProphetEstimator(weekly_seasonality= True, daily_seasonality=True,seasonality_prior_scale=1.0)
my_model.fit(article_df)
smape = my_model.score() # store this somewhere
future_dates = my_model.make_future_dataframe(periods=6, freq='MS')
forecast = my_model.predict(future_dates)
prediction[group] = (forecast, smape)
my_model.plot(forecast)
return prediction

关于python-3.x - 如何在Python中计算时间序列数据组的SMAPE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55892568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com