Skip to content

Commit

Permalink
[python-package] add validate_features argument to refit() (micro…
Browse files Browse the repository at this point in the history
…soft#5331)

add validate_features to refit
  • Loading branch information
jmoralez authored Jul 3, 2022
1 parent dc59072 commit 25e32e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,6 +3602,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 @@ -3645,6 +3646,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 @@ -3659,7 +3663,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 @@ -3657,3 +3657,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)

0 comments on commit 25e32e9

Please sign in to comment.