in python/prophet/diagnostics.py [0:0]
def single_cutoff_forecast(df, model, cutoff, horizon, predict_columns):
"""Forecast for single cutoff. Used in cross validation function
when evaluating for multiple cutoffs either sequentially or in parallel .
Parameters
----------
df: pd.DataFrame.
DataFrame with history to be used for single
cutoff forecast.
model: Prophet model object.
cutoff: pd.Timestamp cutoff date.
Simulated Forecast will start from this date.
horizon: pd.Timedelta forecast horizon.
predict_columns: List of strings e.g. ['ds', 'yhat'].
Columns with date and forecast to be returned in output.
Returns
-------
A pd.DataFrame with the forecast, actual value and cutoff.
"""
# Generate new object with copying fitting options
m = prophet_copy(model, cutoff)
# Train model
history_c = df[df['ds'] <= cutoff]
if history_c.shape[0] < 2:
raise Exception(
'Less than two datapoints before cutoff. '
'Increase initial window.'
)
m.fit(history_c, **model.fit_kwargs)
# Calculate yhat
index_predicted = (df['ds'] > cutoff) & (df['ds'] <= cutoff + horizon)
# Get the columns for the future dataframe
columns = ['ds']
if m.growth == 'logistic':
columns.append('cap')
if m.logistic_floor:
columns.append('floor')
columns.extend(m.extra_regressors.keys())
columns.extend([
props['condition_name']
for props in m.seasonalities.values()
if props['condition_name'] is not None])
yhat = m.predict(df[index_predicted][columns])
# Merge yhat(predicts), y(df, original data) and cutoff
return pd.concat([
yhat[predict_columns],
df[index_predicted][['y']].reset_index(drop=True),
pd.DataFrame({'cutoff': [cutoff] * len(yhat)})
], axis=1)