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

Added Option to Output Aggregated Contributions #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# C extensions
*.so


# Packages
*.egg
*.egg-info
Expand Down Expand Up @@ -41,3 +42,6 @@ output/*/index.html

# Sphinx
docs/_build

.ipynb_checkpoints
*/.ipynb_checkpoints/*
78 changes: 53 additions & 25 deletions treeinterpreter/treeinterpreter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import numpy as np
import sklearn

from collections import Counter
from sklearn.ensemble.forest import ForestClassifier, ForestRegressor
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier, _tree
from distutils.version import LooseVersion
Expand Down Expand Up @@ -38,7 +38,7 @@ def _predict_tree(model, X, joint_contribution=False):
For a given DecisionTreeRegressor, DecisionTreeClassifier,
ExtraTreeRegressor, or ExtraTreeClassifier,
returns a triple of [prediction, bias and feature_contributions], such
that prediction bias + feature_contributions.
that prediction ≈ bias + feature_contributions.
"""
leaves = model.apply(X)
paths = _get_tree_paths(model.tree_, 0)
Expand Down Expand Up @@ -109,11 +109,11 @@ def _predict_tree(model, X, joint_contribution=False):
return direct_prediction, biases, np.array(contributions)


def _predict_forest(model, X, joint_contribution=False):
def _predict_forest(model, X, joint_contribution=False, aggregated_contributions=False):
"""
For a given RandomForestRegressor, RandomForestClassifier,
ExtraTreesRegressor, or ExtraTreesClassifier returns a triple of
[prediction, bias and feature_contributions], such that prediction bias +
[prediction, bias and feature_contributions], such that prediction ≈ bias +
feature_contributions.
"""
biases = []
Expand All @@ -123,27 +123,48 @@ def _predict_forest(model, X, joint_contribution=False):

if joint_contribution:

for tree in model.estimators_:
pred, bias, contribution = _predict_tree(tree, X, joint_contribution=joint_contribution)
# If user wants the contributions outputed to be already aggregated run this section. It uses Counter from
# collections to automatically sum all contributions and divide by number of trees.
if aggregated_contributions:

total_contributions = Counter()

biases.append(bias)
contributions.append(contribution)
predictions.append(pred)


total_contributions = []

for i in range(len(X)):
contr = {}
for j, dct in enumerate(contributions):
for k in set(dct[i]).union(set(contr.keys())):
contr[k] = (contr.get(k, 0)*j + dct[i].get(k,0) ) / (j+1)
for tree in model.estimators_:
pred, bias, contribution = _predict_tree(tree, X, joint_contribution=joint_contribution)

biases.append(bias)
predictions.append(pred)
for dct in contribution:
total_contributions.update(dct)

total_contributions.append(contr)
# Total Contributions already aggregated.
total_contributions = {x: total_contributions[x]/len(X) for x in total_contributions.keys()}




for i, item in enumerate(contribution):
total_contributions[i]
sm = sum([v for v in contribution[i].values()])
else:
for tree in model.estimators_:
pred, bias, contribution = _predict_tree(tree, X, joint_contribution=joint_contribution)

biases.append(bias)
contributions.append(contribution)
predictions.append(pred)


total_contributions = []

for i in range(len(X)):
contr = {}
for j, dct in enumerate(contributions):
for k in set(dct[i]).union(set(contr.keys())):
contr[k] = (contr.get(k, 0)*j + dct[i].get(k,0) ) / (j+1)

total_contributions.append(contr)

for i, item in enumerate(contribution):
total_contributions[i]
sm = sum([v for v in contribution[i].values()])



Expand All @@ -162,9 +183,9 @@ def _predict_forest(model, X, joint_contribution=False):
np.mean(contributions, axis=0))


def predict(model, X, joint_contribution=False):
def predict(model, X, joint_contribution=False, aggregated_contributions=False):
""" Returns a triple (prediction, bias, feature_contributions), such
that prediction bias + feature_contributions.
that prediction ≈ bias + feature_contributions.
Parameters
----------
model : DecisionTreeRegressor, DecisionTreeClassifier,
Expand All @@ -179,6 +200,10 @@ def predict(model, X, joint_contribution=False):
joint_contribution : boolean
Specifies if contributions are given individually from each feature,
or jointly over them

aggregated_contributions : boolean
Specifies if contributions are the aggregated contribution of all the
data samples.

Returns
-------
Expand All @@ -194,6 +219,9 @@ def predict(model, X, joint_contribution=False):
If joint_contribution is True, then shape is array of size n_samples,
where each array element is a dict from a tuple of feature indices to
to a value denoting the contribution from that feature tuple.
If aggregated_contributions is False then nothing changes.
If aggregated_contributions is True then contributions is a dictionary
of the average contribution across all samples.
"""
# Only single out response variable supported,
if model.n_outputs_ > 1:
Expand All @@ -204,7 +232,7 @@ def predict(model, X, joint_contribution=False):
return _predict_tree(model, X, joint_contribution=joint_contribution)
elif (isinstance(model, ForestClassifier) or
isinstance(model, ForestRegressor)):
return _predict_forest(model, X, joint_contribution=joint_contribution)
return _predict_forest(model, X, joint_contribution=joint_contribution, aggregated_contributions=aggregated_contributions)
else:
raise ValueError("Wrong model type. Base learner needs to be a "
"DecisionTreeClassifier or DecisionTreeRegressor.")