Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-package] Allow to pass early stopping min delta in params #6274

Merged
merged 16 commits into from
May 1, 2024
Merged
2 changes: 2 additions & 0 deletions python-package/lightgbm/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def train(
callback.early_stopping(
stopping_rounds=params["early_stopping_round"], # type: ignore[arg-type]
first_metric_only=first_metric_only,
min_delta=params.get("early_stopping_min_delta", 0.0),
verbose=_choose_param_value(
main_param_name="verbosity",
params=params,
Expand Down Expand Up @@ -737,6 +738,7 @@ def cv(
callback.early_stopping(
stopping_rounds=params["early_stopping_round"], # type: ignore[arg-type]
first_metric_only=first_metric_only,
min_delta=params.get("early_stopping_min_delta", 0.0),
verbose=_choose_param_value(
main_param_name="verbosity",
params=params,
Expand Down
12 changes: 8 additions & 4 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,11 @@ def train_fn():
assert bst.best_iteration == 0


@pytest.mark.parametrize('first_metric_only', [True, False])
def test_early_stopping_via_global_params(first_metric_only):
@pytest.mark.parametrize(
('first_metric_only', 'early_stopping_min_delta'),
[(True, 0.0), (True, 1e3), (False, 0.0)]
)
def test_early_stopping_via_global_params(first_metric_only, early_stopping_min_delta):
borchero marked this conversation as resolved.
Show resolved Hide resolved
X, y = load_breast_cancer(return_X_y=True)
num_trees = 5
params = {
Expand All @@ -991,7 +994,8 @@ def test_early_stopping_via_global_params(first_metric_only):
'metric': 'None',
'verbose': -1,
'early_stopping_round': 2,
'first_metric_only': first_metric_only
'first_metric_only': first_metric_only,
'early_stopping_min_delta': early_stopping_min_delta,
}
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
lgb_train = lgb.Dataset(X_train, y_train)
Expand All @@ -1002,7 +1006,7 @@ def test_early_stopping_via_global_params(first_metric_only):
feval=[decreasing_metric, constant_metric],
valid_sets=lgb_eval,
valid_names=valid_set_name)
if first_metric_only:
if first_metric_only and early_stopping_min_delta == 0:
assert gbm.best_iteration == num_trees
else:
assert gbm.best_iteration == 1
Expand Down