How to perform time series analysis that contains multiple groups in Python using fbProphet or other models?

后端 未结 3 1664
盖世英雄少女心
盖世英雄少女心 2020-12-16 00:19

All,

My dataset looks like following. I am trying to predict the \'amount\' for next 6 months using either the fbProphet or other model. But my issue is

3条回答
  •  孤城傲影
    2020-12-16 00:35

    fbprophet requires two columns ds and y, so you need to first rename the two columns

    df = df.rename(columns={'Date': 'ds', 'Amount':'y'})
    

    Assuming that your groups are independent from each other and you want to get one prediction for each group, you can group the dataframe by "Group" column and run forecast for each group

    from fbprophet import Prophet
    grouped = df.groupby('Group')
    for g in grouped.groups:
        group = grouped.get_group(g)
        m = Prophet()
        m.fit(group)
        future = m.make_future_dataframe(periods=365)
        forecast = m.predict(future)
        print(forecast.tail())
    

    Take note that the input dataframe that you supply in the question is not sufficient for the model because group D only has a single data point. fbprophet's forecast needs at least 2 non-Nan rows.

    EDIT: if you want to merge all predictions into one dataframe, the idea is to name the yhat for each observations differently, do pd.merge() in the loop, and then cherry-pick the columns that you need at the end:

    final = pd.DataFrame()
    for g in grouped.groups:
        group = grouped.get_group(g)
        m = Prophet()
        m.fit(group)
        future = m.make_future_dataframe(periods=365)
        forecast = m.predict(future)    
        forecast = forecast.rename(columns={'yhat': 'yhat_'+g})
        final = pd.merge(final, forecast.set_index('ds'), how='outer', left_index=True, right_index=True)
    
    final = final[['yhat_' + g for g in grouped.groups.keys()]]
    

提交回复
热议问题