Use fit kwargs in cross validation (#1040)

This commit is contained in:
Ben Letham 2020-02-03 17:02:23 -08:00
parent 4fcecdb3df
commit 6e51130f28
7 changed files with 12 additions and 4 deletions

View file

@ -117,7 +117,8 @@ cross_validation <- function(
if (nrow(history.c) < 2) { if (nrow(history.c) < 2) {
stop('Less than two datapoints before cutoff. Increase initial window.') stop('Less than two datapoints before cutoff. Increase initial window.')
} }
m <- fit.prophet(m, history.c) fit.args <- c(list(m=m, df=history.c), model$fit.kwargs)
m <- do.call(fit.prophet, fit.args)
# Calculate yhat # Calculate yhat
df.predict <- dplyr::filter(df, ds > cutoff, ds <= cutoff + horizon.dt) df.predict <- dplyr::filter(df, ds > cutoff, ds <= cutoff + horizon.dt)
# Get the columns for the future dataframe # Get the columns for the future dataframe

View file

@ -134,7 +134,8 @@ prophet <- function(df = NULL,
history.dates = NULL, history.dates = NULL,
train.holiday.names = NULL, train.holiday.names = NULL,
train.component.cols = NULL, train.component.cols = NULL,
component.modes = NULL component.modes = NULL,
fit.kwargs = list()
) )
m <- validate_inputs(m) m <- validate_inputs(m)
class(m) <- append("prophet", class(m)) class(m) <- append("prophet", class(m))
@ -1208,6 +1209,7 @@ fit.prophet <- function(m, df, ...) {
component.cols <- out2$component.cols component.cols <- out2$component.cols
m$train.component.cols <- component.cols m$train.component.cols <- component.cols
m$component.modes <- out2$modes m$component.modes <- out2$modes
m$fit.kwargs <- list(...)
m <- set_changepoints(m) m <- set_changepoints(m)

View file

@ -114,6 +114,7 @@ test_that("logistic_floor", {
expect_true(m$logistic.floor) expect_true(m$logistic.floor)
expect_true('floor' %in% colnames(m$history)) expect_true('floor' %in% colnames(m$history))
expect_equal(m$history$y_scaled[1], 1., tolerance = 1e-6) expect_equal(m$history$y_scaled[1], 1., tolerance = 1e-6)
expect_equal(m$fit.kwargs, list(algorithm = 'Newton'))
fcst1 <- predict(m, future1) fcst1 <- predict(m, future1)
m2 <- prophet(growth = 'logistic') m2 <- prophet(growth = 'logistic')

View file

@ -117,7 +117,7 @@ def cross_validation(model, horizon, period=None, initial=None):
'Less than two datapoints before cutoff. ' 'Less than two datapoints before cutoff. '
'Increase initial window.' 'Increase initial window.'
) )
m.fit(history_c) m.fit(history_c, **model.fit_kwargs)
# Calculate yhat # Calculate yhat
index_predicted = (df['ds'] > cutoff) & (df['ds'] <= cutoff + horizon) index_predicted = (df['ds'] > cutoff) & (df['ds'] <= cutoff + horizon)
# Get the columns for the future dataframe # Get the columns for the future dataframe

View file

@ -8,6 +8,7 @@ from __future__ import absolute_import, division, print_function
import logging import logging
from collections import OrderedDict, defaultdict from collections import OrderedDict, defaultdict
from copy import deepcopy
from datetime import timedelta from datetime import timedelta
import numpy as np import numpy as np
@ -137,6 +138,7 @@ class Prophet(object):
self.train_component_cols = None self.train_component_cols = None
self.component_modes = None self.component_modes = None
self.train_holiday_names = None self.train_holiday_names = None
self.fit_kwargs = {}
self.validate_inputs() self.validate_inputs()
def validate_inputs(self): def validate_inputs(self):
@ -1080,6 +1082,7 @@ class Prophet(object):
self.make_all_seasonality_features(history)) self.make_all_seasonality_features(history))
self.train_component_cols = component_cols self.train_component_cols = component_cols
self.component_modes = modes self.component_modes = modes
self.fit_kwargs = deepcopy(kwargs)
self.set_changepoints() self.set_changepoints()

View file

@ -109,7 +109,7 @@ class TestDiagnostics(TestCase):
df = self.__df.copy() df = self.__df.copy()
for uncertainty in [0, False]: for uncertainty in [0, False]:
m = Prophet(uncertainty_samples=uncertainty) m = Prophet(uncertainty_samples=uncertainty)
m.fit(df) m.fit(df, algorithm='Newton')
df_cv = diagnostics.cross_validation( df_cv = diagnostics.cross_validation(
m, horizon='4 days', period='4 days', initial='115 days') m, horizon='4 days', period='4 days', initial='115 days')
expected_cols = ['ds', 'yhat', 'y', 'cutoff'] expected_cols = ['ds', 'yhat', 'y', 'cutoff']

View file

@ -142,6 +142,7 @@ class TestProphet(TestCase):
self.assertTrue(m.logistic_floor) self.assertTrue(m.logistic_floor)
self.assertTrue('floor' in m.history) self.assertTrue('floor' in m.history)
self.assertAlmostEqual(m.history['y_scaled'][0], 1.) self.assertAlmostEqual(m.history['y_scaled'][0], 1.)
self.assertEqual(m.fit_kwargs, {'algorithm': 'Newton'})
fcst1 = m.predict(future) fcst1 = m.predict(future)
m2 = Prophet(growth='logistic') m2 = Prophet(growth='logistic')