-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mikemull/shap
Move SHAP stuff to separate file
- Loading branch information
Showing
4 changed files
with
70 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
import numpy as np | ||
import shap | ||
|
||
|
||
class ShapExplainer: | ||
|
||
def __init__(self, model, classification): | ||
self.model = model | ||
self.classification = classification | ||
|
||
def explain(self, x_boruta): | ||
""" | ||
The shap package has numerous variants of explainers which use different assumptions depending on the model | ||
type this function allows the user to choose explainer | ||
Returns: | ||
shap values | ||
Raise | ||
---------- | ||
ValueError: | ||
if no model type has been specified tree as default | ||
""" | ||
explainer = shap.TreeExplainer( | ||
self.model, feature_perturbation="tree_path_dependent", approximate=True | ||
) | ||
|
||
if self.classification: | ||
# for some reason shap returns values wraped in a list of length 1 | ||
shap_vals = np.array(explainer.shap_values(x_boruta)) | ||
if isinstance(shap_vals, list): | ||
class_inds = range(len(shap_vals)) | ||
shap_imp = np.zeros(shap_vals[0].shape[1]) | ||
for i, ind in enumerate(class_inds): | ||
shap_imp += np.abs(shap_vals[ind]).mean(0) | ||
shap_vals /= len(shap_vals) | ||
|
||
elif len(shap_vals.shape) == 3: | ||
shap_vals = np.abs(shap_vals).sum(axis=0) | ||
shap_vals = shap_vals.mean(axis=1) | ||
|
||
else: | ||
shap_vals = np.abs(shap_vals).mean(0) | ||
|
||
else: | ||
shap_vals = explainer.shap_values(x_boruta) | ||
shap_vals = np.abs(shap_vals).mean(0) | ||
|
||
return shap_vals |
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