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] add validate_features argument to refit #5331

Merged
merged 1 commit into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3585,6 +3585,7 @@ def refit(
categorical_feature='auto',
dataset_params=None,
free_raw_data=True,
validate_features=False,
**kwargs
):
"""Refit the existing Booster by new data.
Expand Down Expand Up @@ -3628,6 +3629,9 @@ def refit(
Other parameters for Dataset ``data``.
free_raw_data : bool, optional (default=True)
If True, raw data is freed after constructing inner Dataset for ``data``.
validate_features : bool, optional (default=False)
If True, ensure that the features used to refit the model match the original ones.
Used only if data is pandas DataFrame.
**kwargs
Other parameters for refit.
These parameters will be passed to ``predict`` method.
Expand All @@ -3642,7 +3646,7 @@ def refit(
if dataset_params is None:
dataset_params = {}
predictor = self._to_predictor(deepcopy(kwargs))
leaf_preds = predictor.predict(data, -1, pred_leaf=True)
leaf_preds = predictor.predict(data, -1, pred_leaf=True, validate_features=validate_features)
nrow, ncol = leaf_preds.shape
out_is_linear = ctypes.c_int(0)
_safe_call(_LIB.LGBM_BoosterGetLinear(
Expand Down
7 changes: 7 additions & 0 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3642,3 +3642,10 @@ def test_validate_features():

# check that disabling the check doesn't raise the error
bst.predict(df2, validate_features=False)

# try to refit with a different feature
with pytest.raises(lgb.basic.LightGBMError, match="Expected 'x3' at position 2 but found 'z'"):
bst.refit(df2, y, validate_features=True)

# check that disabling the check doesn't raise the error
bst.refit(df2, y, validate_features=False)