|
| 1 | +""" |
| 2 | +LightGBM Model |
| 3 | +-------------- |
| 4 | +
|
| 5 | +This is a LightGBM implementation of Gradient Boosted Trees algorightm. |
| 6 | +
|
| 7 | +To enable LightGBM support in Darts, follow the detailed install instructions for LightGBM in the README: |
| 8 | +https://github.com/unit8co/darts/blob/master/README.md |
| 9 | +""" |
| 10 | + |
| 11 | +from ..logging import get_logger |
| 12 | +from typing import Union, Optional, Sequence, List, Tuple |
| 13 | +from .regression_model import RegressionModel |
| 14 | +from ..timeseries import TimeSeries |
| 15 | +import lightgbm as lgb |
| 16 | + |
| 17 | +logger = get_logger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class LightGBMModel(RegressionModel): |
| 21 | + def __init__(self, |
| 22 | + lags: Union[int, list] = None, |
| 23 | + lags_past_covariates: Union[int, List[int]] = None, |
| 24 | + lags_future_covariates: Union[Tuple[int, int], List[int]] = None, |
| 25 | + **kwargs): |
| 26 | + """ Light Gradient Boosted Model |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + lags |
| 31 | + Lagged target values used to predict the next time step. If an integer is given the last `lags` past lags |
| 32 | + are used (from -1 backward). Otherwise a list of integers with lags is required (each lag must be < 0). |
| 33 | + lags_past_covariates |
| 34 | + Number of lagged past_covariates values used to predict the next time step. If an integer is given the last |
| 35 | + `lags_past_covariates` past lags are used (inclusive, starting from lag -1). Otherwise a list of integers |
| 36 | + with lags < 0 is required. |
| 37 | + lags_future_covariates |
| 38 | + Number of lagged future_covariates values used to predict the next time step. If an tuple (past, future) is |
| 39 | + given the last `past` lags in the past are used (inclusive, starting from lag -1) along with the first |
| 40 | + `future` future lags (starting from 0 - the prediction time - up to `future - 1` included). Otherwise a list |
| 41 | + of integers with lags is required. |
| 42 | + **kwargs |
| 43 | + Additional keyword arguments passed to `lightgbm.LGBRegressor`. |
| 44 | + """ |
| 45 | + self.kwargs = kwargs |
| 46 | + |
| 47 | + super().__init__( |
| 48 | + lags=lags, |
| 49 | + lags_past_covariates=lags_past_covariates, |
| 50 | + lags_future_covariates=lags_future_covariates, |
| 51 | + model=lgb.LGBMRegressor( |
| 52 | + **kwargs |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + def __str__(self): |
| 57 | + return 'LGBModel(lags={}, lags_past={}, lags_future={})'.format( |
| 58 | + self.lags, self.lags_past_covariates, self.lags_future_covariates |
| 59 | + ) |
| 60 | + |
| 61 | + def fit(self, |
| 62 | + series: Union[TimeSeries, Sequence[TimeSeries]], |
| 63 | + past_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None, |
| 64 | + future_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None, |
| 65 | + eval_series: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None, |
| 66 | + eval_past_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None, |
| 67 | + eval_future_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None, |
| 68 | + max_samples_per_ts: Optional[int] = None, |
| 69 | + **kwargs) -> None: |
| 70 | + """ |
| 71 | + Fits/trains the model using the provided list of features time series and the target time series. |
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + series : Union[TimeSeries, Sequence[TimeSeries]] |
| 75 | + TimeSeries or Sequence[TimeSeries] object containing the target values. |
| 76 | + past_covariates : Union[TimeSeries, Sequence[TimeSeries]] |
| 77 | + Optionally, a series or sequence of series specifying past-observed covariates |
| 78 | + future_covariates : Union[TimeSeries, Sequence[TimeSeries]] |
| 79 | + Optionally, a series or sequence of series specifying future-known covariates |
| 80 | + eval_series : Union[TimeSeries, Sequence[TimeSeries]] |
| 81 | + TimeSeries or Sequence[TimeSeries] object containing the target values for evaluation dataset |
| 82 | + eval_past_covariates : Union[TimeSeries, Sequence[TimeSeries]] |
| 83 | + Optionally, a series or sequence of series specifying past-observed covariates for evaluation dataset |
| 84 | + eval_future_covariates : Union[TimeSeries, Sequence[TimeSeries]] |
| 85 | + Optionally, a series or sequence of series specifying future-known covariates for evaluation dataset |
| 86 | + max_samples_per_ts : int |
| 87 | + This is an upper bound on the number of tuples that can be produced |
| 88 | + per time series. It can be used in order to have an upper bound on the total size of the dataset and |
| 89 | + ensure proper sampling. If `None`, it will read all of the individual time series in advance (at dataset |
| 90 | + creation) to know their sizes, which might be expensive on big datasets. |
| 91 | + If some series turn out to have a length that would allow more than `max_samples_per_ts`, only the |
| 92 | + most recent `max_samples_per_ts` samples will be considered. |
| 93 | + """ |
| 94 | + |
| 95 | + if eval_series is not None: |
| 96 | + |
| 97 | + kwargs['eval_set'] = self._create_lagged_data( |
| 98 | + target_series=eval_series, |
| 99 | + past_covariates=eval_past_covariates, |
| 100 | + future_covariates=eval_future_covariates, |
| 101 | + max_samples_per_ts=max_samples_per_ts |
| 102 | + ) |
| 103 | + |
| 104 | + super().fit(series=series, |
| 105 | + past_covariates=past_covariates, |
| 106 | + future_covariates=future_covariates, |
| 107 | + max_samples_per_ts=max_samples_per_ts, |
| 108 | + **kwargs) |
0 commit comments