【问题标题】:forecasting two groups with prophet用先知预测两组
【发布时间】:2021-10-11 13:56:51
【问题描述】:

您好,我正在尝试预测每个商店和商品的以下单位

df.head()
ds  item    store   y
0   2019-01-13  A18-303 31  1.000
1   2021-02-21  104451-N04  3   2.000
2   2021-03-07  K231-G39    26  1.000
3   2021-01-10  K288-G39    26  -1.000
4   2019-06-30  A18-303 6   6.000
final = pd.DataFrame(columns=['item','store','ds','yhat'])    
grouped = df.groupby('store','item')
    for g in grouped.groups:
        group = grouped.get_group(g)
        m = Prophet()
        m.fit(group)
        future = m.make_future_dataframe(periods=14)
        forecast = m.predict(future)
        forecast['item','store'] = g
        final = pd.concat([final, forecast], ignore_index=True)

我的结果是所有商店和商品的 NaN。

【问题讨论】:

    标签: python pandas time-series forecasting facebook-prophet


    【解决方案1】:

    这是因为您没有正确定义 final 数据框。

    pd.concat([final, forecast], ignore_index=True) 第一次运行时,final 仍然未定义,因此会产生错误。

    您可以执行以下操作:

    grouped = df.groupby(['store','item'])
    dfs =[]  # list of dataframes 
    for g in grouped.groups:
        group = grouped.get_group(g)
        m = Prophet()
        m.fit(group)
        future = m.make_future_dataframe(periods=14)
        forecast = m.predict(future)
        forecast['item','store'] = g
        dfs.append(forecast)
    final = pd.concat(dfs, ignore_index=True)
    

    【讨论】:

    • 我不确定我是否遵循我作为数据框列表放置的内容
    • @kmox 我只是像在代码中那样附加forecast 数据框。唯一的区别是在定义之前不能使用 'final' 与自身连接。
    • 谢谢@Mohammad 我看到我遗漏了定义final 的代码行。那就是说我仍然无法在 groupby 中同时使用 store 和 item 运行
    • @knox 您忘记将这两项添加为 groupby 中的列表。检查我的更新。
    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多