/
shap_wrapper.py
153 lines (119 loc) · 4.82 KB
/
shap_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from __future__ import print_function
import shap
from aix360.algorithms.lbbe import LocalBBExplainer
from aix360.algorithms.lwbe import LocalWBExplainer
class KernelExplainer(LocalBBExplainer):
"""
This class wraps the source class `KernelExplainer <https://shap.readthedocs.io/>`_
available in the `SHAP <https://github.com/slundberg/shap>`_ library.
Additional variables or functions from the source class can also be accessed via the 'explainer'
object variable that is initialized in '__init__' function of this class.
"""
def __init__(self, *argv, **kwargs):
"""
Initialize shap kernelexplainer object.
"""
super(KernelExplainer, self).__init__(*argv, **kwargs)
self.explainer = shap.KernelExplainer(*argv, **kwargs)
def set_params(self, *argv, **kwargs):
"""
Optionally, set parameters for the explainer.
"""
pass
def explain_instance(self, *argv, **kwargs):
"""
Explain one ore more input instances.
"""
return (self.explainer.shap_values(*argv, **kwargs))
class GradientExplainer(LocalWBExplainer):
"""
This class wraps the source class `GradientExplainer <https://shap.readthedocs.io/>`_
available in the `SHAP <https://github.com/slundberg/shap>`_ library.
Additional variables or functions from the source class can also be accessed via the 'explainer'
object variable that is initialized in '__init__' function of this class.
"""
def __init__(self, *argv, **kwargs):
"""
Initialize shap kernelexplainer object.
"""
super(GradientExplainer, self).__init__(*argv, **kwargs)
self.explainer = shap.GradientExplainer(*argv, **kwargs)
def set_params(self, *argv, **kwargs):
"""
Optionally, set parameters for the explainer.
"""
pass
def explain_instance(self, *argv, **kwargs):
"""
Explain one or more input instances.
"""
return (self.explainer.shap_values(*argv, **kwargs))
class DeepExplainer(LocalWBExplainer):
"""
This class wraps the source class `DeepExplainer <https://shap.readthedocs.io/>`_
available in the `SHAP <https://github.com/slundberg/shap>`_ library.
Additional variables or functions from the source class can also be accessed via the 'explainer'
object variable that is initialized in '__init__' function of this class.
"""
def __init__(self, *argv, **kwargs):
"""
Initialize shap kernelexplainer object.
"""
super(DeepExplainer, self).__init__(*argv, **kwargs)
self.explainer = shap.DeepExplainer(*argv, **kwargs)
def set_params(self, *argv, **kwargs):
"""
Optionally, set parameters for the explainer.
"""
pass
def explain_instance(self, *argv, **kwargs):
"""
Explain one or more input instances.
"""
return (self.explainer.shap_values(*argv, **kwargs))
class TreeExplainer(LocalWBExplainer):
"""
This class wraps the source class `TreeExplainer <https://shap.readthedocs.io/>`_
available in the `SHAP <https://github.com/slundberg/shap>`_ library.
Additional variables or functions from the source class can also be accessed via the 'explainer'
object variable that is initialized in '__init__' function of this class.
"""
def __init__(self, *argv, **kwargs):
"""
Initialize shap kernelexplainer object.
"""
super(TreeExplainer, self).__init__(*argv, **kwargs)
self.explainer = shap.TreeExplainer(*argv, **kwargs)
def set_params(self, *argv, **kwargs):
"""
Optionally, set parameters for the explainer.
"""
pass
def explain_instance(self, *argv, **kwargs):
"""
Explain one or more input instances.
"""
return (self.explainer.shap_values(*argv, **kwargs))
class LinearExplainer(LocalWBExplainer):
"""
This class wraps the source class `Linearexplainer <https://shap.readthedocs.io/>`_
available in the `SHAP <https://github.com/slundberg/shap>`_ library.
Additional variables or functions from the source class can also be accessed via the 'explainer'
object variable that is initialized in '__init__' function of this class.
"""
def __init__(self, *argv, **kwargs):
"""
Initialize shap kernelexplainer object.
"""
super(LinearExplainer, self).__init__(*argv, **kwargs)
self.explainer = shap.LinearExplainer(*argv, **kwargs)
def set_params(self, *argv, **kwargs):
"""
Optionally, set parameters for the explainer.
"""
pass
def explain_instance(self, *argv, **kwargs):
"""
Explain one or more input instances.
"""
return (self.explainer.shap_values(*argv, **kwargs))