【问题标题】:forecasting values for each category using Prophet in python在 python 中使用 Prophet 预测每个类别的值
【发布时间】:2018-03-12 14:32:32
【问题描述】:

我对用 Python 和 Prophet 做时间序列非常陌生。我有一个包含变量文章代码、日期和销售数量的数据集。我正在尝试使用 Python 中的 Prophet 预测每个月每篇文章的销售量。

我尝试使用 for 循环对每篇文章执行预测,但我不确定如何在输出(预测)数据中显示文章类型,以及如何直接从“for 循环”将其写入文件。

df2 = df2.rename(columns={'Date of the document': 'ds','Quantity sold': 'y'})
for article in df2['Article bar code']:

    # 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(df2)
    future_dates = my_model.make_future_dataframe(periods=6, freq='MS')
    forecast = my_model.predict(future_dates)
return forecast

我想要如下所示的输出,并希望将其直接从“for 循环”写入输出文件。

提前致谢。

【问题讨论】:

    标签: python for-loop time-series facebook-prophet


    【解决方案1】:

    articletype 分隔您的数据框,然后尝试将所有预测值存储在字典中

    def get_prediction(df):
        prediction = {}
        df = df.rename(columns={'Date of the document': 'ds','Quantity sold': 'y', 'Article bar code': 'article'})
        list_articles = df2.article.unique()
    
        for article in list_articles:
            article_df = df2.loc[df2['article'] == article]
            # 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[article] = forecast
        return prediction
    

    现在预测将对每种类型的文章进行预测。

    【讨论】:

    • 我现在已经执行了解决方案,但是由于文章条码的总层数在2500左右,记录数超过3M,所以需要很长时间。将检查并更新答案。谢谢@Vj-
    • @Vj 第 3 行有错字 df = df.rename(columns={'Date of the document': 'ds','Quantity sold': 'y', 'Article bar code': 'article'}) 应该是:df2 = df.rename(columns={'Date of the document': 'ds','Quantity sold': 'y', 'Article bar code': 'article'}) 否?
    • 您是否设法将预测字典转换为数据帧?你知道这样做的解决方案吗?
    【解决方案2】:

    我知道这是旧的,但我遇到了类似的问题,这对我有用:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多