-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2285b68
commit 094bf25
Showing
8 changed files
with
422 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
components/alibi-explain-server/alibiexplainer/tree_shap.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
import numpy as np | ||
import alibi | ||
from alibi.api.interfaces import Explanation | ||
from alibiexplainer.explainer_wrapper import ExplainerWrapper | ||
from alibiexplainer.constants import SELDON_LOGLEVEL | ||
from typing import List, Optional | ||
|
||
logging.basicConfig(level=SELDON_LOGLEVEL) | ||
|
||
|
||
class TreeShap(ExplainerWrapper): | ||
def __init__( | ||
self, | ||
explainer: Optional[alibi.explainers.TreeShap], | ||
**kwargs | ||
): | ||
if explainer is None: | ||
raise Exception("Tree Shap requires a built explainer") | ||
self.tree_shap = explainer | ||
self.kwargs = kwargs | ||
|
||
def explain(self, inputs: List) -> Explanation: | ||
arr = np.array(inputs) | ||
logging.info("Tree Shap call with %s", self.kwargs) | ||
logging.info("kernel shap data shape %s",arr.shape) | ||
shap_exp = self.tree_shap.explain(arr, **self.kwargs) | ||
return shap_exp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from alibiexplainer.tree_shap import TreeShap | ||
import kfserving | ||
import os | ||
import dill | ||
from alibi.datasets import fetch_adult | ||
import numpy as np | ||
import json | ||
ADULT_EXPLAINER_URI = "gs://seldon-models/xgboost/adult/tree_shap_py36_0.5.2" | ||
EXPLAINER_FILENAME = "explainer.dill" | ||
|
||
|
||
def test_kernel_shap(): | ||
os.environ.clear() | ||
alibi_model = os.path.join( | ||
kfserving.Storage.download(ADULT_EXPLAINER_URI), EXPLAINER_FILENAME | ||
) | ||
with open(alibi_model, "rb") as f: | ||
alibi_model = dill.load(f) | ||
tree_shap = TreeShap(alibi_model) | ||
adult = fetch_adult() | ||
X_test = adult.data[30001:, :] | ||
np.random.seed(0) | ||
explanation = tree_shap.explain(X_test[0:1].tolist()) | ||
exp_json = json.loads(explanation.to_json()) | ||
print(exp_json) |
Oops, something went wrong.