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

后端 未结 3 1661
盖世英雄少女心
盖世英雄少女心 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:33

    I know this is old but I was trying to predict outcomes for different clients and I tried to use Aditya Santoso solution above but got into some errors, so I added a couple of modifications and finally this worked for me:

    df = pd.read_csv('file.csv')
    df = pd.DataFrame(df)
    df = df.rename(columns={'date': 'ds', 'amount': 'y', 'client_id': 'client_id'})
    #I had to filter first clients with less than 3 records to avoid errors as prophet only works for 2+ records by group
    df = df.groupby('client_id').filter(lambda x: len(x) > 2)
    
    df.client_id = df.client_id.astype(str)
    
    final = pd.DataFrame(columns=['client','ds','yhat'])
    
    grouped = df.groupby('client_id')
    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)
        #I added a column with client id
        forecast['client'] = g
        #I used concat instead of merge
        final = pd.concat([final, forecast], ignore_index=True)
    
    final.head(10)
    

提交回复
热议问题