Skip to content

Commit

Permalink
set check_additivity=False for CausalForestDML (#458)
Browse files Browse the repository at this point in the history
* set check_additivity=False for TreeExplainer
  • Loading branch information
heimengqi authored May 6, 2021
1 parent 7e74ed8 commit b53c0e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions econml/_shap.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _shap_explain_model_cate(cme_model, models, X, d_t, d_y, featurizer=None, fe
shap_outs = defaultdict(dict)
for i in range(dt):
try:
explainer = shap.Explainer(models[i], background,
explainer = shap.Explainer(models[i], masker=background,
feature_names=transformed_feature_names)
except Exception as e:
print("Final model can't be parsed, explain const_marginal_effect() instead!", repr(e))
Expand All @@ -160,7 +160,10 @@ def _shap_explain_model_cate(cme_model, models, X, d_t, d_y, featurizer=None, fe
output_names=output_names_,
input_names=input_names_,
background_samples=background_samples)
shap_out = explainer(F)
if explainer.__class__.__name__ == "Tree":
shap_out = explainer(F, check_additivity=False)
else:
shap_out = explainer(F)
if dy > 1:
for j in range(dy):
base_values = shap_out.base_values[..., j]
Expand Down Expand Up @@ -323,7 +326,7 @@ def _shap_explain_multitask_model_cate(cme_model, multitask_model_cate, X, d_t,
shap_outs = defaultdict(dict)
for j in range(dy):
try:
explainer = shap.Explainer(multitask_model_cate[j], background,
explainer = shap.Explainer(multitask_model_cate[j], masker=background,
feature_names=transformed_feature_names)
except Exception as e:
print("Final model can't be parsed, explain const_marginal_effect() instead!", repr(e))
Expand All @@ -334,7 +337,10 @@ def _shap_explain_multitask_model_cate(cme_model, multitask_model_cate, X, d_t,
input_names=input_names_,
background_samples=background_samples)

shap_out = explainer(F)
if explainer.__class__.__name__ == "Tree":
shap_out = explainer(F, check_additivity=False)
else:
shap_out = explainer(F)
if dt > 1:
for i in range(dt):
base_values = shap_out.base_values[..., i]
Expand Down
2 changes: 2 additions & 0 deletions econml/tests/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
os.path.join(subdir, path) for subdir
in _nbsubdirs for path in os.listdir(os.path.join(_nbdir, subdir)) if
path.endswith('.ipynb')]
# omit the lalonde notebook
_notebooks = [nb for nb in _notebooks if "Lalonde" not in nb]


@pytest.mark.parametrize("file", _notebooks)
Expand Down

0 comments on commit b53c0e3

Please sign in to comment.