mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-09-15 22:10:22 +00:00
Add predict_columns parameter to cross validation (#2486)
This commit is contained in:
parent
265ac0550b
commit
53b9b1a6be
2 changed files with 24 additions and 4 deletions
|
|
@ -58,7 +58,7 @@ def generate_cutoffs(df, horizon, initial, period):
|
||||||
return list(reversed(result))
|
return list(reversed(result))
|
||||||
|
|
||||||
|
|
||||||
def cross_validation(model, horizon, period=None, initial=None, parallel=None, cutoffs=None, disable_tqdm=False):
|
def cross_validation(model, horizon, period=None, initial=None, parallel=None, cutoffs=None, disable_tqdm=False, extra_output_columns=None):
|
||||||
"""Cross-Validation for time series.
|
"""Cross-Validation for time series.
|
||||||
|
|
||||||
Computes forecasts from historical cutoff points, which user can input.
|
Computes forecasts from historical cutoff points, which user can input.
|
||||||
|
|
@ -82,8 +82,6 @@ def cross_validation(model, horizon, period=None, initial=None, parallel=None, c
|
||||||
cross validation. If not provided, they are generated as described
|
cross validation. If not provided, they are generated as described
|
||||||
above.
|
above.
|
||||||
parallel : {None, 'processes', 'threads', 'dask', object}
|
parallel : {None, 'processes', 'threads', 'dask', object}
|
||||||
disable_tqdm: if True it disables the progress bar that would otherwise show up when parallel=None
|
|
||||||
|
|
||||||
How to parallelize the forecast computation. By default no parallelism
|
How to parallelize the forecast computation. By default no parallelism
|
||||||
is used.
|
is used.
|
||||||
|
|
||||||
|
|
@ -109,6 +107,10 @@ def cross_validation(model, horizon, period=None, initial=None, parallel=None, c
|
||||||
for args in zip(*iterables)
|
for args in zip(*iterables)
|
||||||
]
|
]
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
disable_tqdm: if True it disables the progress bar that would otherwise show up when parallel=None
|
||||||
|
extra_output_columns: A String or List of Strings e.g. 'trend' or ['trend'].
|
||||||
|
Additional columns to 'yhat' and 'ds' to be returned in output.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
|
@ -120,10 +122,15 @@ def cross_validation(model, horizon, period=None, initial=None, parallel=None, c
|
||||||
|
|
||||||
df = model.history.copy().reset_index(drop=True)
|
df = model.history.copy().reset_index(drop=True)
|
||||||
horizon = pd.Timedelta(horizon)
|
horizon = pd.Timedelta(horizon)
|
||||||
|
|
||||||
predict_columns = ['ds', 'yhat']
|
predict_columns = ['ds', 'yhat']
|
||||||
|
|
||||||
if model.uncertainty_samples:
|
if model.uncertainty_samples:
|
||||||
predict_columns.extend(['yhat_lower', 'yhat_upper'])
|
predict_columns.extend(['yhat_lower', 'yhat_upper'])
|
||||||
|
|
||||||
|
if extra_output_columns is not None:
|
||||||
|
if isinstance(extra_output_columns, str):
|
||||||
|
extra_output_columns = [extra_output_columns]
|
||||||
|
predict_columns.extend([c for c in extra_output_columns if c not in predict_columns])
|
||||||
|
|
||||||
# Identify largest seasonality period
|
# Identify largest seasonality period
|
||||||
period_max = 0.
|
period_max = 0.
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,19 @@ class TestCrossValidation:
|
||||||
_ = diagnostics.cross_validation(m, *args)
|
_ = diagnostics.cross_validation(m, *args)
|
||||||
# check single forecast function called expected number of times
|
# check single forecast function called expected number of times
|
||||||
assert n_calls == forecasts
|
assert n_calls == forecasts
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("extra_output_columns", ["trend", ["trend"]])
|
||||||
|
def test_check_extra_output_columns_cross_validation(self, ts_short, backend, extra_output_columns):
|
||||||
|
m = Prophet(stan_backend=backend)
|
||||||
|
m.fit(ts_short)
|
||||||
|
df_cv = diagnostics.cross_validation(
|
||||||
|
m,
|
||||||
|
horizon="1 days",
|
||||||
|
period="1 days",
|
||||||
|
initial="140 days",
|
||||||
|
extra_output_columns=extra_output_columns
|
||||||
|
)
|
||||||
|
assert "trend" in df_cv.columns
|
||||||
|
|
||||||
@pytest.mark.parametrize("growth", ["logistic", "flat"])
|
@pytest.mark.parametrize("growth", ["logistic", "flat"])
|
||||||
def test_cross_validation_logistic_or_flat_growth(self, growth, ts_short, backend):
|
def test_cross_validation_logistic_or_flat_growth(self, growth, ts_short, backend):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue