Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality to let LightGBM effectively handle categorical features #1585

Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ec8fe6d
#1580 exploration
Feb 20, 2023
5ee855d
#1580 added cat_components to TimeSeries
Feb 20, 2023
149b2c7
#1580 _fit_model method LightGBM
Feb 20, 2023
b02e8b1
#1580 included static covs in dummy unit test
Feb 20, 2023
948be36
#1580 integration with lgbm
Feb 20, 2023
3c2fee2
#1580 helper func to method in RegressionModel
Feb 21, 2023
c3d642f
#1580 different approach; pass categorical covs to fit method of lgbm…
Feb 21, 2023
5679eeb
#1580 added few unit tests
Feb 21, 2023
ef7fcf8
#1580 small stuff
Feb 21, 2023
5a5a09f
Merge branch 'master' into feature/use_model_native_way_cat_features
madtoinou Feb 22, 2023
4c5b140
#1580 move categorical covs to model constructor
Feb 27, 2023
f6b25fc
#1580 avoid code duplication in unit tests
Feb 28, 2023
e7cde27
#1580 add unit test on forecast quality with cat covs
Feb 28, 2023
d8aa69f
#1580 add column names check in _get_categorical_covs helper
Feb 28, 2023
5be4f4c
#1580 docstrings lgbm
Feb 28, 2023
dc9ceeb
#1580 add changelog entry
Feb 28, 2023
713a850
Merge branch 'feature/use_model_native_way_cat_features' of https://g…
Feb 28, 2023
165d1bc
#1580 change check if ts has static cov
Feb 28, 2023
d02d3a0
Merge branch 'master' into feature/use_model_native_way_cat_features
Feb 28, 2023
95bf521
Merge branch 'master' into feature/use_model_native_way_cat_features
dennisbader Mar 5, 2023
9df90ae
#1580 implemented RegressionModelWithCategoricalCovariates class
Mar 12, 2023
36e56de
#1580 delete redundant test
Mar 12, 2023
e85bad2
#1580 replace test_quality_forecast_with_categorical_covariates unit …
Mar 12, 2023
9ba3190
#1580 adjustment error messages validation method
Mar 12, 2023
5f2535b
#1580 adding categorical feature support for CatBoost
Mar 12, 2023
ae1d4df
#1580 remove cat support CatBoost and smaller comments Dennis
Mar 27, 2023
7cb8c72
#1580 finalizing
Mar 27, 2023
20073fe
Merge branch 'master' into feature/use_model_native_way_cat_features
Mar 27, 2023
6eb4ed4
#1580 use parent _fit_model method
Mar 27, 2023
5dc1341
Merge branch 'master' into feature/use_model_native_way_cat_features
Mar 27, 2023
fc41cd8
avoid creating lagged data twice
dennisbader Mar 27, 2023
0836ff2
remove empty lines
dennisbader Mar 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
[#1545](https://github.com/unit8co/darts/pull/1545) by [Rijk van der Meulen](https://github.com/rijkvandermeulen).

[Full Changelog](https://github.com/unit8co/darts/compare/0.23.1...master)
- `LightGBM` model now supports native categorical feature handling as described
[here](https://lightgbm.readthedocs.io/en/latest/Features.html#optimal-split-for-categorical-features).
[#1585](https://github.com/unit8co/darts/pull/1585) by [Rijk van der Meulen](https://github.com/rijkvandermeulen)

## [0.23.1](https://github.com/unit8co/darts/tree/0.23.1) (2023-01-12)
Patch release
Expand Down
28 changes: 25 additions & 3 deletions darts/models/forecasting/lgbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import numpy as np

from darts.logging import get_logger
from darts.models.forecasting.regression_model import RegressionModel, _LikelihoodMixin
from darts.models.forecasting.regression_model import (
RegressionModelWithCategoricalCovariates,
_LikelihoodMixin,
)
from darts.timeseries import TimeSeries

logger = get_logger(__name__)


class LightGBMModel(RegressionModel, _LikelihoodMixin):
class LightGBMModel(RegressionModelWithCategoricalCovariates, _LikelihoodMixin):
def __init__(
self,
lags: Union[int, list] = None,
Expand All @@ -34,6 +37,9 @@ def __init__(
quantiles: List[float] = None,
random_state: Optional[int] = None,
multi_models: Optional[bool] = True,
categorical_past_covariates: Optional[Union[str, List[str]]] = None,
categorical_future_covariates: Optional[Union[str, List[str]]] = None,
categorical_static_covariates: Optional[Union[str, List[str]]] = None,
**kwargs,
):
"""LGBM Model
Expand Down Expand Up @@ -87,6 +93,20 @@ def __init__(
multi_models
If True, a separate model will be trained for each future lag to predict. If False, a single model is
trained to predict at step 'output_chunk_length' in the future. Default: True.
categorical_past_covariates
Optionally, component name or list of component names specifying the past covariates that should be treated
as categorical by the underlying `lightgbm.LightGBMRegressor`. It's recommended that the components that
are treated as categorical are integer-encoded. For more information on how LightGBM handles categorical
features, visit: `Categorical feature support documentation
<https://lightgbm.readthedocs.io/en/latest/Features.html#optimal-split-for-categorical-features>`_
categorical_future_covariates
Optionally, component name or list of component names specifying the future covariates that should be
treated as categorical by the underlying `lightgbm.LightGBMRegressor`. It's recommended that the components
that are treated as categorical are integer-encoded.
categorical_static_covariates
Optionally, string or list of strings specifying the static covariates that should be treated as categorical
by the underlying `lightgbm.LightGBMRegressor`. It's recommended that the static covariates that are
treated as categorical are integer-encoded.
**kwargs
Additional keyword arguments passed to `lightgbm.LGBRegressor`.
"""
Expand Down Expand Up @@ -117,6 +137,9 @@ def __init__(
add_encoders=add_encoders,
multi_models=multi_models,
model=lgb.LGBMRegressor(**self.kwargs),
categorical_past_covariates=categorical_past_covariates,
categorical_future_covariates=categorical_future_covariates,
categorical_static_covariates=categorical_static_covariates,
)

def fit(
Expand Down Expand Up @@ -157,7 +180,6 @@ def fit(
**kwargs
Additional kwargs passed to `lightgbm.LGBRegressor.fit()`
"""

if val_series is not None:
kwargs["eval_set"] = self._create_lagged_data(
target_series=val_series,
Expand Down
Loading