Skip to main content

A collection of LightGBM callbacks.

Project description

LightGBM Callbacks

CI Status Documentation Status Test coverage percentage

Poetry black pre-commit

PyPI Version Supported Python versions License

A collection of LightGBM callbacks. Provides implementations of ProgressBarCallback (#5867) and DartEarlyStoppingCallback (#4805), as well as an LGBMDartEarlyStoppingEstimator that automatically passes these callbacks. (#3313, #5808)

Installation

Install this via pip (or your favourite package manager):

pip install lightgbm-callbacks

Usage

SciKit-Learn API, simple

from lightgbm import LGBMRegressor
from lightgbm_callbacks import LGBMDartEarlyStoppingEstimator
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
LGBMDartEarlyStoppingEstimator(
    LGBMRegressor(boosting_type="dart"), # or "gbdt", ...
    stopping_rounds=10, # or n_iter_no_change=10
    test_size=0.2, # or validation_fraction=0.2
    shuffle=False,
    tqdm_cls="rich", # "auto", "autonotebook", ...
).fit(X_train, y_train)

Scikit-Learn API, manually passing callbacks

from lightgbm import LGBMRegressor
from lightgbm_callbacks import ProgressBarCallback, DartEarlyStoppingCallback
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train)
early_stopping_callback = DartEarlyStoppingCallback(stopping_rounds=10)
LGBMRegressor(
).fit(
    X_train,
    y_train,
    eval_set=[(X_train, y_train), (X_val, y_val)],
    callbacks=[
        early_stopping_callback,
        ProgressBarCallback(early_stopping_callback=early_stopping_callback),
    ],
)

Details on DartEarlyStoppingCallback

Below is a description of the DartEarlyStoppingCallback method parameter and lgb.plot_metric for each lgb.LGBMRegressor(boosting_type="dart", n_estimators=1000) trained with entire sklearn_datasets.load_diabetes() dataset.

Method Description iteration Image Actual iteration
(Baseline) If Early stopping is not used. n_estimators image 1000
"none" Do nothing and return the original estimator. min(best_iteration + early_stopping_rounds, n_estimators) image 50
"save" Save the best model by deepcopying the estimator and return the best model (using pickle). min(best_iteration + 1, n_estimators) image 21
"refit" Refit the estimator with the best iteration and return the refitted estimator. min(best_iteration, n_estimators) image 20

Contributors ✨

Thanks goes to these wonderful people (emoji key):

34j
34j

💻 🤔 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lightgbm_callbacks-0.1.16.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

lightgbm_callbacks-0.1.16-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file lightgbm_callbacks-0.1.16.tar.gz.

File metadata

  • Download URL: lightgbm_callbacks-0.1.16.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.11.2 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.3 tqdm/4.67.0 importlib-metadata/8.5.0 keyring/25.5.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15

File hashes

Hashes for lightgbm_callbacks-0.1.16.tar.gz
Algorithm Hash digest
SHA256 0bcb6d444dd9c396069d6e823f6c37ced400d4b8c7d84ea8904bd453b1f5c997
MD5 bbc07a59ef7ee509e7f345db57c54e57
BLAKE2b-256 b40d9d3dccec4fca6ba3b5ab2a202519e34ee8f54c37da9393cab49afc8fcc3c

See more details on using hashes here.

File details

Details for the file lightgbm_callbacks-0.1.16-py3-none-any.whl.

File metadata

  • Download URL: lightgbm_callbacks-0.1.16-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.11.2 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.3 tqdm/4.67.0 importlib-metadata/8.5.0 keyring/25.5.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15

File hashes

Hashes for lightgbm_callbacks-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 136c3d40805834ae1eefcd6e3468bf2f4964271fdb0f2e3499341a57d2cf8145
MD5 206c7f1f175dc2bb1cbe571524f1c494
BLAKE2b-256 7cff238cb10842c34f726774d3b08d1ce8716c387847a6a1c8917e0075c9e4ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page