Skip to content
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
23 changes: 23 additions & 0 deletions smarttree/_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def check__params(
if rank_features is not None:
_check__rank_features(rank_features)

if num_features is not None and cat_features is not None:
param_name1, param_name2 = "num_features", "cat_features"
_check__ambiguous(num_features, cat_features, param_name1, param_name2)

if num_features is not None and rank_features is not None:
param_name1, param_name2 = "num_features", "rank_features"
_check__ambiguous(num_features, list(rank_features.keys()), param_name1, param_name2)

if cat_features is not None and rank_features is not None:
param_name1, param_name2 = "cat_features", "rank_features"
_check__ambiguous(cat_features, list(rank_features.keys()), param_name1, param_name2)

if hierarchy is not None:
_check__hierarchy(hierarchy)

Expand Down Expand Up @@ -221,6 +233,17 @@ def _check__features_contain_duplicates(param_name, features):
raise ValueError(f"`{param_name}` contains duplicates.")


def _check__ambiguous(features1, features2, param_name1, param_name2):
set_features1 = {features1} if isinstance(features1, str) else set(features1)
set_features2 = {features2} if isinstance(features2, str) else set(features2)
intersection = set_features1 & set_features2
if intersection:
raise ValueError(
"Following feature names are ambiguous, they are defined in both"
f" '{param_name1}' and '{param_name2}': {intersection}."
)


def _check__hierarchy(hierarchy):
common_message = (
"`hierarchy` must be a dictionary"
Expand Down
51 changes: 51 additions & 0 deletions tests/decision_tree/base/test__check_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,57 @@ def test__check_params__features_contain_duplicates(params_to_set, expected_cont
SmartDecisionTreeClassifier(**params_to_set)


@pytest.mark.parametrize(
("n_features", "c_features", "r_features", "expected_context"),
[
("f1", "f2", None, does_not_raise()),
(
"f1",
"f1",
None,
pytest.raises(
ValueError,
match=(
"Following feature names are ambiguous, they are defined in"
" both 'num_features' and 'cat_features': {'f1'}."
),
),
),
("f1", None, {"f2": ["v2"]}, does_not_raise()),
(
"f1",
None,
{"f1": ["v1"]},
pytest.raises(
ValueError,
match=(
"Following feature names are ambiguous, they are defined in"
" both 'num_features' and 'rank_features': {'f1'}."
),
),
),
(None, "f1", {"f2": ["v2"]}, does_not_raise()),
(
None,
"f1",
{"f1": ["v1"]},
pytest.raises(
ValueError,
match=(
"Following feature names are ambiguous, they are defined in"
" both 'cat_features' and 'rank_features': {'f1'}."
),
),
),
],
)
def test__check_params__ambiguous(n_features, c_features, r_features, expected_context):
with expected_context:
SmartDecisionTreeClassifier(
num_features=n_features, cat_features=c_features, rank_features=r_features
)


@pytest.mark.parametrize(
("hierarchy", "expected_context"),
[
Expand Down
Loading