Skip to content

fix(model): upgrade LightGBM API usage to modern callbacks (#904) - #2347

Open
Yash Dharmendra Parmar (Yashparmar1125) wants to merge 1 commit into
microsoft:mainfrom
Yashparmar1125:upgrade-lightgbm-api-904
Open

fix(model): upgrade LightGBM API usage to modern callbacks (#904)#2347
Yash Dharmendra Parmar (Yashparmar1125) wants to merge 1 commit into
microsoft:mainfrom
Yashparmar1125:upgrade-lightgbm-api-904

Conversation

@Yashparmar1125

Copy link
Copy Markdown

Description

This PR modernizes LightGBM API usage across Qlib and updates related documentation and models to eliminate deprecation warnings from legacy arguments:

  • Replaced deprecated lgb.train parameters (early_stopping_rounds, verbose_eval, evals_result) with modern callback objects (lgb.early_stopping, lgb.log_evaluation, lgb.record_evaluation).
  • Introduced a helper _parse_callbacks to cleanly construct callback lists and avoid redundant boilerplate across LightGBM models.
  • Safely handles early_stopping_rounds=None without raising TypeError: early_stopping_round should be an integer. Got 'NoneType'.
  • Safely handles verbose_eval for both boolean values (True/False) and integer periods.
  • Added support for passing custom callbacks via **kwargs into fit() and finetune() without causing duplicate keyword argument errors.
  • Updated documentation in docs/start/integration.rst to demonstrate modern callback-based training.
  • Added comprehensive unit tests in tests/model/test_lightgbm.py.

Motivation and Context

Resolves #904

In recent versions of LightGBM (v3.3+ and v4.x), passing parameters like early_stopping_rounds, verbose_eval, and evals_result directly to lgb.train emits UserWarning deprecation messages and can cause runtime exceptions when early stopping is disabled (None). This PR brings Qlib's LightGBM integration in line with modern standards while maintaining full backward compatibility.

How Has This Been Tested?

  • Pass the test by running: pytest qlib/tests/test_all_pipeline.py under upper directory of qlib.
  • If you are adding a new feature, test on your own test scripts.

Ran test suites and style checks:

  1. pytest tests/model/test_lightgbm.py tests/test_contrib_model.py -v (5/5 passed)
  2. black --check qlib/contrib/model/gbdt.py qlib/contrib/model/highfreq_gdbt_model.py tests/model/test_lightgbm.py -l 120 (clean)
  3. flake8 --ignore=E501,E266,E402,W503,E731,E203 --per-file-ignores="__init__.py:F401,F403" qlib/contrib/model/gbdt.py qlib/contrib/model/highfreq_gdbt_model.py tests/model/test_lightgbm.py (0 warnings)

Screenshots of Test Results (if appropriate):

  1. Test suite output:
tests\model\test_lightgbm.py::TestLightGBM::test_hflgb_model PASSED      [ 20%]
tests\model\test_lightgbm.py::TestLightGBM::test_lgb_model PASSED        [ 40%]
tests\model\test_lightgbm.py::TestLightGBM::test_lgb_model_custom_callbacks PASSED [ 60%]
tests\model\test_lightgbm.py::TestLightGBM::test_lgb_model_no_early_stopping PASSED [ 80%]
tests\test_contrib_model.py::TestAllFlow::test_0_initialize PASSED       [100%]
======================= 5 passed in 3.04s =======================

- Replace deprecated LightGBM parameters (early_stopping_rounds, verbose_eval, evals_result) in documentation with callbacks
- Add helper _parse_callbacks to safely construct callbacks list
- Handle early_stopping_rounds=None safely without raising TypeError
- Support custom callbacks passed via kwargs in LGBModel and HFLGBModel
- Support kwargs in finetune() across LGBModel and HFLGBModel
- Add unit tests for LightGBM callback and model compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upgrading lightGBM API usage

1 participant