Skip to content

Commit 16e7b4f

Browse files
committed
add docs and tests
1 parent 8789a25 commit 16e7b4f

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

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

0 commit comments

Comments
 (0)