From a92e0f6240fc68a9a1d96bdab8acdf3539cf6b9e Mon Sep 17 00:00:00 2001 From: Cheng Li <45052202+lichengphysics@users.noreply.github.com> Date: Tue, 1 Mar 2022 02:10:15 -0800 Subject: [PATCH] multi groups in the constraints (#7711) --- python-package/xgboost/core.py | 4 ++-- tests/python/test_interaction_constraints.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index f22371ab8..0f528270a 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -1393,9 +1393,9 @@ class Booster: + ",".join( [feature_idx_mapping[feature_name] for feature_name in constraint] ) - + "]" + + "]," ) - return s + "]" + return s[:-1] + "]" except KeyError as e: raise ValueError( "Constrained features are not a subset of training data feature names" diff --git a/tests/python/test_interaction_constraints.py b/tests/python/test_interaction_constraints.py index 12653b538..18d416501 100644 --- a/tests/python/test_interaction_constraints.py +++ b/tests/python/test_interaction_constraints.py @@ -78,6 +78,12 @@ class TestInteractionConstraints: feature_names=feature_names, interaction_constraints=constraints) + constraints = [['feature_0', 'feature_1'], ['feature_2']] + feature_names = ['feature_0', 'feature_1', 'feature_2'] + self.run_interaction_constraints(tree_method='exact', + feature_names=feature_names, + interaction_constraints=constraints) + @pytest.mark.skipif(**tm.no_sklearn()) def training_accuracy(self, tree_method): """Test accuracy, reused by GPU tests."""