Skip to content

Commit

Permalink
Fix monotone constraint with tuple input. (#7891)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis authored May 12, 2022
1 parent 94ca52b commit db80671
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
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

0 comments on commit db80671

Please sign in to comment.