ForecastingHorizon#

class ForecastingHorizon(values=None, is_relative=None, freq=None)[source]#

Forecasting horizon.

Parameters:
valuespd.Index, pd.TimedeltaIndex, np.array, list, pd.Timedelta, or int

Values of forecasting horizon

is_relativebool, optional (default=None)
  • If True, a relative ForecastingHorizon is created:

    values are relative to end of training series.

  • If False, an absolute ForecastingHorizon is created:

    values are absolute.

  • if None, the flag is determined automatically:

    relative, if values are of supported relative index type absolute, if not relative and values of supported absolute index type

freqstr, pd.Index, pandas offset, or sktime forecaster, optional (default=None)

object carrying frequency information on values ignored unless values is without inferable freq

Attributes:
freq

Frequency attribute.

is_relative

Whether forecasting horizon is relative to the end of the training series.

Examples

>>> from sktime.forecasting.base import ForecastingHorizon
>>> from sktime.forecasting.naive import NaiveForecaster
>>> from sktime.datasets import load_airline
>>> from sktime.split import temporal_train_test_split
>>> import numpy as np
>>> y = load_airline()
>>> y_train, y_test = temporal_train_test_split(y, test_size=6)

List as ForecastingHorizon

>>> ForecastingHorizon([1, 2, 3])  
>>> # ForecastingHorizon([1, 2, 3], is_relative=True)

Numpy as ForecastingHorizon

>>> ForecastingHorizon(np.arange(1, 7))  
>>> # ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)

Absolute ForecastingHorizon with a pandas Index

>>> ForecastingHorizon(y_test.index, is_relative=False) 
>>> # ForecastingHorizon(['1960-07', ..., '1960-12'], is_relative=False)

Converting

>>> # set cutoff (last time point of training data)
>>> cutoff = y_train.index[-1]
>>> cutoff
Period('1960-06', 'M')
>>> # to_relative
>>> fh = ForecastingHorizon(y_test.index, is_relative=False)
>>> fh.to_relative(cutoff=cutoff)  
>>> # ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)
>>> # to_absolute
>>> fh = ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)
>>> fh = fh.to_absolute(cutoff=cutoff) 
>>> # ForecastingHorizon(['1960-07', ..., '1960-12'], is_relative=False)

Automatically casted ForecastingHorizon from list when calling predict()

>>> forecaster = NaiveForecaster(strategy="drift")
>>> forecaster.fit(y_train)
NaiveForecaster(...)
>>> y_pred = forecaster.predict(fh=[1,2,3])
>>> forecaster.fh  
>>> # ForecastingHorizon([1, 2, 3], dtype='int64', is_relative=True)

This is identical to give an object of ForecastingHorizon

>>> y_pred = forecaster.predict(fh=ForecastingHorizon([1,2,3]))
>>> forecaster.fh  
>>> # ForecastingHorizon([1, 2, 3], dtype='int64', is_relative=True)

Methods

get_expected_pred_idx([y, cutoff, sort_by_time])

Construct DataFrame Index expected in y_pred, return of _predict.

is_all_in_sample([cutoff])

Whether the forecasting horizon is purely in-sample for given cutoff.

is_all_out_of_sample([cutoff])

Whether the forecasting horizon is purely out-of-sample for given cutoff.

to_absolute(cutoff)

Return absolute version of forecasting horizon values.

to_absolute_index([cutoff])

Return absolute values of the horizon as a pandas.Index.

to_absolute_int(start[, cutoff])

Return absolute values as zero-based integer index starting from start.

to_in_sample([cutoff])

Return in-sample index values of fh.

to_indexer([cutoff, from_cutoff])

Return zero-based indexer values for easy indexing into arrays.

to_numpy(**kwargs)

Return forecasting horizon's underlying values as np.array.

to_out_of_sample([cutoff])

Return out-of-sample values of fh.

to_pandas()

Return forecasting horizon's underlying values as pd.Index.

to_relative([cutoff])

Return forecasting horizon values relative to a cutoff.

property is_relative: bool[source]#

Whether forecasting horizon is relative to the end of the training series.

Returns:
is_relativebool
property freq: str[source]#

Frequency attribute.

Returns:
freqpandas frequency string
to_pandas() Index[source]#

Return forecasting horizon’s underlying values as pd.Index.

Returns:
fhpd.Index

pandas Index containing forecasting horizon’s underlying values.

to_numpy(**kwargs) ndarray[source]#

Return forecasting horizon’s underlying values as np.array.

Parameters:
**kwargsdict of kwargs

kwargs passed to to_numpy() of wrapped pandas index.

Returns:
fhnp.ndarray

NumPy array containing forecasting horizon’s underlying values.

