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

Explainable boosting parameters #6335

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f47f823
First version of the new parameter "tree_interaction_constraints""
Apr 7, 2022
5730198
readme update
Apr 7, 2022
5d69338
First version of the new parameter "tree_interaction_constraints""
Apr 7, 2022
ec9ed61
readme update
Apr 7, 2022
bfac4e1
Merge branch 'master' into microsoft-master
veneres Feb 14, 2024
f5b391e
Merge pull request #2 from veneres/microsoft-master
veneres Feb 14, 2024
d1966c2
Updated readme
veneres Feb 14, 2024
6438f0e
Merge remote-tracking branch 'upstream/master'
veneres Feb 14, 2024
848fd58
Fix missing parenthesis
veneres Feb 14, 2024
d32b7f6
Temporarly remove a new test
veneres Feb 14, 2024
d216823
Merge with private repository edits
veneres Feb 15, 2024
8dabbb2
Merge remote-tracking branch 'upstream/master'
veneres Feb 15, 2024
137bc6d
Resolved lint errors identified by github actions
veneres Feb 15, 2024
9b3fb5e
Fix docs
veneres Feb 15, 2024
997e06b
Fix docs
veneres Feb 15, 2024
64ff80c
Fix docs and linting
veneres Feb 15, 2024
ee8d6e6
Fix docs
veneres Feb 15, 2024
09acfcf
Fix docs
veneres Feb 15, 2024
0d66bea
Boolean guards added for constrained learning
veneres Feb 16, 2024
84287f1
test and small fix added
veneres Feb 16, 2024
61727ca
Merge branch 'microsoft:master' into master
veneres Feb 21, 2024
227ec1b
Param name refactor
veneres Feb 21, 2024
ca3dac5
Interaction constraints test added
veneres Feb 21, 2024
ab04352
Addressed: Unnecessary `list` comprehension (rewrite using `list()`)
veneres Feb 21, 2024
2fc53c0
Merge remote-tracking branch 'upstream/master'
veneres Feb 22, 2024
c0a4591
Skip constraint test on CUDA for the moment
veneres Feb 22, 2024
8165317
Reformat file for ruff check
veneres Feb 22, 2024
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
Prev Previous commit
Reformat file for ruff check
  • Loading branch information
veneres committed Feb 22, 2024
commit 8165317e322f94391300db9f2b170d9ef4005ae6
45 changes: 28 additions & 17 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
@@ -3458,15 +3458,20 @@ def test_interaction_constraints():
num_boost_round=10,
)

@pytest.mark.skipif(getenv('TASK', '') == 'cuda', reason='Interaction constraints are not yet supported on the CUDA version')

@pytest.mark.skipif(
getenv("TASK", "") == "cuda", reason="Interaction constraints are not yet supported on the CUDA version"
)
def test_tree_interaction_constraints():
def check_consistency(est, tree_interaction_constraints):
feat_to_index = {feat: i for i, feat in enumerate(est.feature_name())}
tree_df = est.trees_to_dataframe()
inter_found = set()
for tree_index in tree_df["tree_index"].unique():
tree_df_per_index = tree_df[tree_df["tree_index"] == tree_index]
feat_used = [feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None]
feat_used = [
feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None
]
inter_found.add(tuple(sorted(feat_used)))
for feats_found in inter_found:
found = False
@@ -3475,16 +3480,17 @@ def check_consistency(est, tree_interaction_constraints):
found = True
break
assert found is True

X, y = make_synthetic_regression(n_samples=400, n_features=30)
num_features = X.shape[1]
train_data = lgb.Dataset(X, label=y)
# check that tree constraint containing all features is equivalent to no constraint
params = {'verbose': -1,
'seed': 0}
params = {"verbose": -1, "seed": 0}
est = lgb.train(params, train_data, num_boost_round=10)
pred1 = est.predict(X)
est = lgb.train(dict(params, tree_interaction_constraints=[list(range(num_features))]), train_data,
num_boost_round=10)
est = lgb.train(
dict(params, tree_interaction_constraints=[list(range(num_features))]), train_data, num_boost_round=10
)
pred2 = est.predict(X)
np.testing.assert_allclose(pred1, pred2)

@@ -3507,24 +3513,26 @@ def check_consistency(est, tree_interaction_constraints):
check_consistency(est, tree_interaction_constraints)



@pytest.mark.skipif(getenv('TASK', '') == 'cuda', reason='Interaction constraints are not yet supported on the CUDA version')
@pytest.mark.skipif(
getenv("TASK", "") == "cuda", reason="Interaction constraints are not yet supported on the CUDA version"
)
def test_max_tree_interactions():
def check_n_interactions(est):
feat_to_index = {feat: i for i, feat in enumerate(est.feature_name())}
tree_df = est.trees_to_dataframe()
max_n_interactions = 0
for tree_index in tree_df["tree_index"].unique():
tree_df_per_index = tree_df[tree_df["tree_index"] == tree_index]
feat_used = [feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None]
feat_used = [
feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None
]
max_n_interactions = max(max_n_interactions, len(feat_used))
assert max_n_interactions <= est.params["max_tree_interactions"]

X, y = make_synthetic_regression(n_samples=400, n_features=30)
train_data = lgb.Dataset(X, label=y)
# check that limiting the number of interaction to the number of features is equivalent to no constraint
params = {'verbose': -1,
'seed': 0}
params = {"verbose": -1, "seed": 0}
est = lgb.train(params, train_data, num_boost_round=100)
pred1 = est.predict(X)
est = lgb.train(dict(params, max_tree_interactions=400), train_data, num_boost_round=100)
@@ -3545,18 +3553,23 @@ def check_n_interactions(est):
est = lgb.train(new_params, train_data, num_boost_round=100)
check_n_interactions(est)

@pytest.mark.skipif(getenv('TASK', '') == 'cuda', reason='Interaction constraints are not yet supported on the CUDA version')

@pytest.mark.skipif(
getenv("TASK", "") == "cuda", reason="Interaction constraints are not yet supported on the CUDA version"
)
def test_max_interactions():
def check_interactions(est, max_interactions):
feat_to_index = {feat: i for i, feat in enumerate(est.feature_name())}
tree_df = est.trees_to_dataframe()
inter_found = set()
for tree_index in tree_df["tree_index"].unique():
tree_df_per_index = tree_df[tree_df["tree_index"] == tree_index]
feat_used = [feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None]
feat_used = [
feat_to_index[feat] for feat in tree_df_per_index["split_feature"].unique() if feat is not None
]
add_inter = True
for inter in inter_found:
if set(feat_used) <= set(inter): # the interaction found is a subset of another interaction
if set(feat_used) <= set(inter): # the interaction found is a subset of another interaction
add_inter = False
if add_inter:
inter_found.add(tuple(sorted(feat_used)))
@@ -3565,8 +3578,7 @@ def check_interactions(est, max_interactions):
X, y = make_synthetic_regression(n_samples=400, n_features=30)
train_data = lgb.Dataset(X, label=y)
# check that limiting the number of distinct interactions to the number of trees is equivalent to no constraint
params = {'verbose': -1,
'seed': 0}
params = {"verbose": -1, "seed": 0}
est = lgb.train(params, train_data, num_boost_round=100)
pred1 = est.predict(X)

@@ -3590,7 +3602,6 @@ def check_interactions(est, max_interactions):
check_interactions(est, max_interactions)



def test_linear_trees_num_threads():
# check that number of threads does not affect result
np.random.seed(0)
Loading
Oops, something went wrong.