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

Add LGBM model #65

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DartsLocalForecastingModel,
LinearRegressionModel,
RandomForestModel,
LGBMModel,
)
from tot.models.models_naive import NaiveModel, SeasonalNaiveModel
from tot.models.models_neuralprophet import NeuralProphetModel, TorchProphetModel
Expand Down Expand Up @@ -406,6 +407,44 @@ def test_darts_StatsForecastAutoARIMA_model():
print(results_test)


def test_darts_LGBM_model():
air_passengers_df = pd.read_csv(AIR_FILE, nrows=NROWS)
ercot_df_aux = pd.read_csv(ERCOT_FILE)
ercot_df = pd.DataFrame()
for region in ERCOT_REGIONS:
ercot_df = pd.concat(
(
ercot_df,
ercot_df_aux[ercot_df_aux["ID"] == region].iloc[:NROWS].copy(deep=True),
),
ignore_index=True,
)

dataset_list = [
Dataset(df=air_passengers_df, name="air_passengers", freq="MS"),
Dataset(df=ercot_df, name="ercot_df", freq="H"),
]
model_classes_and_params = [
(
LGBMModel,
{"lags": 12, "n_forecasts": 1},
),
]
log.debug("{}".format(model_classes_and_params))

benchmark = SimpleBenchmark(
model_classes_and_params=model_classes_and_params,
datasets=dataset_list,
metrics=list(ERROR_FUNCTIONS.keys()),
test_percentage=0.25,
save_dir=SAVE_DIR,
num_processes=1,
)
results_train, results_test = benchmark.run()
log.info("#### test_darts_local_model")
print(results_test)


def test_torch_prophet_model():
air_passengers_df = pd.read_csv(AIR_FILE, nrows=NROWS)
peyton_manning_df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
Expand Down
50 changes: 49 additions & 1 deletion tot/models/models_darts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)

try:
from darts.models import RegressionModel
from darts.models import LightGBMModel, RegressionModel

_darts_installed = True
except ImportError:
Expand Down Expand Up @@ -395,3 +395,51 @@ class RandomForestModel(DartsRegressionModel):
"""

regression_class: Type = RandomForestRegressor


@dataclass
class LGBMModel(DartsRegressionModel):
"""
A forecasting model using a LightGBMModel to obtain a forecast.

Examples
--------
>>> model_classes_and_params = [
>>> (
>>> LGBMModel,
>>> {"lags": 12, "n_forecasts": 4},
>>> ),
>>> ]
>>>
>>> benchmark = SimpleBenchmark(
>>> model_classes_and_params=model_classes_and_params,
>>> datasets=dataset_list,
>>> metrics=list(ERROR_FUNCTIONS.keys()),
>>> test_percentage=25,
>>> save_dir=SAVE_DIR,
>>> num_processes=1,
>>> )
"""

def __post_init__(self):
# check if installed
if not (_darts_installed or _sklearn_installed):
raise RuntimeError(
"Requires darts and sklearn to be installed:"
"https://scikit-learn.org/stable/install.html"
"https://github.com/unit8co/darts/blob/master/INSTALL.md"
)
params = deepcopy(self.params)
params.pop("_data_params")
# n_forecasts is not a parameter of the model
params.pop("n_forecasts")
params.pop("retrain", None)
params.pop("norm_mode", None)
params.pop("norm_type", None)
params.pop("norm_affine", None)
# overwrite output_chunk_length with n_forecasts
params.update({"output_chunk_length": self.params["n_forecasts"]})
self.model = LightGBMModel(**params)
self.n_forecasts = self.params["n_forecasts"]
self.n_lags = params["lags"]
# input checks are provided by model itself