-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathexplain.py
More file actions
278 lines (253 loc) · 10.4 KB
/
Copy pathexplain.py
File metadata and controls
278 lines (253 loc) · 10.4 KB
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
# Copyright 2020 The SQLFlow Authors. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pandas as pd
import scipy
import shap
import six
import xgboost as xgb
from runtime import db, explainer
from runtime.dbapi.paiio import PaiIOConnection
def infer_data_type(feature):
if isinstance(feature, np.ndarray):
if feature.dtype == np.float32 or feature.dtype == np.float64:
return 'float32'
elif feature.dtype == np.int32 or feature.dtype == np.int64:
return 'int64'
else:
raise ValueError('Not supported data type {}'.format(
feature.dtype))
elif isinstance(feature, (np.float32, np.float64, float)):
return 'float32'
elif isinstance(feature, (np.int32, np.int64, six.integer_types)):
return 'int64'
else:
raise ValueError('Not supported data type {}'.format(type(feature)))
def xgb_shap_dataset(datasource,
select,
feature_column_names,
label_meta,
feature_metas,
is_pai,
pai_explain_table,
transform_fn=None,
feature_column_code=""):
if is_pai:
# (TODO: lhw) we may specify pai_explain_table in datasoure
# and discard the condition statement here
conn = PaiIOConnection.from_table(pai_explain_table)
stream = db.db_generator(conn, None, label_meta)
else:
conn = db.connect_with_data_source(datasource)
stream = db.db_generator(conn, select, label_meta)
selected_cols = db.selected_cols(conn, select)
if transform_fn:
feature_names = transform_fn.get_feature_column_names()
else:
feature_names = feature_column_names
xs = None
dtypes = []
sizes = []
offsets = []
i = 0
for row, label in stream():
features = db.read_features_from_row(row,
selected_cols,
feature_column_names,
feature_metas,
is_xgboost=True)
if transform_fn:
features = transform_fn(features)
flatten_features = []
for j, feature in enumerate(features):
if len(feature) == 3: # convert sparse to dense
col_indices, values, dense_shape = feature
size = int(np.prod(dense_shape))
row_indices = np.zeros(shape=[col_indices.size])
sparse_matrix = scipy.sparse.csr_matrix(
(values, (row_indices, col_indices)), shape=[1, size])
values = sparse_matrix.toarray()
else:
values = feature[0]
if isinstance(values, np.ndarray):
flatten_features.extend(values.flatten().tolist())
if i == 0:
sizes.append(values.size)
dtypes.append(infer_data_type(values))
else:
flatten_features.append(values)
if i == 0:
sizes.append(1)
dtypes.append(infer_data_type(values))
# Create the column name according to the feature number
# of each column.
#
# If the column "c" contains only 1 feature, the result
# column name would be "c" too.
#
# If the column "c" contains 3 features,
# the result column name would be "c_0", "c_1" and "c_2"
if i == 0:
offsets = np.cumsum([0] + sizes)
column_names = []
for j in six.moves.range(len(offsets) - 1):
start = offsets[j]
end = offsets[j + 1]
if end - start == 1:
column_names.append(feature_names[j])
else:
for k in six.moves.range(start, end):
column_names.append('{}_{}'.format(
feature_names[j], k))
xs = pd.DataFrame(columns=column_names)
xs.loc[i] = flatten_features
i += 1
# NOTE(typhoonzero): set dtype to the feature's actual type, or the dtype
# may be "object". Use below code to reproduce:
# import pandas as pd
# feature_column_names=["a", "b"]
# xs = pd.DataFrame(columns=feature_column_names)
# for i in range(10):
# xs.loc[i] = [int(j) for j in range(2)]
# print(xs.dtypes)
columns = xs.columns
for i, dtype in enumerate(dtypes):
for j in six.moves.range(offsets[i], offsets[i + 1]):
xs[columns[j]] = xs[columns[j]].astype(dtype)
return xs
def xgb_shap_values(x):
bst = xgb.Booster()
bst.load_model("my_model")
explainer = shap.TreeExplainer(bst)
return explainer.shap_values(x), explainer.shap_interaction_values(
x), explainer.expected_value
def explain(datasource,
select,
feature_field_meta,
feature_column_names,
label_meta,
summary_params,
explainer="TreeExplainer",
result_table="",
is_pai=False,
pai_explain_table="",
oss_dest=None,
oss_ak=None,
oss_sk=None,
oss_endpoint=None,
oss_bucket_name=None,
transform_fn=None,
feature_column_code=""):
if explainer == "XGBoostExplainer":
if result_table == "":
raise ValueError("""XGBoostExplainer must use with INTO to output
result to a table.""")
bst = xgb.Booster()
bst.load_model("my_model")
gain_map = bst.get_score(importance_type="gain")
fscore_map = bst.get_fscore()
if is_pai:
from runtime.dbapi.paiio import PaiIOConnection
conn = PaiIOConnection.from_table(result_table)
else:
conn = db.connect_with_data_source(datasource)
all_feature_keys = list(gain_map.keys())
all_feature_keys.sort()
with db.buffered_db_writer(conn, result_table,
["feature", "fscore", "gain"], 100) as w:
for fkey in all_feature_keys:
row = [fkey, fscore_map[fkey], gain_map[fkey]]
w.write(list(row))
else:
# when explainer is "" or "TreeExplainer" use SHAP by default.
shap_explain(datasource,
select,
feature_field_meta,
feature_column_names,
label_meta,
summary_params,
result_table=result_table,
is_pai=is_pai,
pai_explain_table=pai_explain_table,
oss_dest=oss_dest,
oss_ak=oss_ak,
oss_sk=oss_sk,
oss_endpoint=oss_endpoint,
oss_bucket_name=oss_bucket_name,
transform_fn=transform_fn,
feature_column_code=feature_column_code)
def shap_explain(datasource,
select,
feature_field_meta,
feature_column_names,
label_meta,
summary_params,
result_table="",
is_pai=False,
pai_explain_table="",
oss_dest=None,
oss_ak=None,
oss_sk=None,
oss_endpoint=None,
oss_bucket_name=None,
transform_fn=None,
feature_column_code=""):
x = xgb_shap_dataset(datasource,
select,
feature_column_names,
label_meta,
feature_field_meta,
is_pai,
pai_explain_table,
transform_fn=transform_fn,
feature_column_code=feature_column_code)
shap_values, shap_interaction_values, expected_value = xgb_shap_values(x)
if result_table != "":
if is_pai:
from runtime.dbapi.paiio import PaiIOConnection
conn = PaiIOConnection.from_table(result_table)
else:
conn = db.connect_with_data_source(datasource)
# TODO(typhoonzero): the shap_values is may be a
# list of shape [3, num_samples, num_features],
# use the first dimension here, should find out
# when to use the other two. When shap_values is
# not a list it can be directly used.
if isinstance(shap_values, list):
to_write = shap_values[0]
else:
to_write = shap_values
write_shap_values(to_write, conn, result_table, feature_column_names)
if summary_params.get("plot_type") == "decision":
explainer.plot_and_save(
lambda: shap.decision_plot(expected_value,
shap_interaction_values,
x,
show=False,
feature_display_range=slice(
None, -40, -1),
alpha=1), oss_dest, oss_ak, oss_sk,
oss_endpoint, oss_bucket_name)
else:
explainer.plot_and_save(
lambda: shap.summary_plot(
shap_values, x, show=False, **summary_params), oss_dest,
oss_ak, oss_sk, oss_endpoint, oss_bucket_name)
def write_shap_values(shap_values, conn, result_table, feature_column_names):
with db.buffered_db_writer(conn, result_table, feature_column_names,
100) as w:
for row in shap_values:
# NOTE(typhoonzero): assume all shap explain value are float, and
# there's no INT or other types of values yet.
row_float = [float(c) for c in row]
w.write(list(row_float))