to_relative(cutoff=None)[source]#

Return forecasting horizon values relative to a cutoff.

Parameters:
cutoffpd.Period, pd.Timestamp, int, or pd.Index, optional (default=None)

Cutoff value required to convert a relative forecasting horizon to an absolute one (and vice versa). If pd.Index, last/latest value is considered the cutoff

Returns:
fhForecastingHorizon

Relative representation of forecasting horizon.

to_absolute(cutoff)[source]#

Return absolute version of forecasting horizon values.

Parameters:
cutoffpd.Period, pd.Timestamp, int, or pd.Index

Cutoff value is required to convert a relative forecasting horizon to an absolute one (and vice versa). If pd.Index, last/latest value is considered the cutoff

Returns:
fhForecastingHorizon

Absolute representation of forecasting horizon.

to_absolute_index(cutoff=None)[source]#

Return absolute values of the horizon as a pandas.Index.

For a forecaster f that has fh being self, the return of this method with cutoff=f.cutoff is the same as the expected index of the return of the forecaster’s predict methods, e.g., f.predict or f.predict_interval

Parameters:
cutoffpd.Period, pd.Timestamp, int, or pd.Index

Cutoff value is required to convert a relative forecasting horizon to an absolute one (and vice versa). If pd.Index, last/latest value is considered the cutoff

Returns:
fhForecastingHorizon

Absolute representation of forecasting horizon.

to_absolute_int(start, cutoff=None)[source]#

Return absolute values as zero-based integer index starting from start.

Parameters:
startpd.Period, pd.Timestamp, int

Start value returned as zero.

cutoffpd.Period, pd.Timestamp, int, or pd.Index, optional (default=None)

Cutoff value required to convert a relative forecasting horizon to an absolute one (and vice versa). If pd.Index, last/latest value is considered the cutoff

Returns:
fhForecastingHorizon

Absolute representation of forecasting horizon as zero-based integer index.

to_in_sample(cutoff=None)[source]#

Return in-sample index values of fh.

Parameters:
cutoffpd.Period, pd.Timestamp, int, optional (default=None)

Cutoff value required to convert a relative forecasting horizon to an absolute one (and vice versa).

Returns:
fhForecastingHorizon

In-sample values of forecasting horizon.

to_out_of_sample(cutoff=None)[source]#

Return out-of-sample values of fh.

Parameters:
cutoffpd.Period, pd.Timestamp, int, optional (default=None)

Cutoff value is required to convert a relative forecasting horizon to an absolute one (and vice versa).

Returns:
fhForecastingHorizon

Out-of-sample values of forecasting horizon.

is_all_in_sample(cutoff=None) bool[source]#

Whether the forecasting horizon is purely in-sample for given cutoff.

Parameters:
cutoffpd.Period, pd.Timestamp, int, default=None

Cutoff value used to check if forecasting horizon is purely in-sample.

Returns:
retbool

True if the forecasting horizon is purely in-sample for given cutoff.

is_all_out_of_sample(cutoff=None) bool[source]#

Whether the forecasting horizon is purely out-of-sample for given cutoff.

Parameters:
cutoffpd.Period, pd.Timestamp, int, optional (default=None)

Cutoff value used to check if forecasting horizon is purely out-of-sample.

Returns:
retbool

True if the forecasting horizon is purely out-of-sample for given cutoff.

to_indexer(cutoff=None, from_cutoff=True)[source]#

Return zero-based indexer values for easy indexing into arrays.

Parameters:
cutoffpd.Period, pd.Timestamp, int, optional (default=None)

Cutoff value required to convert a relative forecasting horizon to an absolute one and vice versa.

from_cutoffbool, optional (default=True)
  • If True, zero-based relative to cutoff.

  • If False, zero-based relative to first value in forecasting

horizon.

Returns:
fhpd.Index

Indexer.

get_expected_pred_idx(y=None, cutoff=None, sort_by_time=False)[source]#

Construct DataFrame Index expected in y_pred, return of _predict.

Parameters:
ypd.DataFrame, pd.Series, pd.Index, or None

data to compute fh relative to, assumed in sktime pandas based mtype or index thereof if None, assumes no MultiIndex

cutoffpd.Period, pd.Timestamp, int, or pd.Index, optional (default=None)

Cutoff value to use in computing resulting index. If cutoff is not provided, is computed from y via get_cutoff.

sort_by_timebool, optional (default=False)

for MultiIndex returns, whether to sort by time index (level -1) - If True, result Index is sorted by time index (level -1) - If False, result Index is sorted overall

Returns:
fh_idxpd.Index, expected index of y_pred returned by predict

assumes pandas based return mtype