lightgbm_callbacks package

class lightgbm_callbacks.CallbackBase[source]

Bases: object

class lightgbm_callbacks.DartEarlyStoppingCallback(stopping_rounds: int, first_metric_only: bool = False, verbose: bool = True, min_delta: float | list[float] = 0.0)[source]

Bases: EarlyStoppingCallback

A callback that activates early stopping.

Activates early stopping. The model will train until the validation score doesn’t improve by at least min_delta. Validation score needs to improve at least every stopping_rounds round(s) to continue training. Requires at least one validation data and one metric. If there’s more than one, will check all of them. But the training data is ignored anyway. To check only the first metric set first_metric_only to True. The index of iteration that has the best performance will be saved in the best_iteration attribute of a model.

When using dart, the model is copied and retained using pickle at iterations where scores no longer improve. Eventually, the number of iterations of the best_model should be max(best_iteration[i] + 2, num_boost_round/n_iter/n_estimators). (max(best_iteration[i] + 1, num_boost_round/n_iter/n_estimators) in case of normal early stopping.)

Parameters:
  • stopping_rounds (int) – The possible number of rounds without the trend occurrence.

  • first_metric_only (bool, optional (default=False)) – Whether to use only the first metric for early stopping.

  • verbose (bool, optional (default=True)) – Whether to log message with early stopping information. By default, standard output resource is used. Use register_logger() function to register a custom logger.

  • min_delta (float or list of float, optional (default=0.0)) – Minimum improvement in score to keep training. If float, this single value is used for all metrics. If list, its length should match the total number of metrics.

on_best_score_update_stopped(env: CallbackEnv, i: int) None[source]
class lightgbm_callbacks.EarlyStoppingCallback(stopping_rounds: int, first_metric_only: bool = False, verbose: bool = True, min_delta: float | list[float] = 0.0)[source]

Bases: object

A callback that activates early stopping.

Activates early stopping. The model will train until the validation score doesn’t improve by at least min_delta. Validation score needs to improve at least every stopping_rounds round(s) to continue training. Requires at least one validation data and one metric. If there’s more than one, will check all of them. But the training data is ignored anyway. To check only the first metric set first_metric_only to True. The index of iteration that has the best performance will be saved in the best_iteration attribute of a model.

Compared to the official implementation, the best_iteration information is retained even when using dart.

Parameters:
  • stopping_rounds (int) – The possible number of rounds without the trend occurrence.

  • first_metric_only (bool, optional (default=False)) – Whether to use only the first metric for early stopping.

  • verbose (bool, optional (default=True)) – Whether to log message with early stopping information. By default, standard output resource is used. Use register_logger() function to register a custom logger.

  • min_delta (float or list of float, optional (default=0.0)) – Minimum improvement in score to keep training. If float, this single value is used for all metrics. If list, its length should match the total number of metrics.

on_best_score_not_updated(env: CallbackEnv, i: int) None[source]
on_best_score_update_started(env: CallbackEnv, i: int) None[source]
on_best_score_update_stopped(env: CallbackEnv, i: int) None[source]
on_best_score_updated(env: CallbackEnv, i: int) None[source]
class lightgbm_callbacks.EstimatorWrapperBase(estimator: TEstimator)[source]

Bases: BaseEstimator, RegressorMixin, Generic[TEstimator]

A base class for estimator wrappers that delegates all attributes to the wrapped estimator.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') EstimatorWrapperBase

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class lightgbm_callbacks.LGBMDartEarlyStoppingEstimator(estimator: LGBMModel, *, early_stopping_factory: Callable[[int, bool, bool, float | list[float]], Callable[[CallbackEnv], None]] | Callable[[int, bool, bool], Callable[[CallbackEnv], None]] = <class 'lightgbm_callbacks._early_stopping_callback.DartEarlyStoppingCallback'>, stopping_rounds: int | None = None, first_metric_only: bool = False, verbose: bool = False, min_delta: float | None = None, eval_metric: str | Callable[[NDArray, NDArray], tuple[str, float, bool]] | None = None, test_size: float | int | None = None, train_size: float | int | None = None, random_state: int = 0, shuffle: bool = True, stratify: bool = False, split_enabled: bool = True, dart_early_stopping_method: Literal['save', 'refit', 'none'] = 'save', metric_idx: int = -1, tqdm_cls: Literal['auto', 'autonotebook', 'std', 'notebook', 'asyncio', 'keras', 'dask', 'tk', 'gui', 'rich', 'contrib.slack', 'contrib.discord', 'contrib.telegram', 'contrib.bells'] | type[tqdm.std.tqdm] | None = None, tqdm_kwargs: dict[str, Any] | None = None, **kwargs: Any)[source]

Bases: LGBMEarlyStoppingEstimator

LightGBM wrapper that does early stopping with sklearn.train_test_split and uses LGBMDartEarlyStoppingSimpleWrapper to support early stopping in dart mode.

fit(X, y=None, **fit_params) Self[source]

Fit the model according to the given training data.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LGBMDartEarlyStoppingEstimator

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class lightgbm_callbacks.LGBMDartEarlyStoppingSimpleWrapper(estimator: LGBMModel, *, method: Literal['save', 'refit', 'none'] = 'save', metric_idx: int = -1)[source]

Bases: EstimatorWrapperBase[LGBMModel]

A simple wrapper for dart LGBMModel that returns the best model after early stopping.

fit(X, y, **fit_params: Any) Self[source]
set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LGBMDartEarlyStoppingSimpleWrapper

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class lightgbm_callbacks.LGBMEarlyStoppingEstimator(estimator: LGBMModel, *, early_stopping_factory: Callable[[int, bool, bool, float | list[float]], Callable[[CallbackEnv], None]] | Callable[[int, bool, bool], Callable[[CallbackEnv], None]] = <class 'lightgbm_callbacks._early_stopping_callback.DartEarlyStoppingCallback'>, stopping_rounds: int | None = None, first_metric_only: bool = False, verbose: bool = False, min_delta: float | None = None, eval_metric: str | Callable[[NDArray, NDArray], tuple[str, float, bool]] | None = None, test_size: float | int | None = None, train_size: float | int | None = None, random_state: int = 0, shuffle: bool = True, stratify: bool = False, split_enabled: bool = True, tqdm_cls: Literal['auto', 'autonotebook', 'std', 'notebook', 'asyncio', 'keras', 'dask', 'tk', 'gui', 'rich', 'contrib.slack', 'contrib.discord', 'contrib.telegram', 'contrib.bells'] | type[tqdm.std.tqdm] | None = None, tqdm_kwargs: dict[str, Any] | None = None, **kwargs: Any)[source]

Bases: EstimatorWrapperBase[LGBMModel]

LightGBM wrapper that does early stopping with sklearn.train_test_split.

fit(X, y=None, **fit_params) Self[source]

Fit the model according to the given training data.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LGBMEarlyStoppingEstimator

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class lightgbm_callbacks.ProgressBarCallback(tqdm_cls: Literal['auto', 'autonotebook', 'std', 'notebook', 'asyncio', 'keras', 'dask', 'tk', 'gui', 'rich', 'contrib.slack', 'contrib.discord', 'contrib.telegram', 'contrib.bells'] | type[tqdm.std.tqdm] | None = 'auto', early_stopping_callback: Any | None = None, **tqdm_kwargs: Any)[source]

Bases: CallbackBase

pbar: tqdm.std.tqdm | None
tqdm_cls: type[tqdm.std.tqdm] | None

Subpackages