问题
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.
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
I want the output like below, and want this to be written to an output file directly from the "for loop".
Thanks in Advance.
回答1:
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.
来源:https://stackoverflow.com/questions/49237785/forecasting-values-for-each-category-using-prophet-in-python