turn off debug log output of lightgbm sklearn interface

36 Views Asked by At

How do I turn off debug log output of lightgbm sklearn interface? Tried

regressor = lightgbm.LGBMRegressor(verbose=-1)

does not work.

regressor.fit(verbose=-1)

is not accepted.

What I found is through lightgbm itself, not sklearn interface which I want to use. How can I stop the log output of lightgbm?

Thanks.

1

There are 1 best solutions below

0
James Lamb On

Short Answer

These two mechanisms are enough to suppress all log output from lightgbm's scikit-learn interface

  • pass verbosity=-1 to constructor of estimators like LGBMRegressor()
  • use warnings.filterwarnings("ignore", category=UserWarning)

Details

With lightgbm>=4.3.0, just passing verbosity=-1 into the constructor should be sufficient.

Consider this minimal, reproducible example:

import lightgbm as lgb
from sklearn.datasets import make_regression

# create datasets
X, y = make_regression(
    n_samples=10_000,
    n_features=10,
)

# create regressor and fit to data
clf = lgb.LGBMRegressor(
    n_estimators=10,
    num_leaves=31,
    verbosity=-1
).fit(X, y)

With Python 3.11, lightgbm==4.3.0, and scikit-learn==1.4.1, that does not produce any log output.

The same code but with verbosity=1 instead produces this output:

[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000161 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 2550
[LightGBM] [Info] Number of data points in the train set: 10000, number of used features: 10
[LightGBM] [Info] Start training from score -0.050604

As of v4.3.0, there is one source of possible log output that cannot be suppressed via the verbosity keyword argument. Any parameters passed into LGBMRegressor() which don't match its keyword arguments are collected and passed down to lightgbm.train(), and lightgbm sometimes warns when it uses such values.

Like this:

clf = lgb.LGBMRegressor(
    num_boost_round=10,
    num_leaves=31,
    verbosity=-1
).fit(X, y)

Produces this:

.../lib/python3.11/site-packages/lightgbm/engine.py:177: UserWarning: Found `num_boost_round` in params. Will use it instead of argument
  _log_warning(f"Found `{alias}` in params. Will use it instead of argument")

You can suppress those using Python's warning-control mechanisms, described in the documentation for the warnings module (docs link).

Add this prior to calling any lightgbm code:

import warnings
warnings.filterwarnings("ignore", category=UserWarning)