hts.core package¶
hts.core.exceptions¶
hts.core.regressor¶
-
class
hts.core.regressor.
HTSRegressor
(model: str = 'prophet', revision_method: str = 'OLS', transform: Union[hts._t.Transform, bool, None] = False, n_jobs: int = 1, low_memory: bool = False, **kwargs)[source]¶ Bases:
sklearn.base.BaseEstimator
,sklearn.base.RegressorMixin
Main regressor class for scikit-hts. Likely the only import you’ll need for using this project. It takes a pandas dataframe, the nodes specifying the hierarchies, model kind, revision method, and a few other parameters. See Examples to get an idea of how to use it.
Variables: - transform (Union[NamedTuple[str, Callable], bool]) – Function transform to be applied to input and outputs. If True, it will use
scipy.stats.boxcox
andscipy.special._ufuncs.inv_boxcox
on input and output data - sum_mat (array_like) – The summing matrix, explained in depth in Forecasting
- nodes (Dict[str, List[str]]) – Nodes representing node, edges of the hierarchy. Keys are nodes, values are list of edges.
- df (pandas.DataFrame) – The dataframe containing the nodes and edges specified above
- revision_method (str) – One of:
"OLS", "WLSS", "WLSV", "FP", "PHA", "AHP", "BU", "NONE"
- models (dict) – Dictionary that holds the trained models
- mse (dict) – Dictionary that holds the mse scores for the trained models
- residuals (dict) – Dictionary that holds the mse residual for the trained models
- forecasts (dict) – Dictionary that holds the forecasts for the trained models
- model_instance (TimeSeriesModel) – Reference to the class implementing the actual time series model
-
__init__
(model: str = 'prophet', revision_method: str = 'OLS', transform: Union[hts._t.Transform, bool, None] = False, n_jobs: int = 1, low_memory: bool = False, **kwargs)[source]¶ Parameters: - model (str) – One of the models supported by
hts
. These can be found - revision_method (str) – The revision method to be used. One of:
"OLS", "WLSS", "WLSV", "FP", "PHA", "AHP", "BU", "NONE"
- transform (Boolean or NamedTuple) –
If True,
scipy.stats.boxcox
andscipy.special._ufuncs.inv_boxcox
will be applied prior and after fitting. If False (default), no transform is applied. If you desired to use custom functions, use a NamedTuple like:from collections import namedtuple Transform = namedtuple('Transform', ['func', 'inv_func'] transform = Transform(func=numpy.exp, inv_func=numpy.log) ht = HTSRegressor(transform=transform, ...)
The signatures for the
func
as well asinv_func
parameters must both beCallable[[numpy.ndarry], numpy.ndarray]
, i.e. they must take an array and return an array, both of equal dimensions - n_jobs (int) – Number of parallel jobs to run the forecasting on
- low_memory (Bool) – If True, models will be fit, serialized, and released from memory. Usually a good idea if you are dealing with a large amount of nodes
- kwargs – Keyword arguments to be passed to the underlying model to be instantiated
- model (str) – One of the models supported by
-
fit
(df: Optional[pandas.core.frame.DataFrame] = None, nodes: Optional[Dict[str, List[str]]] = None, tree: Optional[hts.hierarchy.HierarchyTree] = None, exogenous: Optional[Dict[str, List[str]]] = None, root: str = 'total', distributor: Optional[hts.utilities.distribution.DistributorBaseClass] = None, disable_progressbar=False, show_warnings=False, **fit_kwargs) → hts.core.regressor.HTSRegressor[source]¶ Fit hierarchical model to dataframe containing hierarchical data as specified in the
nodes
parameter.Exogenous can also be passed as a dict of (string, list), where string is the specific node key and the list contains the names of the columns to be used as exogenous variables for that node.
Alternatively, a pre-built HierarchyTree can be passed without specifying the node and df. See more at
hts.hierarchy.HierarchyTree
Parameters: - df (pandas.DataFrame) – A Dataframe of time series with a DateTimeIndex. Each column represents a node in the hierarchy. Ignored if tree argument is passed
- nodes (Dict[str, List[str]]) –
- The hierarchy defined as a dict of (string, list), as specified in
HierarchyTree.from_nodes
- tree (HierarchyTree) – A pre-built HierarchyTree. Ignored if df and nodes are passed, as the tree will be built from thise
- distributor (Optional[DistributorBaseClass]) – A distributor, for parallel/distributed processing
- exogenous (Dict[str, List[str]] or None) – Node key mapping to columns that contain the exogenous variable for that node
- root (str) – The name of the root node
- disable_progressbar (Bool) – Disable or enable progressbar
- show_warnings (Bool) – Disable warnings
- fit_kwargs (Any) – Any arguments to be passed to the underlying forecasting model’s fit function
Returns: The fitted HTSRegressor instance
Return type:
-
predict
(exogenous_df: pandas.core.frame.DataFrame = None, steps_ahead: int = None, distributor: Optional[hts.utilities.distribution.DistributorBaseClass] = None, disable_progressbar: bool = False, show_warnings: bool = False, **predict_kwargs) → pandas.core.frame.DataFrame[source]¶ Parameters: - distributor (Optional[DistributorBaseClass]) – A distributor, for parallel/distributed processing
- disable_progressbar (Bool) – Disable or enable progressbar
- show_warnings (Bool) – Disable warnings
- predict_kwargs (Any) – Any arguments to be passed to the underlying forecasting model’s predict function
- exogenous_df (pandas.DataFrame) –
A dataframe of length == steps_ahead containing the exogenous data for each of the nodes. Only required when using
prophet
orauto_arima
models. See fbprophet’s additional regression docs and AutoARIMA’s exogenous handling docs for more information.Other models do not require additional regressors at predict time.
- steps_ahead (int) – The number of forecasting steps for which to produce a forecast
Returns: - Revised Forecasts, as a pandas.DataFrame in the same format as the one passed for fitting, extended by steps_ahead
- time steps`
- transform (Union[NamedTuple[str, Callable], bool]) – Function transform to be applied to input and outputs. If True, it will use