forecasting values for each category using Prophet in python

前端 未结 2 1827
野趣味
野趣味 2021-01-03 08:13

I am very new to doing time series in Python and Prophet. I have a dataset with the variables article code, date and quantity sold. I am trying to forecast the quantity sold

2条回答
  •  误落风尘
    2021-01-03 08:51

    I know this is old, but I faced a similar problem and this worked for me:

    df = pd.read_csv('file.csv')
    df = pd.DataFrame(df)
    df = df.rename(columns={'Date of the document': 'ds', 'Quantity sold': 'y', 'Article bar code': 'Article'})
    #I filter first Articles bar codes with less than 3 records to avoid errors as prophet only works for 2+ records by group
    df = df.groupby('Article').filter(lambda x: len(x) > 2)
    
    df.Article = df.Article.astype(str)
    
    final = pd.DataFrame(columns=['Article','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 add a column with Article bar code
        forecast['Article'] = g
        #I concad all results in one dataframe
        final = pd.concat([final, forecast], ignore_index=True)
    
    final.head(10)
    

提交回复
热议问题