ForecastingHorizon#

class ForecastingHorizon(values: Optional[Union[int, list, numpy.ndarray, pandas.core.indexes.base.Index]] = None, is_relative: Optional[bool] = 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 inferrable 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.forecasting.model_selection 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], dtype='int64', is_relative=True)

Numpy as ForecastingHorizon

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

Absolute ForecastingHorizon with a pandas Index

>>> ForecastingHorizon(y_test.index, is_relative=False) 
ForecastingHorizon(['1960-07', '1960-08', '1960-09', '1960-10',
    '1960-11', '1960-12'], dtype='period[M]', name='Period', 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], dtype='int64', is_relative=True)
>>> # to_absolute
>>> fh = ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)
>>> fh.to_absolute(cutoff=cutoff) 
ForecastingHorizon(['1960-07', '1960-08', '1960-09', '1960-10',
    '1960-11', '1960-12'], dtype='period[M]', 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

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_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() pandas.core.indexes.base.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) numpy.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 containg 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_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.