Skip to content

Commit

Permalink
new function for test partial dependence
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelFu512 committed May 14, 2024
1 parent 219eda4 commit cd722d7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions evalml/tests/model_understanding_tests/test_partial_dependence.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections

Check warning on line 1 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L1

Added line #L1 was not covered by tests
import re
from unittest.mock import patch

Expand Down Expand Up @@ -713,13 +714,24 @@ def test_partial_dependence_more_categories_than_grid_resolution(
fraud_local,
logistic_regression_binary_pipeline,
):
def round_dict_keys(dictionary, places=3):
def round_dict_keys(dictionary, places=6):
"""Function to round all keys of a dictionary that has floats as keys."""
dictionary_rounded = {}
for key in dictionary:
dictionary_rounded[round(key, places)] = dictionary[key]
return dictionary_rounded

def check_dicts_approx_equal(part_dep_ans, part_dep_dict, rel=1e-3):
keys_part_dep_ans = part_dep_ans.keys()
keys_part_dep_dict = part_dep_dict.keys()
keys_part_dep_ans.sort()
keys_part_dep_dict.sort()

Check warning on line 728 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L724-L728

Added lines #L724 - L728 were not covered by tests

assert keys_part_dep_ans == pytest.approx(keys_part_dep_dict, rel=rel)
assert collections.Counter(list(part_dep_ans.values())) == collections.Counter(

Check warning on line 731 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L730-L731

Added lines #L730 - L731 were not covered by tests
list(part_dep_dict.values()),
)

X, y = fraud_local
X = X[:100]
y = y[:100]
Expand Down Expand Up @@ -753,7 +765,7 @@ def round_dict_keys(dictionary, places=3):
grid_resolution=round(num_cat_features / 2),
)
part_dep_dict = dict(part_dep["partial_dependence"].value_counts())
assert part_dep_ans_rounded == round_dict_keys(part_dep_dict)
check_dicts_approx_equal(part_dep_ans_rounded, round_dict_keys(part_dep_dict))

Check warning on line 768 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L768

Added line #L768 was not covered by tests

fast_part_dep = partial_dependence(
pipeline,
Expand All @@ -774,7 +786,7 @@ def round_dict_keys(dictionary, places=3):
grid_resolution=round(num_cat_features),
)
part_dep_dict = dict(part_dep["partial_dependence"].value_counts())
assert part_dep_ans_rounded == round_dict_keys(part_dep_dict)
check_dicts_approx_equal(part_dep_ans_rounded, round_dict_keys(part_dep_dict))

Check warning on line 789 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L789

Added line #L789 was not covered by tests

fast_part_dep = partial_dependence(
pipeline,
Expand All @@ -795,7 +807,7 @@ def round_dict_keys(dictionary, places=3):
grid_resolution=round(num_cat_features * 2),
)
part_dep_dict = dict(part_dep["partial_dependence"].value_counts())
assert part_dep_ans_rounded == round_dict_keys(part_dep_dict)
check_dicts_approx_equal(part_dep_ans_rounded, round_dict_keys(part_dep_dict))

Check warning on line 810 in evalml/tests/model_understanding_tests/test_partial_dependence.py

View check run for this annotation

Codecov / codecov/patch

evalml/tests/model_understanding_tests/test_partial_dependence.py#L810

Added line #L810 was not covered by tests

fast_part_dep = partial_dependence(
pipeline,
Expand Down

0 comments on commit cd722d7

Please sign in to comment.