-
Notifications
You must be signed in to change notification settings - Fork 93
/
template_dt_explainer.py
291 lines (262 loc) · 9.17 KB
/
template_dt_explainer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Decision Tree explainer which can be used to create explainer with global and local decision tree explanations."""
import json
import random
from h2oaicore.mli.oss.byor.core.explainers import CustomExplainer
from h2oaicore.mli.oss.byor.core.explanations import (
CustomExplanation,
GlobalDtExplanation,
LocalDtExplanation,
)
from h2oaicore.mli.oss.byor.core.representations import (
GlobalDtJSonFormat,
LocalDtJSonFormat,
)
from h2oaicore.mli.oss.commons import ExplainerModel
class TemplateDecisionTreeExplainer(CustomExplainer):
"""Decision tree explainer template.
Use this template to create explainer with global and local decision tree
explanations.
"""
_display_name = "Template DecisionTree explainer"
_description = (
"Template DecisionTree explainer which can be used to create explainer with "
"global and local decision tree explanations."
)
_regression = True
_binary = True
_multiclass = False
_global_explanation = True
_local_explanation = True
_explanation_types = [GlobalDtExplanation]
_keywords = [CustomExplainer.KEYWORD_TEMPLATE]
def setup(self, model: ExplainerModel, persistence, **kwargs):
CustomExplainer.setup(
self, model=model, persistence=persistence, **kwargs
)
def explain(self, X, y=None, explanations_types: list = None, **kwargs):
"""Create global explanations (local will be calculated on-demand).
Template explainer returns MOCK explanation data - replace mock data
preparation with actual computation to create real explainer.
"""
# explanations list
explanations = list()
# global explanation: pre-computed/cached
global_explanation = self._explain_global_dt()
explanations.append(global_explanation)
# local explanation: on-demand
local_explanation = self._explain_local_dt()
# associate local explanation with the global one
global_explanation.has_local = local_explanation.explanation_type()
explanations.append(local_explanation)
return explanations
def _explain_global_dt(self):
global_explanation = GlobalDtExplanation(
explainer=self,
display_name="Template Decision Tree",
display_category=CustomExplanation.DISPLAY_CAT_EXAMPLE,
)
#
# JSon explanation representation formed by multiple files
#
json_representation = GlobalDtJSonFormat(
explanation=global_explanation,
json_data=json.dumps(TemplateDecisionTreeExplainer.JSON_FORMAT_IDX),
)
# add more format files: per-feature, per-class (saved as added to format)
# (feature and class names MUST fit names from index file ^)
for clazz in TemplateDecisionTreeExplainer.MOCK_CLASSES:
json_representation.add_data(
# IMPROVE: tweak values for every class
format_data=json.dumps(
TemplateDecisionTreeExplainer.JSON_FORMAT_F_C
),
# filename must fit the name from index file ^
file_name=f"dt_{clazz}.json",
)
return global_explanation
def _explain_local_dt(self) -> LocalDtExplanation:
"""Persist local DT explanation as JSon which indicates that it will be
created on-demand. Note passing of parameters for subsequent computation.
"""
local_dt_explanation = LocalDtExplanation(
explainer=self,
display_name="Template Local DT",
display_category=GlobalDtExplanation.DISPLAY_CAT_EXAMPLE,
)
json_local_idx, _ = LocalDtJSonFormat.serialize_index_file(
classes=TemplateDecisionTreeExplainer.MOCK_CLASSES,
doc=TemplateDecisionTreeExplainer._description,
)
json_local_idx[LocalDtJSonFormat.KEY_ON_DEMAND] = True
on_demand_params: dict = dict()
on_demand_params[LocalDtJSonFormat.KEY_SYNC_ON_DEMAND] = True
json_local_idx[
LocalDtJSonFormat.KEY_ON_DEMAND_PARAMS
] = on_demand_params
local_dt_explanation.add_format(
explanation_format=LocalDtJSonFormat(
explanation=local_dt_explanation,
json_data=json.dumps(json_local_idx, indent=4),
)
)
return local_dt_explanation
def explain_local(self, X, y=None, **extra_params) -> str:
"""On-demand DT surrogate local explanation."""
dt = json.loads(
json.dumps(TemplateDecisionTreeExplainer.JSON_FORMAT_F_C)
)
TemplateDecisionTreeExplainer._set_local_dt_keys(
dt=dt,
keys_to_set=TemplateDecisionTreeExplainer._get_random_local_path(),
)
return json.dumps(dt)
@staticmethod
def _get_random_local_path() -> list:
key = "0"
keys = [key]
for _ in range(3):
key = f"{key}.{random.randint(0, 1)}"
keys.append(key)
return keys
@staticmethod
def _set_local_dt_keys(dt: dict, keys_to_set: list):
for node in dt[LocalDtJSonFormat.KEY_DATA]:
if node["key"] in keys_to_set:
node["leaf_path"] = True
#
# JSon DT mock
#
MOCK_CLASSES = ["class_A", "class_B", "class_C"]
# DT
JSON_FORMAT_IDX: dict = {
"files": {
"class_A": "dt_class_A.json",
"class_B": "dt_class_B.json",
"class_C": "dt_class_C.json",
}
}
# DT: feature-?, class-?
JSON_FORMAT_F_C: dict = {
"data": [
{
"key": "0",
"name": "LIMIT_BAL",
"parent": None,
"edge_in": None,
"edge_weight": None,
"leaf_path": False,
},
{
"key": "0.0",
"name": "LIMIT_BAL",
"parent": "0",
"edge_in": "< 144868.000 , NA",
"edge_weight": 0.517,
"leaf_path": False,
},
{
"key": "0.0.0",
"name": "BILL_AMT1",
"parent": "0.0",
"edge_in": "< 74931.500 , NA",
"edge_weight": 0.314,
"leaf_path": False,
},
{
"key": "0.0.0.0",
"name": "0.001",
"parent": "0.0.0",
"edge_in": "< 97439.500 , NA",
"edge_weight": 0.313,
"leaf_path": False,
},
{
"key": "0.0.0.1",
"name": "0.012",
"parent": "0.0.0",
"edge_in": ">= 97439.500",
"edge_weight": 0.001,
"leaf_path": False,
},
{
"key": "0.0.1",
"name": "PAY_4",
"parent": "0.0",
"edge_in": ">= 74931.500",
"edge_weight": 0.203,
"leaf_path": False,
},
{
"key": "0.0.1.0",
"name": "0.006",
"parent": "0.0.1",
"edge_in": "< -1.500",
"edge_weight": 0.023,
"leaf_path": False,
},
{
"key": "0.0.1.1",
"name": "0.003",
"parent": "0.0.1",
"edge_in": ">= -1.500 , NA",
"edge_weight": 0.181,
"leaf_path": False,
},
{
"key": "0.1",
"name": "BILL_AMT4",
"parent": "0",
"edge_in": ">= 144868.000",
"edge_weight": 0.483,
"leaf_path": False,
},
{
"key": "0.1.0",
"name": "PAY_AMT1",
"parent": "0.1",
"edge_in": "< 19.500",
"edge_weight": 0.079,
"leaf_path": False,
},
{
"key": "0.1.0.0",
"name": "0.007",
"parent": "0.1.0",
"edge_in": "< 1972.500 , NA",
"edge_weight": 0.062,
"leaf_path": False,
},
{
"key": "0.1.0.1",
"name": "0.015",
"parent": "0.1.0",
"edge_in": ">= 1972.500",
"edge_weight": 0.018,
"leaf_path": False,
},
{
"key": "0.1.1",
"name": "AGE",
"parent": "0.1",
"edge_in": ">= 19.500 , NA",
"edge_weight": 0.404,
"leaf_path": False,
},
{
"key": "0.1.1.0",
"name": "0.007",
"parent": "0.1.1",
"edge_in": "< 30.500",
"edge_weight": 0.117,
"leaf_path": False,
},
{
"key": "0.1.1.1",
"name": "0.004",
"parent": "0.1.1",
"edge_in": ">= 30.500 , NA",
"edge_weight": 0.287,
"leaf_path": False,
},
]
}