|
| 1 | +"""Experiment adapter for LightGBM cross-validation experiments.""" |
| 2 | + |
| 3 | +# copyright: hyperactive developers, MIT License (see LICENSE file) |
| 4 | + |
| 5 | +from hyperactive.experiment.integrations.sklearn_cv import SklearnCvExperiment |
| 6 | + |
| 7 | + |
| 8 | +class LightGBMExperiment(SklearnCvExperiment): |
| 9 | + """Experiment adapter for LightGBM cross-validation experiments. |
| 10 | +
|
| 11 | + Thin wrapper around ``SklearnCvExperiment`` for LightGBM estimators. |
| 12 | + LightGBM's sklearn-compatible API (``LGBMClassifier``, ``LGBMRegressor``) |
| 13 | + works without adaptation. This class exists for discoverability, explicit |
| 14 | + soft-dependency tracking via the ``python_dependencies`` tag, and as an |
| 15 | + extension point for future LightGBM-specific behavior. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + estimator : LGBMClassifier or LGBMRegressor |
| 20 | + The LightGBM estimator to evaluate. Any sklearn-compatible estimator |
| 21 | + is accepted, but LightGBM estimators are the intended use case. |
| 22 | + X : array-like, shape (n_samples, n_features) |
| 23 | + Input data. |
| 24 | + y : array-like, shape (n_samples,) |
| 25 | + Target values. |
| 26 | + scoring : callable or str, default=None |
| 27 | + Scoring function. Defaults follow ``SklearnCvExperiment`` conventions: |
| 28 | + ``accuracy_score`` for classifiers, ``mean_squared_error`` for |
| 29 | + regressors. |
| 30 | + cv : int or cross-validation generator, default=KFold(n_splits=3, shuffle=True) |
| 31 | + Cross-validation strategy. |
| 32 | +
|
| 33 | + Notes |
| 34 | + ----- |
| 35 | + LightGBM prints training logs to stdout by default. Pass |
| 36 | + ``verbosity=-1`` to the estimator constructor to suppress this output. |
| 37 | +
|
| 38 | + For all remaining parameter details see ``SklearnCvExperiment``. |
| 39 | +
|
| 40 | + Examples |
| 41 | + -------- |
| 42 | + >>> from hyperactive.experiment.integrations import LightGBMExperiment |
| 43 | + >>> from lightgbm import LGBMClassifier |
| 44 | + >>> from sklearn.datasets import load_iris |
| 45 | + >>> X, y = load_iris(return_X_y=True) |
| 46 | + >>> exp = LightGBMExperiment( |
| 47 | + ... estimator=LGBMClassifier(verbosity=-1), |
| 48 | + ... X=X, |
| 49 | + ... y=y, |
| 50 | + ... ) |
| 51 | + >>> params = {"n_estimators": 50, "max_depth": 3} |
| 52 | + >>> score, metadata = exp.score(params) |
| 53 | + """ |
| 54 | + |
| 55 | + _tags = { |
| 56 | + "authors": ["kajal-jotwani"], |
| 57 | + "python_dependencies": "lightgbm", |
| 58 | + } |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def get_test_params(cls, parameter_set="default"): |
| 62 | + """Return testing parameter settings for the estimator.""" |
| 63 | + from skbase.utils.dependencies import _check_soft_dependencies |
| 64 | + |
| 65 | + if not _check_soft_dependencies("lightgbm", severity="none"): |
| 66 | + return [] |
| 67 | + |
| 68 | + from lightgbm import LGBMClassifier, LGBMRegressor |
| 69 | + from sklearn.datasets import load_diabetes, load_iris |
| 70 | + |
| 71 | + X, y = load_iris(return_X_y=True) |
| 72 | + params0 = { |
| 73 | + "estimator": LGBMClassifier(n_estimators=10, verbosity=-1), |
| 74 | + "X": X, |
| 75 | + "y": y, |
| 76 | + "cv": 2, |
| 77 | + } |
| 78 | + |
| 79 | + X, y = load_diabetes(return_X_y=True) |
| 80 | + params1 = { |
| 81 | + "estimator": LGBMRegressor(n_estimators=10, verbosity=-1), |
| 82 | + "X": X, |
| 83 | + "y": y, |
| 84 | + "cv": 2, |
| 85 | + } |
| 86 | + |
| 87 | + return [params0, params1] |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def _get_score_params(cls): |
| 91 | + """Return parameter settings for score/evaluate tests.""" |
| 92 | + from skbase.utils.dependencies import _check_soft_dependencies |
| 93 | + |
| 94 | + if not _check_soft_dependencies("lightgbm", severity="none"): |
| 95 | + return [] |
| 96 | + |
| 97 | + score_params = {"n_estimators": 5, "max_depth": 2} |
| 98 | + return [score_params, score_params] |
0 commit comments