Сначала мы попробуем создать воспроизводимый пример:
Данные и импорт
from fbprophet import Prophet
import numpy as np
import pandas as pd
df = pd.util.testing\
.makeTimeDataFrame()\
.reset_index()\
.rename(columns={"index":"Date"})
df = pd.melt(df,
id_vars=["Date"],
var_name="item_no",
value_vars=df.columns[1:],
value_name="qty_ordered")
Использование вашей функции
Здесь я только что немного почистил ваш код
# This returns a dict
def get_prediction(df):
prediction = {}
df = df.rename(columns={'Date': 'ds','qty_ordered': 'y', 'item_no': 'item'})
list_items = df.item.unique()
for item in list_items:
item_df = df.loc[df['item'] == item]
# set the uncertainty interval to 95% (the Prophet default is 80%)
model = Prophet(yearly_seasonality= True, seasonality_prior_scale=1.0)
model.fit(item_df)
future_dates = model.make_future_dataframe(periods=12, freq='M')
forecast = model.predict(future_dates)
prediction[item] = forecast
return prediction
df1 = get_prediction(df)
# From dictionary to df
df2 = []
for k,v in df1.items():
v.insert(1, "item", [k]*len(v))
df2.append(v)
df2 = pd.concat(df2, ignore_index=True)
Предсказание вывода df
Если вы немного измените свою функцию, вы можете получить dataframe
в качестве вывода:
def get_prediction_pandas(df):
prediction = []
df = df.rename(columns={'Date': 'ds','qty_ordered': 'y', 'item_no': 'item'})
list_items = df.item.unique()
for item in list_items:
item_df = df.loc[df['item'] == item]
# set the uncertainty interval to 95% (the Prophet default is 80%)
model = Prophet(yearly_seasonality= True, seasonality_prior_scale=1.0)
model.fit(item_df)
future_dates = model.make_future_dataframe(periods=12, freq='M')
forecast = model.predict(future_dates)
forecast.insert(1, "item_no", [item]*len(forecast))
prediction.append(forecast)
prediction = pd.concat(prediction, ignore_index=True)
return prediction
# this is a dataframe
df1 = get_prediction_pandas(df)