fix zero division in compute_expectations #566
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello, @slundberg ) Thank you for writing this tool.
I have a request with a really short implementation.
I want to add new SHAP functionality to Yandex CatBoost. But due to the special structure of trees in CatBoost, there might be nodes with right and left leafs with both zero sample weights in them. As a result, after calling the
_cext.compute_expectations
function https://github.com/slundberg/shap/blob/master/shap/explainers/tree.py#L899which calls
compute_expectations
function here https://github.com/slundberg/shap/blob/master/shap/_cext.cc#L99 and finally callingcompute_expectations
function here https://github.com/slundberg/shap/blob/master/shap/tree_shap.h#L509, if(left_weight + right_weight)
equals to0
, this leads to zero division andnan
s in returned Python array of expected values in nodes of trees.In other tree-based algorithms, such as XGBClassifier and RandomForestClassifier, at least one sample must be in each leaf, so this problem does not appear. But in CatBoost trees it appears.
I just have added additional if-statement to check that
left_weight
andright_weight
are not zeroes.