def fit(self, y, X=None, fh=None): """Fit to training data. Parameters ---------- y : pd.Series Target time series to which to fit the forecaster. X : pd.DataFrame, optional (default=None) Exogenous variables are ignored fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. Returns ------- self : returns an instance of self. """ n_timepoints = len(y) self._set_y_X(y, X) self._set_fh(fh) self.step_length_ = check_step_length(self.step_length) self.window_length_ = check_window_length(self.window_length, n_timepoints) self._fit(y, X) self._is_fitted = True return self
def get_cutoffs(self, y: Optional[ACCEPTED_Y_TYPES] = None) -> np.ndarray: """Return the cutoff points. Parameters ---------- y : pd.Series or pd.Index, optional (default=None) Time series to split Returns ------- cutoffs : np.array The array of cutoff points. """ if y is None: raise ValueError( f"{self.__class__.__name__} requires `y` to compute the cutoffs." ) y = _check_y(y) fh = _check_fh(self.fh) step_length = check_step_length(self.step_length) if hasattr(self, "initial_window") and self.initial_window is not None: if is_timedelta_or_date_offset(x=self.initial_window): start = y.get_loc(y[0] + self.initial_window) else: start = self.initial_window else: start = self._get_start(y=y, fh=fh) end = _get_end(y, fh) step_length = self._get_step_length(x=step_length) return np.arange(start, end, step_length) - 1
def get_cutoffs(self, y=None): """Get the cutoff time points. Parameters ---------- y : pd.Series or pd.Index, optional (default=None) Returns ------- cutoffs : np.array """ if y is None: raise ValueError( f"{self.__class__.__name__} requires `y` to compute the cutoffs." ) y = _check_y(y) fh = _check_fh(self.fh) step_length = check_step_length(self.step_length) if hasattr(self, "initial_window") and self.initial_window is not None: start = self.initial_window else: start = self._get_start(fh) end = _get_end(y, fh) return np.arange(start, end, step_length) - 1
def _split(self, y): step_length = check_step_length(self.step_length) window_length = check_window_length(self.window_length, "window_length") initial_window = check_window_length(self.initial_window, "initial_window") fh = _check_fh(self.fh) _check_window_lengths(y, fh, window_length, initial_window) if self.initial_window is not None: if not self.start_with_window: raise ValueError( "`start_with_window` must be True if `initial_window` is given" ) if self.initial_window <= self.window_length: raise ValueError("`initial_window` must greater than `window_length`") # For in-sample forecasting horizons, the first split must ensure that # in-sample test set is still within the data. if not fh.is_all_out_of_sample() and abs(fh[0]) >= self.initial_window: initial_start = abs(fh[0]) - self.initial_window + 1 else: initial_start = 0 initial_end = initial_start + initial_window train = np.arange(initial_start, initial_end) test = initial_end + fh.to_numpy() - 1 yield train, test start = self._get_start(fh) end = _get_end(y, fh) for train, test in self._split_windows( start, end, step_length, window_length, fh.to_numpy() ): yield train, test
def _split(self, y: pd.Index) -> SPLIT_GENERATOR_TYPE: n_timepoints = y.shape[0] step_length = check_step_length(self.step_length) window_length = check_window_length( window_length=self.window_length, n_timepoints=n_timepoints, name="window_length", ) initial_window = check_window_length( window_length=self.initial_window, n_timepoints=n_timepoints, name="initial_window", ) fh = _check_fh(self.fh) _check_window_lengths( y=y, fh=fh, window_length=window_length, initial_window=initial_window ) if self.initial_window is not None: yield self._split_for_initial_window(y) start = self._get_start(y=y, fh=fh) end = _get_end(y_index=y, fh=fh) + 2 step_length = self._get_step_length(x=step_length) for train, test in self._split_windows( start=start, end=end, step_length=step_length, window_length=window_length, y=y, fh=fh.to_numpy(), ): yield train, test
def _split_windows(self, y): step_length = check_step_length(self.step_length) window_length = check_window_length(self.window_length) fh = self._check_fh() end = self._get_end(y) start = self._get_start() for split_point in range(start, end, step_length): training_window = np.arange(split_point - window_length, split_point) test_window = split_point + fh - 1 yield training_window, test_window
def fit(self, y, X=None, fh=None): """Fit to training data. Parameters ---------- y : pd.Series Target time series to which to fit the forecaster. fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. X : pd.DataFrame, optional (default=None) Exogenous variables are ignored Returns ------- self : returns an instance of self. """ self._set_y_X(y, X) if X is not None: raise NotImplementedError( "Exogenous variables `X` are not yet supported.") self._set_fh(fh) if len(self.fh.to_in_sample(self.cutoff)) > 0: raise NotImplementedError( "In-sample predictions are not implemented") self.step_length_ = check_step_length(self.step_length) self.window_length_ = check_window_length(self.window_length) # for the direct reduction strategy, a separate forecaster is fitted # for each step ahead of the forecasting horizon self._cv = SlidingWindowSplitter( fh=self.fh.to_relative(self.cutoff), window_length=self.window_length_, step_length=self.step_length_, start_with_window=True, ) # transform data using rolling window split X, Y_train = self._transform(y, X) # iterate over forecasting horizon self.regressors_ = [] for i in range(len(self.fh)): y = Y_train[:, i] regressor = clone(self.regressor) regressor.fit(X, y) self.regressors_.append(regressor) self._is_fitted = True return self
def _split(self, y: Optional[ACCEPTED_Y_TYPES]) -> SPLIT_GENERATOR_TYPE: n_timepoints = y.shape[0] step_length = check_step_length(self.step_length) window_length = check_window_length(self.window_length, n_timepoints, "window_length") initial_window = check_window_length(self.initial_window, n_timepoints, "initial_window") fh = _check_fh(self.fh) _check_window_lengths(y, fh, window_length, initial_window) if self.initial_window is not None: if not self.start_with_window: raise ValueError( "`start_with_window` must be True if `initial_window` is given" ) if self.initial_window <= self.window_length: raise ValueError( "`initial_window` must greater than `window_length`") if is_timedelta_or_date_offset(x=self.initial_window): initial_window_threshold = y.get_loc(y[0] + self.initial_window) else: initial_window_threshold = self.initial_window # For in-sample forecasting horizons, the first split must ensure that # in-sample test set is still within the data. if not fh.is_all_out_of_sample() and abs( fh[0]) >= initial_window_threshold: initial_start = abs(fh[0]) - self.initial_window + 1 else: initial_start = 0 if is_timedelta_or_date_offset(x=initial_window): initial_end = y.get_loc(y[initial_start] + initial_window) else: initial_end = initial_start + initial_window train = np.arange(initial_start, initial_end) test = initial_end + fh.to_numpy() - 1 yield train, test start = self._get_start(y=y, fh=fh) end = _get_end(y=y, fh=fh) step_length = self._get_step_length(x=step_length) for train, test in self._split_windows(start, end, step_length, window_length, y, fh.to_numpy()): yield train, test
def fit(self, y_train, fh=None, X_train=None): """Fit to training data. Parameters ---------- y_train : pd.Series Target time series to which to fit the forecaster. fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. X_train : pd.DataFrame, optional (default=None) Exogenous variables are ignored Returns ------- self : returns an instance of self. """ # input checks if X_train is not None: raise NotImplementedError() self._set_oh(y_train) self._set_fh(fh) if np.any(self.fh <= 0): raise NotImplementedError( "in-sample predictions are not implemented") self.step_length_ = check_step_length(self.step_length) self.window_length_ = check_window_length(self.window_length) # for the direct reduction strategy, a separate forecaster is fitted # for each step ahead of the forecasting horizon self._cv = SlidingWindowSplitter(fh=self.fh, window_length=self.window_length_, step_length=self.step_length_, start_with_window=True) # transform data using rolling window split X_train, Y_train = self._transform(y_train, X_train) # iterate over forecasting horizon self.regressors_ = [] for i in range(len(self.fh)): y_train = Y_train[:, i] regressor = clone(self.regressor) regressor.fit(X_train, y_train) self.regressors_.append(regressor) self._is_fitted = True return self
def fit(self, y_train, fh=None, X_train=None): """Fit to training data. Parameters ---------- y_train : pd.Series Target time series to which to fit the forecaster. fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. X_train : pd.DataFrame, optional (default=None) Exogenous variables are ignored Returns ------- self : returns an instance of self. """ # input checks if X_train is not None: raise NotImplementedError() # set values self._set_y_X(y_train, X_train) self._set_fh(fh) # Set this and then call the super method, that should be enought I think ..... self._nbr_dependent = y_train.shape[1] self.step_length_ = check_step_length(self.step_length) self.window_length_ = check_window_length(self.window_length) # set up cv iterator, for recursive strategy, a single estimator # is fit for a one-step-ahead forecasting horizon and then called # iteratively to predict multiple steps ahead self._cv = SlidingWindowSplitter( fh=1, window_length=self.window_length_, step_length=self.step_length_, start_with_window=True, ) # transform data into tabular form X_train_tab, y_train_tab = self._transform(y_train, X_train) # fit base regressor regressor = clone(self.regressor) regressor.fit(X_train_tab, y_train_tab) self.regressor_ = regressor self._is_fitted = True return self
def fit(self, y, X=None, fh=None): """Fit to training data. Parameters ---------- y : pd.Series Target time series to which to fit the forecaster. fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. X : pd.DataFrame, optional (default=None) Exogenous variables are ignored Returns ------- self : returns an instance of self. """ self._set_y_X(y, X) if X is not None: raise NotImplementedError( "Exogenous variables `X` are not yet supported.") self._set_fh(fh) if len(self.fh.to_in_sample(self.cutoff)) > 0: raise NotImplementedError( "In-sample predictions are not implemented") self.step_length_ = check_step_length(self.step_length) self.window_length_ = check_window_length(self.window_length) # for the multioutput reduction strategy, a single forecaster is fitted # simultaneously to all the future steps in the forecasting horizon # by reducing to a forecaster that can handle multi-dimensional outputs self._cv = SlidingWindowSplitter( fh=self.fh.to_relative(self.cutoff), window_length=self.window_length_, step_length=self.step_length_, start_with_window=True, ) # transform data using rolling window split X, Y_train = self._transform(y, X) # fit regressor to training data regressor = clone(self.regressor) regressor.fit(X, Y_train) self.regressor_ = regressor self._is_fitted = True return self
def get_cutoffs(self, y=None): """Get the cutoff time points. Parameters ---------- y : pd.Series or pd.Index, optional (default=None) Returns ------- cutoffs : np.array """ if y is None: raise ValueError( f"{self.__class__.__name__} requires `y` to compute the " f"cutoffs.") y = self._check_y(y) end = self._get_end(y) start = self._get_start() step_length = check_step_length(self.step_length) return np.arange(start, end, step_length) - 1