使用python中的Prophet预测每个类别的值

问题描述 投票:2回答:1

我很擅长用Python和Prophet做时间序列。我有一个数据集,其中包含变量商品代码,销售日期和数量。我想用python中的Prophet来预测每个月每篇文章的销量。 Dataset

我尝试使用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循环”写入输出文件。

Output Expected

提前致谢。

python for-loop time-series facebook-prophet
1个回答
2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.