Support configuring constraints by feature names (#6783)
Co-authored-by: fis <jm.yuan@outlook.com>
This commit is contained in:
@@ -9,7 +9,7 @@ rng = np.random.RandomState(1994)
|
||||
|
||||
|
||||
class TestInteractionConstraints:
|
||||
def run_interaction_constraints(self, tree_method):
|
||||
def run_interaction_constraints(self, tree_method, feature_names=None, interaction_constraints='[[0, 1]]'):
|
||||
x1 = np.random.normal(loc=1.0, scale=1.0, size=1000)
|
||||
x2 = np.random.normal(loc=1.0, scale=1.0, size=1000)
|
||||
x3 = np.random.choice([1, 2, 3], size=1000, replace=True)
|
||||
@@ -17,13 +17,13 @@ class TestInteractionConstraints:
|
||||
+ np.random.normal(
|
||||
loc=0.001, scale=1.0, size=1000) + 3 * np.sin(x1)
|
||||
X = np.column_stack((x1, x2, x3))
|
||||
dtrain = xgboost.DMatrix(X, label=y)
|
||||
dtrain = xgboost.DMatrix(X, label=y, feature_names=feature_names)
|
||||
|
||||
params = {
|
||||
'max_depth': 3,
|
||||
'eta': 0.1,
|
||||
'nthread': 2,
|
||||
'interaction_constraints': '[[0, 1]]',
|
||||
'interaction_constraints': interaction_constraints,
|
||||
'tree_method': tree_method
|
||||
}
|
||||
num_boost_round = 12
|
||||
@@ -35,7 +35,7 @@ class TestInteractionConstraints:
|
||||
# by the same amount
|
||||
def f(x):
|
||||
tmat = xgboost.DMatrix(
|
||||
np.column_stack((x1, x2, np.repeat(x, 1000))))
|
||||
np.column_stack((x1, x2, np.repeat(x, 1000))), feature_names=feature_names)
|
||||
return bst.predict(tmat)
|
||||
|
||||
preds = [f(x) for x in [1, 2, 3]]
|
||||
@@ -57,6 +57,26 @@ class TestInteractionConstraints:
|
||||
def test_approx_interaction_constraints(self):
|
||||
self.run_interaction_constraints(tree_method='approx')
|
||||
|
||||
def test_interaction_constraints_feature_names(self):
|
||||
with pytest.raises(ValueError):
|
||||
constraints = [('feature_0', 'feature_1')]
|
||||
self.run_interaction_constraints(tree_method='exact',
|
||||
interaction_constraints=constraints)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
constraints = [('feature_0', 'feature_3')]
|
||||
feature_names = ['feature_0', 'feature_1', 'feature_2']
|
||||
self.run_interaction_constraints(tree_method='exact',
|
||||
feature_names=feature_names,
|
||||
interaction_constraints=constraints)
|
||||
|
||||
|
||||
constraints = [('feature_0', 'feature_1')]
|
||||
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):
|
||||
from sklearn.metrics import accuracy_score
|
||||
|
||||
Reference in New Issue
Block a user