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

Fix monotone constraint with tuple input. #7891

Merged
merged 1 commit into from
May 12, 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
4 changes: 3 additions & 1 deletion python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,10 +1405,12 @@ def __init__(
self.set_param(params_processed or {})

def _transform_monotone_constrains(
self, value: Union[Dict[str, int], str]
self, value: Union[Dict[str, int], str, Tuple[int, ...]]
) -> Union[Tuple[int, ...], str]:
if isinstance(value, str):
return value
if isinstance(value, tuple):
return value

constrained_features = set(value.keys())
feature_names = self.feature_names or []
Expand Down
5 changes: 5 additions & 0 deletions tests/python/test_monotone_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def test_monotone_constraints(self, tree_method: str, policy: str) -> None:
constrained = xgb.train(params_for_constrained, training_dset)
assert is_correctly_constrained(constrained)

def test_monotone_constraints_tuple(self) -> None:
params_for_constrained = {"monotone_constraints": (1, -1)}
constrained = xgb.train(params_for_constrained, training_dset)
assert is_correctly_constrained(constrained)

@pytest.mark.parametrize('format', [dict, list])
def test_monotone_constraints_feature_names(self, format):

Expand Down