Skip to content

Commit 08199ef

Browse files
committed
add docs and tests
1 parent 8789a25 commit 08199ef

File tree

4 files changed

+145
-2
lines changed

4 files changed

+145
-2
lines changed

docs/source/_snippets/user_guide/integrations.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,41 @@ def configure_optimizers(self):
226226
best_params = optimizer.solve()
227227
# [end:pytorch_lightning]
228228

229+
# [start:lightgbm_experiment]
230+
from lightgbm import LGBMClassifier
231+
from sklearn.datasets import load_iris
232+
233+
from hyperactive.experiment.integrations import LightGBMExperiment
234+
from hyperactive.opt.gfo import BayesianOptimizer
235+
236+
# Load data
237+
X, y = load_iris(return_X_y=True)
238+
239+
# Create the experiment
240+
experiment = LightGBMExperiment(
241+
estimator=LGBMClassifier(),
242+
X=X,
243+
y=y,
244+
cv=3,
245+
)
246+
247+
# Define search space
248+
search_space = {
249+
"n_estimators": [50, 100, 200],
250+
"max_depth": [3, 5, 7, -1],
251+
"learning_rate": [0.01, 0.05, 0.1, 0.2],
252+
}
253+
254+
# Optimize
255+
optimizer = BayesianOptimizer(
256+
search_space=search_space,
257+
n_iter=10,
258+
experiment=experiment,
259+
)
260+
best_params = optimizer.solve()
261+
print(f"Best parameters: {best_params}")
262+
# [end:lightgbm_experiment]
263+
229264

230265
# --- Runnable test code below ---
231266
if __name__ == "__main__":

docs/source/api_reference/experiments_integrations.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The :mod:`hyperactive.experiment.integrations` module contains experiment classe
77
for integration with machine learning frameworks.
88

99
These experiments provide seamless hyperparameter optimization for scikit-learn,
10-
sktime, skpro, and PyTorch Lightning models.
10+
sktime, skpro, PyTorch Lightning, and LightGBM models.
1111

1212
Scikit-Learn
1313
------------
@@ -55,3 +55,14 @@ Experiments for PyTorch Lightning models.
5555
:template: class.rst
5656

5757
TorchExperiment
58+
59+
LightGBM
60+
--------
61+
62+
Cross-validation experiments for LightGBM estimators.
63+
64+
.. autosummary::
65+
:toctree: auto_generated/
66+
:template: class.rst
67+
68+
LightGBMExperiment

docs/source/user_guide/integrations.rst

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Framework Integrations
77
Hyperactive integrates with popular ML frameworks, providing drop-in replacements
88
for tools like ``GridSearchCV``. Each ML framework has its own conventions for training and evaluation. The integration
99
classes handle cross-validation setup, scoring metrics, and parameter translation, so
10-
you can use any optimizer with scikit-learn, sktime, skpro, or PyTorch models.
10+
you can use any optimizer with scikit-learn, sktime, skpro, PyTorch, or LightGBM models.
1111

1212
----
1313

@@ -53,6 +53,15 @@ Supported Frameworks
5353

5454
Deep learning models
5555

56+
.. grid-item-card:: LightGBM
57+
:class-card: sd-border-info
58+
:link: #lightgbm-integration
59+
:link-type: url
60+
61+
**LightGBMExperiment**
62+
63+
Gradient boosting models
64+
5665
----
5766

5867
Quick Reference
@@ -86,6 +95,10 @@ Quick Reference
8695
- ``TorchExperiment``
8796
- Deep learning models
8897
- ``[all_extras]``
98+
* - LightGBM
99+
- ``LightGBMExperiment``
100+
- Classification, regression
101+
- ``[lightgbm]``
89102

90103
----
91104

@@ -237,6 +250,34 @@ For deep learning hyperparameter optimization with PyTorch Lightning:
237250

238251
----
239252

253+
LightGBM Integration
254+
--------------------
255+
256+
For gradient boosting hyperparameter optimization with LightGBM:
257+
258+
.. note::
259+
260+
Requires ``pip install lightgbm``
261+
262+
.. grid:: 1
263+
:gutter: 0
264+
265+
.. grid-item::
266+
:class: sd-bg-light sd-pt-3 sd-pb-1 sd-ps-3 sd-pe-3 sd-rounded-3
267+
268+
**Key Features**
269+
270+
- Optimize LightGBM classifiers and regressors
271+
- LightGBM follows the sklearn API, so cross-validation works out of the box
272+
- Supports all LightGBM hyperparameters (``n_estimators``, ``max_depth``, ``learning_rate``, etc.)
273+
274+
.. literalinclude:: ../_snippets/user_guide/integrations.py
275+
:language: python
276+
:start-after: # [start:lightgbm_experiment]
277+
:end-before: # [end:lightgbm_experiment]
278+
279+
----
280+
240281
Tips
241282
----
242283

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Integration test for end-to-end usage of optimizer with LightGBM experiment."""
2+
# copyright: hyperactive developers, MIT License (see LICENSE file)
3+
4+
import pytest
5+
6+
7+
@pytest.mark.skipif(
8+
not pytest.importorskip("lightgbm", reason="lightgbm not installed"),
9+
reason="lightgbm not installed",
10+
)
11+
def test_endtoend_lightgbm():
12+
"""Test end-to-end usage of HillClimbing optimizer with LightGBM experiment."""
13+
from skbase.utils.dependencies import _check_soft_dependencies
14+
15+
if not _check_soft_dependencies("lightgbm", severity="none"):
16+
pytest.skip("lightgbm not installed")
17+
18+
# define the experiment
19+
from lightgbm import LGBMClassifier
20+
from sklearn.datasets import load_iris
21+
22+
from hyperactive.experiment.integrations import LightGBMExperiment
23+
24+
X, y = load_iris(return_X_y=True)
25+
26+
lgbm_exp = LightGBMExperiment(
27+
estimator=LGBMClassifier(n_estimators=10, verbosity=-1),
28+
X=X,
29+
y=y,
30+
cv=2,
31+
)
32+
33+
# set up the HillClimbing optimizer
34+
import numpy as np
35+
36+
from hyperactive.opt import HillClimbing
37+
38+
hillclimbing_config = {
39+
"search_space": {
40+
"n_estimators": np.array([5, 10, 20]),
41+
"max_depth": np.array([2, 3, 5]),
42+
},
43+
"n_iter": 10,
44+
}
45+
hill_climbing = HillClimbing(**hillclimbing_config, experiment=lgbm_exp)
46+
47+
# run the HillClimbing optimizer
48+
hill_climbing.solve()
49+
50+
best_params = hill_climbing.best_params_
51+
assert best_params is not None, "Best parameters should not be None"
52+
assert isinstance(best_params, dict), "Best parameters should be a dictionary"
53+
assert (
54+
"n_estimators" in best_params
55+
), "Best parameters should contain 'n_estimators'"
56+
assert "max_depth" in best_params, "Best parameters should contain 'max_depth'"

0 commit comments

Comments
 (0)