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

forecasting values for each category using Prophet in python(在 python 中使用 Prophet 预测每个类别的值)
本文介绍了在 python 中使用 Prophet 预测每个类别的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 for each article for each month using Prophet in python.

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

I tried using for loop for performing the forecast for each article, But I am not sure how to display the article type in output(forecast) data and also write it to a file directly from the "for loop".

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 循环"写入输出文件.

I want the output like below, and want this to be written to an output file directly from the "for loop".

提前致谢.

推荐答案

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

Separate your dataframe by articletype and then try storing all your predicted values in a dictionary

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

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

now the prediction will have forecasts for each type of article.

这篇关于在 python 中使用 Prophet 预测每个类别的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)