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

improve the efficiency of tree leaf contribution calculation #9

Merged
merged 1 commit into from May 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions treeinterpreter/treeinterpreter.py
Expand Up @@ -91,8 +91,10 @@ def _predict_tree(model, X, joint_contribution=False):
return direct_prediction, biases, contributions

else:

for row, leaf in enumerate(leaves):
unique_leaves = np.unique(leaves)
unique_contributions = {}

for row, leaf in enumerate(unique_leaves):
for path in paths:
if leaf == path[-1]:
break
Expand All @@ -103,8 +105,11 @@ def _predict_tree(model, X, joint_contribution=False):
contrib = values_list[path[i+1]] - \
values_list[path[i]]
contribs[feature_index[path[i]]] += contrib
contributions.append(contribs)

unique_contributions[leaf] = contribs

for row, leaf in enumerate(leaves):
contributions.append(unique_contributions[leaf])

return direct_prediction, biases, np.array(contributions)


Expand Down