qshap 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qshap/__init__.py +2 -0
- qshap/main.py +189 -0
- qshap/plot.py +170 -0
- qshap/qshap.py +139 -0
- qshap/test.py +165 -0
- qshap/utils.py +333 -0
- qshap/vis_module.py +271 -0
- qshap-0.1.0.dist-info/METADATA +73 -0
- qshap-0.1.0.dist-info/RECORD +11 -0
- qshap-0.1.0.dist-info/WHEEL +5 -0
- qshap-0.1.0.dist-info/top_level.txt +1 -0
qshap/__init__.py
ADDED
qshap/main.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
from qshap.utils import summarize_tree, simple_tree, tree_summary, weight, store_complex_root, store_complex_v_invc, xgb_formatter, divide_chunks
|
|
2
|
+
from qshap.qshap import loss_treeshap
|
|
3
|
+
|
|
4
|
+
from types import SimpleNamespace
|
|
5
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
6
|
+
|
|
7
|
+
import sklearn
|
|
8
|
+
import numpy as np
|
|
9
|
+
import xgboost
|
|
10
|
+
import shap
|
|
11
|
+
import warnings
|
|
12
|
+
import os
|
|
13
|
+
import json
|
|
14
|
+
import uuid
|
|
15
|
+
|
|
16
|
+
class gazer:
|
|
17
|
+
def __init__(self, model):
|
|
18
|
+
self.model = model
|
|
19
|
+
implemented_model = (sklearn.tree.DecisionTreeRegressor,
|
|
20
|
+
sklearn.ensemble.GradientBoostingRegressor,
|
|
21
|
+
xgboost.sklearn.XGBRegressor)
|
|
22
|
+
|
|
23
|
+
if isinstance(model, implemented_model):
|
|
24
|
+
self.explainer = shap.TreeExplainer(model)
|
|
25
|
+
if isinstance(model, (sklearn.tree.DecisionTreeRegressor,sklearn.ensemble.GradientBoostingRegressor)):
|
|
26
|
+
self.max_depth = model.max_depth
|
|
27
|
+
elif isinstance(model, xgboost.sklearn.XGBRegressor):
|
|
28
|
+
# set to default value 6 if max_depth not set by user
|
|
29
|
+
max_depth_xgb = model.get_params().get("max_depth")
|
|
30
|
+
if max_depth_xgb is not None:
|
|
31
|
+
self.max_depth = max_depth_xgb
|
|
32
|
+
else:
|
|
33
|
+
self.max_depth = 6
|
|
34
|
+
|
|
35
|
+
unique_filename = str(uuid.uuid4())
|
|
36
|
+
model_filename = f"xgb_model_{unique_filename}.json"
|
|
37
|
+
model.save_model(model_filename)
|
|
38
|
+
|
|
39
|
+
# Load the model data
|
|
40
|
+
with open(model_filename, 'r') as file:
|
|
41
|
+
model_data = json.load(file)
|
|
42
|
+
# Delete it after loading
|
|
43
|
+
os.remove(model_filename)
|
|
44
|
+
self.base_score = np.float64(model_data["learner"]["learner_model_param"]['base_score'])
|
|
45
|
+
self.xgb_res = xgb_formatter(model_data, self.max_depth)
|
|
46
|
+
del model_data
|
|
47
|
+
|
|
48
|
+
# store v_inc * c /d evaluated at complex roots
|
|
49
|
+
self.store_v_invc = store_complex_v_invc(self.max_depth * 2)
|
|
50
|
+
self.store_z = store_complex_root(self.max_depth * 2)
|
|
51
|
+
else:
|
|
52
|
+
supported_models_str = ', '.join([m.__name__ for m in implemented_model])
|
|
53
|
+
raise NotImplementedError(f"Model not supported yet. Supported models are: {supported_models_str}")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def loss(self, x, y, y_mean_ori=None):
|
|
57
|
+
"""
|
|
58
|
+
Parameters
|
|
59
|
+
-x: x
|
|
60
|
+
-y: y
|
|
61
|
+
-y_mean_ori: mean of the original
|
|
62
|
+
"""
|
|
63
|
+
max_depth = self.max_depth
|
|
64
|
+
model = self.model
|
|
65
|
+
store_v_invc = self.store_v_invc
|
|
66
|
+
store_z = self.store_z
|
|
67
|
+
explainer = self.explainer
|
|
68
|
+
|
|
69
|
+
if y_mean_ori is None:
|
|
70
|
+
y_mean_ori = np.mean(y)
|
|
71
|
+
|
|
72
|
+
if isinstance(model, sklearn.tree.DecisionTreeRegressor):
|
|
73
|
+
summary_tree = summarize_tree(model.tree_)
|
|
74
|
+
loss = loss_treeshap(x, y, summary_tree, store_v_invc, store_z, explainer)
|
|
75
|
+
|
|
76
|
+
# GBM
|
|
77
|
+
elif isinstance(model, sklearn.ensemble.GradientBoostingRegressor):
|
|
78
|
+
ensemble_tree = model.estimators_
|
|
79
|
+
num_tree = len(model)
|
|
80
|
+
staged_predict = list(model.staged_predict(x))
|
|
81
|
+
# learning_rate
|
|
82
|
+
alpha = model.learning_rate
|
|
83
|
+
|
|
84
|
+
loss = np.zeros_like(x, dtype=np.float64)
|
|
85
|
+
|
|
86
|
+
for i in range(num_tree):
|
|
87
|
+
if i==0:
|
|
88
|
+
res = y - y_mean_ori
|
|
89
|
+
else:
|
|
90
|
+
res = y - staged_predict[i-1]
|
|
91
|
+
|
|
92
|
+
summary_tree = summarize_tree(ensemble_tree[i, 0].tree_)
|
|
93
|
+
explainer = shap.TreeExplainer(ensemble_tree[i, 0])
|
|
94
|
+
loss += loss_treeshap(x, res, summary_tree, store_v_invc, store_z, explainer, alpha)
|
|
95
|
+
|
|
96
|
+
# XGBOOST
|
|
97
|
+
elif isinstance(model, xgboost.sklearn.XGBRegressor):
|
|
98
|
+
|
|
99
|
+
xgb_booster = model.get_booster()
|
|
100
|
+
xgb_res = self.xgb_res
|
|
101
|
+
num_tree = len(xgb_res)
|
|
102
|
+
|
|
103
|
+
warnings.filterwarnings("ignore", module="xgb")
|
|
104
|
+
|
|
105
|
+
loss = np.zeros_like(x, dtype=np.float64)
|
|
106
|
+
|
|
107
|
+
for i in range(num_tree):
|
|
108
|
+
# get summary_tree first
|
|
109
|
+
if i==0:
|
|
110
|
+
res = y - self.base_score
|
|
111
|
+
else:
|
|
112
|
+
res = y - model.predict(x, iteration_range=(0, i))
|
|
113
|
+
|
|
114
|
+
summary_tree = summarize_tree(xgb_res[i])
|
|
115
|
+
explainer = shap.TreeExplainer(xgb_booster[i])
|
|
116
|
+
|
|
117
|
+
# learning rate is different
|
|
118
|
+
loss += loss_treeshap(x, res, summary_tree, store_v_invc, store_z, explainer, 1)
|
|
119
|
+
|
|
120
|
+
return loss
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def rsq(self, x, y, loss_out=False, ncore=1, nfrac=None):
|
|
125
|
+
"""
|
|
126
|
+
Parameters
|
|
127
|
+
-x: the original x
|
|
128
|
+
-y: the original y
|
|
129
|
+
-loss_out: output loss or not
|
|
130
|
+
-nfrac: fraction of samples to sample from, by default use all samples
|
|
131
|
+
-ncore: number of cores to use, with default value 1. It will NOT be beneficial for small datasets and shallow depth.
|
|
132
|
+
|
|
133
|
+
Return
|
|
134
|
+
Shapley R-squared
|
|
135
|
+
"""
|
|
136
|
+
if nfrac is not None:
|
|
137
|
+
if nfrac <= 0 or nfrac >= 1:
|
|
138
|
+
raise ValueError("Sample fraction (nfrac) must be between (0, 1), use none for no sampling.")
|
|
139
|
+
sample_size = int(len(x) * nfrac)
|
|
140
|
+
sample_ind = np.random.choice(len(x), sample_size, replace=False)
|
|
141
|
+
x = x[sample_ind]
|
|
142
|
+
y = y[sample_ind]
|
|
143
|
+
|
|
144
|
+
max_core = os.cpu_count()
|
|
145
|
+
if ncore == -1:
|
|
146
|
+
ncore = os.cpu_count()
|
|
147
|
+
ncore = min(max_core, ncore)
|
|
148
|
+
|
|
149
|
+
explainer = self.explainer
|
|
150
|
+
y_mean_ori = np.mean(y)
|
|
151
|
+
sst = np.sum((y - y_mean_ori) ** 2)
|
|
152
|
+
|
|
153
|
+
if ncore==1:
|
|
154
|
+
loss = self.loss(x, y, y_mean_ori=y_mean_ori)
|
|
155
|
+
else:
|
|
156
|
+
x_chunks = divide_chunks(x, ncore)
|
|
157
|
+
y_chunks = divide_chunks(y, ncore)
|
|
158
|
+
|
|
159
|
+
with ProcessPoolExecutor(max_workers=ncore) as executor:
|
|
160
|
+
# Submit all chunks for processing
|
|
161
|
+
futures = [executor.submit(self.loss, x_chunks[i], y_chunks[i], y_mean_ori) for i in range(ncore)]
|
|
162
|
+
|
|
163
|
+
# Wait for all futures to complete and collect results
|
|
164
|
+
results = [future.result() for future in futures]
|
|
165
|
+
|
|
166
|
+
loss = np.concatenate(results)
|
|
167
|
+
|
|
168
|
+
rsq = 0 - np.sum(loss, axis=0)/sst
|
|
169
|
+
|
|
170
|
+
if loss_out:
|
|
171
|
+
return SimpleNamespace(rsq=rsq, loss=loss)
|
|
172
|
+
else:
|
|
173
|
+
return rsq
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def gcorr(rsq_res):
|
|
177
|
+
"""
|
|
178
|
+
Parameters
|
|
179
|
+
-rsq_res: the rsq result from calling gazer.rsq
|
|
180
|
+
|
|
181
|
+
Return
|
|
182
|
+
Generalized correlation (Square root of Shapley R-squared)
|
|
183
|
+
"""
|
|
184
|
+
res = np.sqrt(rsq_res)
|
|
185
|
+
return rsq_res
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
qshap/plot.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import ipywidgets as widgets
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
from ipywidgets import BoundedIntText, Layout, interactive_output, VBox, HBox
|
|
5
|
+
from IPython.display import display, clear_output
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def vis_rsq(x, color_map_name="Blues", horizontal=False, model_rsq=True, max_feature=10, title="Shapley R²", xtitle="Feature index", ytitle="R²", decimal=3, save_name=None):
|
|
9
|
+
"""
|
|
10
|
+
Visualize Shapley R²
|
|
11
|
+
|
|
12
|
+
Parameters:
|
|
13
|
+
-x: 1 dim array, but we recommend array from Shapley rsq OvO ...
|
|
14
|
+
-color_map_name: Color map allows to you take a variety color map from matplotlib to customize your visualization color.
|
|
15
|
+
You can try to use "Pastel1", "Pasterl2", "PuBu", "Reds", "Greens" ...
|
|
16
|
+
-horizontal: horizontal plot or not
|
|
17
|
+
-model_rsq: display model rsq or not
|
|
18
|
+
-max_feature: maximum number of features to show
|
|
19
|
+
-xtitle: xtitle, note that it's reversed for horizontal
|
|
20
|
+
-ytitle: ytitle, note that it's reversed for horizontal
|
|
21
|
+
-title: plot title
|
|
22
|
+
-decimal: decimals to show
|
|
23
|
+
-save_name: the name of the file if you want to save. None as default without saving.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
# Calculate the sum of x
|
|
27
|
+
x_sum = np.sum(x)
|
|
28
|
+
x_len = np.size(x)
|
|
29
|
+
show_len = min(x_len, max_feature)
|
|
30
|
+
|
|
31
|
+
# Sort x and keep track of the indices
|
|
32
|
+
indices = np.argsort(x)[::-1] # Indices of sorted elements (in descending order)
|
|
33
|
+
|
|
34
|
+
sorted_x = x[indices] # Sorted x
|
|
35
|
+
indices = indices[:show_len]
|
|
36
|
+
sorted_x = sorted_x[:show_len]
|
|
37
|
+
|
|
38
|
+
# Creating a color map based on the sorted values
|
|
39
|
+
# Normalize the sorted_x values to the range [0, 1]
|
|
40
|
+
normalized_x = (sorted_x - min(sorted_x)) / (max(sorted_x) - min(sorted_x))
|
|
41
|
+
|
|
42
|
+
# Get the colormap
|
|
43
|
+
cmap = plt.get_cmap(color_map_name)
|
|
44
|
+
|
|
45
|
+
# Generate colors from the colormap
|
|
46
|
+
colors = cmap(normalized_x)
|
|
47
|
+
|
|
48
|
+
# Define an offset for the text to ensure it doesn't touch the bars
|
|
49
|
+
text_offset = max(sorted_x) * 0.02 # 2% of the max value as the offset
|
|
50
|
+
|
|
51
|
+
if not horizontal:
|
|
52
|
+
# Create the bar chart
|
|
53
|
+
bars = plt.bar(range(len(sorted_x)), sorted_x, color=colors)
|
|
54
|
+
|
|
55
|
+
# Label the x-ticks with the original indices
|
|
56
|
+
plt.xticks(range(len(sorted_x)), indices)
|
|
57
|
+
|
|
58
|
+
# Adding the text on top of the bars
|
|
59
|
+
for bar in bars:
|
|
60
|
+
height = bar.get_height() # Get the height of the bar
|
|
61
|
+
# Place the text at the top of the bar, slightly above
|
|
62
|
+
plt.text(bar.get_x() + bar.get_width() / 2, height + text_offset, f'{height:.{decimal}f}', ha='center', va='bottom')
|
|
63
|
+
|
|
64
|
+
plt.ylim(0, max(sorted_x) * 1.1)
|
|
65
|
+
|
|
66
|
+
if model_rsq:
|
|
67
|
+
# Add text for the sum of values
|
|
68
|
+
plt.text(len(sorted_x) - 1, max(sorted_x), f'Model R²: {round(x_sum, 3)}',
|
|
69
|
+
horizontalalignment='right', verticalalignment='top', fontsize=12)
|
|
70
|
+
|
|
71
|
+
plt.xlabel(xtitle)
|
|
72
|
+
plt.ylabel(ytitle)
|
|
73
|
+
|
|
74
|
+
else:
|
|
75
|
+
bars = plt.barh(range(len(sorted_x)), sorted_x, color=colors)
|
|
76
|
+
|
|
77
|
+
# Label the y-ticks with the original indices
|
|
78
|
+
plt.yticks(range(len(sorted_x)), indices)
|
|
79
|
+
plt.gca().invert_yaxis()
|
|
80
|
+
|
|
81
|
+
# Adding the text beside the bars
|
|
82
|
+
for bar in bars:
|
|
83
|
+
width = bar.get_width() # Get the width of the bar (since it's horizontal)
|
|
84
|
+
# Place the text to the right of the bar, slightly beyond its end
|
|
85
|
+
plt.text(width + text_offset, bar.get_y() + bar.get_height() / 2, f'{width:.{decimal}f}',
|
|
86
|
+
ha='left', va='center')
|
|
87
|
+
|
|
88
|
+
plt.xlim(0, max(sorted_x) * 1.2)
|
|
89
|
+
|
|
90
|
+
if model_rsq:
|
|
91
|
+
plt.text(max(sorted_x), len(sorted_x) - 1,f'Model R²: {round(x_sum, 3)}',
|
|
92
|
+
horizontalalignment='right', verticalalignment='bottom', fontsize=12)
|
|
93
|
+
|
|
94
|
+
plt.xlabel(ytitle)
|
|
95
|
+
plt.ylabel(xtitle)
|
|
96
|
+
|
|
97
|
+
# Add title
|
|
98
|
+
plt.title(title)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
if save_name is not None:
|
|
102
|
+
name = save_name + ".pdf"
|
|
103
|
+
plt.savefig(name, bbox_inches='tight')
|
|
104
|
+
|
|
105
|
+
plt.show()
|
|
106
|
+
|
|
107
|
+
# vis_rsq(rsq_res)
|
|
108
|
+
# # Change color
|
|
109
|
+
# vis_rsq(rsq_res, color_map_name="Pastel2")
|
|
110
|
+
# # Give a horizontal plot, hide model rsq, change the number of features to show
|
|
111
|
+
# vis_rsq(rsq_res, color_map_name="PuBu", horizontal=True, model_rsq=False, max_feature=15, save_name="rsq_eg")
|
|
112
|
+
|
|
113
|
+
def vis_loss(loss, save_ind=None, save_prefix="Treeshap loss sample", title="Treeshap Loss", color_map_name="Blues", model_rsq=False, decimal=0, xtitle="Feature Index", ytitle="Loss"):
|
|
114
|
+
"""
|
|
115
|
+
Visualize the loss function for each sample
|
|
116
|
+
|
|
117
|
+
Parameters:
|
|
118
|
+
-loss: multidimensonal loss matrix. usually n*p
|
|
119
|
+
-save_ind: index of sample you want to save, default None.
|
|
120
|
+
-save_prefix: prefix if you want to save
|
|
121
|
+
other parameters inherite from vis_rsq
|
|
122
|
+
"""
|
|
123
|
+
def sample_loss(i):
|
|
124
|
+
plt.cla() # Clear the current axes
|
|
125
|
+
if 0 <= i < loss.shape[0]: # Check if i is within the valid range
|
|
126
|
+
vis_rsq(loss[i], title=title, color_map_name=color_map_name, model_rsq=model_rsq, decimal=decimal, xtitle=xtitle, ytitle=ytitle)
|
|
127
|
+
|
|
128
|
+
# # Slider for quick navigation
|
|
129
|
+
# i_slider = widgets.IntSlider(
|
|
130
|
+
# value=0,
|
|
131
|
+
# min=0,
|
|
132
|
+
# max=x.shape[1]-1,
|
|
133
|
+
# step=1,
|
|
134
|
+
# description='Slider:',
|
|
135
|
+
# continuous_update=False
|
|
136
|
+
# )
|
|
137
|
+
|
|
138
|
+
# Text input for precise entry
|
|
139
|
+
i_text = widgets.BoundedIntText(
|
|
140
|
+
value=0,
|
|
141
|
+
min=0,
|
|
142
|
+
max=loss.shape[0]-1,
|
|
143
|
+
step=1,
|
|
144
|
+
description='Sample Index',
|
|
145
|
+
style={'description_width': 'initial'}, # This ensures the description is not cut off
|
|
146
|
+
layout=Layout(width='150px'),
|
|
147
|
+
continuous_update=True
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
# Link the slider and text input to keep them in sync
|
|
151
|
+
# widgets.jslink((i_slider, 'value'), (i_text, 'value'))
|
|
152
|
+
|
|
153
|
+
# # Display both the slider and the text input
|
|
154
|
+
# ui = widgets.VBox([i_slider, i_text])
|
|
155
|
+
ui = widgets.VBox([i_text])
|
|
156
|
+
|
|
157
|
+
# Use interactive_output to update the plot, linking both controls
|
|
158
|
+
out = widgets.interactive_output(sample_loss, {'i': i_text})
|
|
159
|
+
|
|
160
|
+
if save_ind is not None:
|
|
161
|
+
save_name = save_prefix + " " + str(save_ind)
|
|
162
|
+
vis_rsq(loss[save_ind], title=title, color_map_name=color_map_name, model_rsq=model_rsq, decimal=decimal, xtitle=xtitle, ytitle=ytitle,
|
|
163
|
+
save_name=save_name)
|
|
164
|
+
else:
|
|
165
|
+
display(ui, out)
|
|
166
|
+
|
|
167
|
+
# vis_loss(loss)
|
|
168
|
+
|
|
169
|
+
# # Find a lovely plot and save it, say for the 5-th sample
|
|
170
|
+
# vis_loss(loss, save_ind=10)
|
qshap/qshap.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from numba import njit
|
|
3
|
+
#import cProfile
|
|
4
|
+
from numba import complex128, int32, prange
|
|
5
|
+
from qshap.utils import inv_binom_coef, complex_v_invc_degree, store_complex_v_invc, complex_dot_v2, weight, store_complex_root
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@njit
|
|
9
|
+
def T2_sample(i, w_matrix, w_ind, init_prediction, store_v_invc, store_z, shap_value, feature_uniq):
|
|
10
|
+
## Calculate T2 for each sample
|
|
11
|
+
L, p = w_matrix.shape
|
|
12
|
+
|
|
13
|
+
for l1 in range(L):
|
|
14
|
+
for l2 in range(l1, L):
|
|
15
|
+
init_prediction_product = init_prediction[l1] * init_prediction[l2]
|
|
16
|
+
|
|
17
|
+
union_f12 = feature_uniq[(w_ind[l1, feature_uniq] + w_ind[l2, feature_uniq]) >= 1]
|
|
18
|
+
|
|
19
|
+
n12 = len(union_f12)
|
|
20
|
+
# begin to use the property of complex conjugate
|
|
21
|
+
n12_c = n12 // 2 + 1
|
|
22
|
+
|
|
23
|
+
v_invc = store_v_invc[n12, :n12_c]
|
|
24
|
+
z = store_z[n12, :n12_c]
|
|
25
|
+
|
|
26
|
+
p_z = np.zeros((n12_c), dtype=complex128)
|
|
27
|
+
tmp_p_z = np.zeros((n12_c), dtype=complex128)
|
|
28
|
+
|
|
29
|
+
for k in range(n12_c):
|
|
30
|
+
p_z[k] = np.prod(z[k] + w_matrix[l1, union_f12] * w_matrix[l2, union_f12])
|
|
31
|
+
|
|
32
|
+
# update only when feature j belongs to the union of f1 and f2
|
|
33
|
+
for j in union_f12:
|
|
34
|
+
# remove the operation for j by dividing
|
|
35
|
+
for k in range(n12_c):
|
|
36
|
+
tmp_p_z[k] = p_z[k] / (z[k] + w_matrix[l1, j] * w_matrix[l2, j])
|
|
37
|
+
|
|
38
|
+
if l1 != l2:
|
|
39
|
+
shap_value[i, j] += 2 * (w_matrix[l1, j] * w_matrix[l2, j] - 1) * init_prediction_product * complex_dot_v2(tmp_p_z, v_invc, n12)
|
|
40
|
+
else:
|
|
41
|
+
shap_value[i, j] += (w_matrix[l1, j] * w_matrix[l2, j] - 1) * init_prediction_product * complex_dot_v2(tmp_p_z, v_invc, n12)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def T2(x, summary_tree, store_v_invc, store_z, parallel = True):
|
|
45
|
+
"""
|
|
46
|
+
Calculate the second order treeshap value
|
|
47
|
+
|
|
48
|
+
Parameters:
|
|
49
|
+
-x: sample to be explained
|
|
50
|
+
-summary_tree: summary tree
|
|
51
|
+
|
|
52
|
+
Return:
|
|
53
|
+
treeshap value for the sample
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
init_prediction = summary_tree.init_prediction[summary_tree.init_prediction!=0]
|
|
57
|
+
|
|
58
|
+
shap_value = np.zeros_like(x)
|
|
59
|
+
|
|
60
|
+
for i in range(x.shape[0]):
|
|
61
|
+
xi = x[i]
|
|
62
|
+
w = weight(xi, summary_tree)
|
|
63
|
+
w_matrix = w[0]
|
|
64
|
+
w_ind = w[1]
|
|
65
|
+
|
|
66
|
+
T2_sample(i, w_matrix, w_ind, init_prediction, store_v_invc, store_z, shap_value, summary_tree.feature_uniq)
|
|
67
|
+
|
|
68
|
+
return shap_value
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def loss_treeshap(x, y, summary_tree, store_v_invc, store_z, explainer, learning_rate=1):
|
|
72
|
+
"""
|
|
73
|
+
Explain l2 loss for every sample
|
|
74
|
+
|
|
75
|
+
Parameters:
|
|
76
|
+
-x: samples
|
|
77
|
+
-y: y corresponding to x
|
|
78
|
+
-summary_tree: summary tree
|
|
79
|
+
-store_v_invc: stored v_invc/d
|
|
80
|
+
-store_z: stored complex root of unity
|
|
81
|
+
-explainer: explainer from shap
|
|
82
|
+
-learning_rate: learning_rate if it's a tree from scikit learn GBM, learning_rate for decision tree and xgboost should be 1.
|
|
83
|
+
|
|
84
|
+
Return:
|
|
85
|
+
loss treeshap for x
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
square_treeshap_x = T2(x, summary_tree, store_v_invc, store_z) * learning_rate ** 2
|
|
89
|
+
# direct call from shap
|
|
90
|
+
T0_x = explainer.shap_values(x) * learning_rate
|
|
91
|
+
res = square_treeshap_x - 2 * (y * T0_x.T).T
|
|
92
|
+
return res
|
|
93
|
+
|
|
94
|
+
# WARNING: This is the wrong way to parallelize the code! The overhead is even more severe for ensemble trees, and it only serves as a bad example to warn the successors to NOT parallelize in this way.
|
|
95
|
+
# def loss_treeshap_parallel(x, y, summary_tree, store_v_invc, store_z, explainer, learning_rate=1, ncore=-1):
|
|
96
|
+
# """
|
|
97
|
+
# Explain l2 loss for every sample
|
|
98
|
+
|
|
99
|
+
# Explain l2 loss for every sample
|
|
100
|
+
|
|
101
|
+
# Parameters:
|
|
102
|
+
# -x: samples
|
|
103
|
+
# -y: y corresponding to x
|
|
104
|
+
# -summary_tree: summary tree
|
|
105
|
+
# -store_v_invc: stored v_invc/d
|
|
106
|
+
# -store_z: stored complex root of unity
|
|
107
|
+
# -explainer: explainer from shap
|
|
108
|
+
# -learning_rate: learning_rate if it's a tree from scikit learn GBM, learning_rate for decision tree and xgboost should be 1.
|
|
109
|
+
# -ncore: number of cores to use, with default value -1 to utilize all the cores
|
|
110
|
+
|
|
111
|
+
# Return:
|
|
112
|
+
# loss treeshap for x
|
|
113
|
+
# """
|
|
114
|
+
# max_core = os.cpu_count()
|
|
115
|
+
# if ncore == -1:
|
|
116
|
+
# ncore = os.cpu_count()
|
|
117
|
+
# ncore = min(max_core, ncore)
|
|
118
|
+
|
|
119
|
+
# if ncore==1:
|
|
120
|
+
# square_treeshap_x = T2(x, summary_tree, store_v_invc, store_z) * learning_rate ** 2
|
|
121
|
+
# else:
|
|
122
|
+
# chunks = divide_chunks(x, ncore)
|
|
123
|
+
|
|
124
|
+
# # Use ProcessPoolExecutor to process each chunk in parallel
|
|
125
|
+
# with ProcessPoolExecutor(max_workers=ncore) as executor:
|
|
126
|
+
# # Submit all chunks for processing
|
|
127
|
+
# futures = [executor.submit(T2, chunk, summary_tree, store_v_invc, store_z) for chunk in chunks]
|
|
128
|
+
|
|
129
|
+
# # Wait for all futures to complete and collect results
|
|
130
|
+
# results = [future.result() for future in futures]
|
|
131
|
+
|
|
132
|
+
# # Combine the results back into a single array
|
|
133
|
+
# # have to compensate the learning_rate
|
|
134
|
+
# square_treeshap_x = np.concatenate(results) * learning_rate ** 2
|
|
135
|
+
|
|
136
|
+
# # direct call from shap
|
|
137
|
+
# T0_x = explainer.shap_values(x) * learning_rate
|
|
138
|
+
# res = square_treeshap_x - 2 * (y * T0_x.T).T
|
|
139
|
+
# return res
|
qshap/test.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
from qshap import gazer
|
|
2
|
+
import sklearn.ensemble
|
|
3
|
+
import numpy as np
|
|
4
|
+
import time as time
|
|
5
|
+
from sklearn.model_selection import train_test_split
|
|
6
|
+
from sklearn.tree import DecisionTreeRegressor
|
|
7
|
+
from sklearn.ensemble import GradientBoostingRegressor
|
|
8
|
+
from sklearn.preprocessing import Binarizer
|
|
9
|
+
import xgboost
|
|
10
|
+
|
|
11
|
+
from sklearn.datasets import make_regression
|
|
12
|
+
|
|
13
|
+
# Install the developer's version
|
|
14
|
+
# python -m pip install .
|
|
15
|
+
# If no priviledge
|
|
16
|
+
# sudo -H python -m pip install .
|
|
17
|
+
|
|
18
|
+
# Let's start with a toy example with 1000 samples and 1000 features
|
|
19
|
+
np.random.seed(0)
|
|
20
|
+
x, y, coefficients = make_regression(n_samples=1000, n_features=1000, n_informative=5, coef=True, random_state=0)
|
|
21
|
+
#binarizer = Binarizer(threshold=np.median(x))
|
|
22
|
+
#x_binary = binarizer.transform(x)
|
|
23
|
+
|
|
24
|
+
# model fitting
|
|
25
|
+
# scikit learn decision tree example, the initial run after import will be slower since numba is comiling
|
|
26
|
+
max_depth = 2
|
|
27
|
+
tree_regressor = DecisionTreeRegressor(max_depth=max_depth)
|
|
28
|
+
tree_fit = tree_regressor.fit(x, y)
|
|
29
|
+
|
|
30
|
+
start = time.time()
|
|
31
|
+
gazer_rsq = gazer(tree_regressor)
|
|
32
|
+
rsq_res = gazer.rsq(gazer_rsq, x, y)
|
|
33
|
+
end = time.time()
|
|
34
|
+
print("time: " + str(end - start))
|
|
35
|
+
|
|
36
|
+
# Let's check the real R^2
|
|
37
|
+
ypred = tree_regressor.predict(x)
|
|
38
|
+
sst = np.sum((y - np.mean(y)) ** 2)
|
|
39
|
+
sse = np.sum((y - ypred) ** 2)
|
|
40
|
+
model_rsq = 1 - sse/sst
|
|
41
|
+
|
|
42
|
+
print("Treeshap R^2 sum is: " + str(np.sum(rsq_res)))
|
|
43
|
+
print("Model R^2 is: " + str(model_rsq) + "\n")
|
|
44
|
+
#print("Treeshap R^2 sum and the model R^2 is equal?: " + str(round(np.sum(rsq_res), 3)==round(model_rsq, 3)) + "!!!!!")
|
|
45
|
+
|
|
46
|
+
# scikit learn gbdt example. The usage is the same.
|
|
47
|
+
# model fitting
|
|
48
|
+
max_depth = 2
|
|
49
|
+
n_estimators = 50
|
|
50
|
+
tree_regressor = GradientBoostingRegressor(n_estimators=n_estimators, max_depth=max_depth)
|
|
51
|
+
tree_fit = tree_regressor.fit(x, y)
|
|
52
|
+
|
|
53
|
+
# start to explain
|
|
54
|
+
start = time.time()
|
|
55
|
+
gazer_rsq = gazer(tree_regressor)
|
|
56
|
+
rsq_res = gazer.rsq(gazer_rsq, x, y)
|
|
57
|
+
end = time.time()
|
|
58
|
+
print("time: " + str(end - start))
|
|
59
|
+
|
|
60
|
+
# Let's check the real R^2
|
|
61
|
+
ypred = tree_regressor.predict(x)
|
|
62
|
+
sst = np.sum((y - np.mean(y)) ** 2)
|
|
63
|
+
sse = np.sum((y - ypred) ** 2)
|
|
64
|
+
model_rsq = 1 - sse/sst
|
|
65
|
+
|
|
66
|
+
print("Treeshap R^2 sum is: " + str(np.sum(rsq_res)))
|
|
67
|
+
print("Model R^2 is: " + str(model_rsq) + "\n")
|
|
68
|
+
|
|
69
|
+
# scikit learn xgboost example. The usage is the same again.
|
|
70
|
+
# model fitting
|
|
71
|
+
max_depth = 2
|
|
72
|
+
n_estimators = 50
|
|
73
|
+
tree_regressor = xgboost.XGBRegressor(n_estimators=n_estimators, max_depth=max_depth)
|
|
74
|
+
tree_regressor.fit(x, y)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# start to explain
|
|
78
|
+
start = time.time()
|
|
79
|
+
gazer_rsq = gazer(tree_regressor)
|
|
80
|
+
rsq_res = gazer.rsq(gazer_rsq, x, y)
|
|
81
|
+
end = time.time()
|
|
82
|
+
print("time: " + str(end - start))
|
|
83
|
+
|
|
84
|
+
# Let's check the real R^2
|
|
85
|
+
ypred = tree_regressor.predict(x)
|
|
86
|
+
sst = np.sum((y - np.mean(y)) ** 2)
|
|
87
|
+
sse = np.sum((y - ypred) ** 2)
|
|
88
|
+
model_rsq = 1 - sse/sst
|
|
89
|
+
|
|
90
|
+
print("Treeshap R^2 sum is: " + str(np.sum(rsq_res)))
|
|
91
|
+
print("Model R^2 is: " + str(model_rsq) + "\n")
|
|
92
|
+
|
|
93
|
+
# if you would like to use sampling
|
|
94
|
+
start = time.time()
|
|
95
|
+
gazer_rsq = gazer(tree_regressor)
|
|
96
|
+
rsq_res_frac = gazer.rsq(gazer_rsq, x, y, nfrac=0.5)
|
|
97
|
+
end = time.time()
|
|
98
|
+
print("time: " + str(end - start))
|
|
99
|
+
|
|
100
|
+
# Let's check the real R^2
|
|
101
|
+
ypred = tree_regressor.predict(x)
|
|
102
|
+
sst = np.sum((y - np.mean(y)) ** 2)
|
|
103
|
+
sse = np.sum((y - ypred) ** 2)
|
|
104
|
+
model_rsq = 1 - sse/sst
|
|
105
|
+
|
|
106
|
+
print("Treeshap R^2 sum is: " + str(np.sum(rsq_res_frac)))
|
|
107
|
+
print("Model R^2 is: " + str(model_rsq) + "\n")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# output generalized correlation (Square root of Shapley R squared)
|
|
111
|
+
gcorr_res = gazer.gcorr(rsq_res)
|
|
112
|
+
|
|
113
|
+
# extract loss example
|
|
114
|
+
|
|
115
|
+
# Or simply output both
|
|
116
|
+
rsq_res2 = gazer.rsq(gazer_rsq, x, y, loss_out=True)
|
|
117
|
+
rsq_res2.rsq
|
|
118
|
+
rsq_res2.loss
|
|
119
|
+
|
|
120
|
+
# You can extract loss only by, it decompose the loss for each sample
|
|
121
|
+
# But directly calling loss doesn't support parallel computing yet
|
|
122
|
+
loss_res = gazer.loss(gazer_rsq, x, y)
|
|
123
|
+
|
|
124
|
+
# Interstingly, you can calculate loss decomposition for arbitrarily sample (which doesn't make sense for R^2)
|
|
125
|
+
# This will be particular fast if you only want to several samples among a large dataset
|
|
126
|
+
# All you have to do is add the original mean of y
|
|
127
|
+
loss_sample1 = gazer.loss(gazer_rsq, x[0:1, :], y[0], y_mean_ori=np.mean(y))
|
|
128
|
+
loss_multiple = gazer.loss(gazer_rsq, x[0:99], y[0:99], y_mean_ori=np.mean(y))
|
|
129
|
+
# Check if the result match
|
|
130
|
+
#print(np.sum(np.abs(loss_res[0] - loss_sample1)))
|
|
131
|
+
#print(np.sum(np.abs(loss_res[0:99] - loss_multiple)))
|
|
132
|
+
|
|
133
|
+
# Visualize rsq
|
|
134
|
+
# First we can import the module
|
|
135
|
+
from qshap import vis
|
|
136
|
+
|
|
137
|
+
# # Generate feature names using list comprehension and format them
|
|
138
|
+
feature_names = np.array([f"feature{i}" for i in range(1, rsq_res.shape[0]+1)])
|
|
139
|
+
|
|
140
|
+
# # Give it a name and rotate
|
|
141
|
+
vis.rsq(rsq_res, color_map_name="Pastel2", label=feature_names, rotation=45)
|
|
142
|
+
|
|
143
|
+
# default
|
|
144
|
+
vis.rsq(rsq_res)
|
|
145
|
+
|
|
146
|
+
# plot up to a certain cutoff
|
|
147
|
+
vis.rsq(rsq_res, cutoff=0.1, model_rsq=False)
|
|
148
|
+
|
|
149
|
+
# change color
|
|
150
|
+
vis.rsq(rsq_res, color_map_name="Pastel2")
|
|
151
|
+
# Give a horizontal plot, hide model rsq, change the number of features to show, and you can always save it
|
|
152
|
+
vis.rsq(rsq_res, color_map_name="PuBu", horizontal=True, model_rsq=False, max_feature=15, save_name="rsq_eg")
|
|
153
|
+
|
|
154
|
+
# Elbow plot
|
|
155
|
+
vis.elbow(rsq_res, max_comp=15)
|
|
156
|
+
|
|
157
|
+
# Vidualize individual loss
|
|
158
|
+
# The interactive effect works with jupyter notebook
|
|
159
|
+
vis.loss(loss_res)
|
|
160
|
+
|
|
161
|
+
# Find a lovely plot for one sample and save it, say for the 10-th sample
|
|
162
|
+
vis.loss(loss_res, save_ind=10)
|
|
163
|
+
|
|
164
|
+
# Visualize generalized rsq
|
|
165
|
+
vis.gcorr(rsq_res)
|
qshap/utils.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from numba import njit
|
|
3
|
+
import time as time
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import json
|
|
6
|
+
#d_test = 9
|
|
7
|
+
|
|
8
|
+
def inv_binom_coef(d):
|
|
9
|
+
"""
|
|
10
|
+
calculate the inverse of binomial coefficients using an iterative method with symmetry.
|
|
11
|
+
|
|
12
|
+
Parameters:
|
|
13
|
+
- d: dimension
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
binom_coef(5)
|
|
17
|
+
([ 1., 5., 10., 10., 5., 1.])
|
|
18
|
+
"""
|
|
19
|
+
coef = np.zeros(d + 1)
|
|
20
|
+
coef[0] = 1
|
|
21
|
+
for i in range(1, d // 2 + 1):
|
|
22
|
+
coef[i] = coef[i - 1] * (d - i + 1) / i
|
|
23
|
+
for i in range(d // 2 + 1, d + 1):
|
|
24
|
+
coef[i] = coef[d - i]
|
|
25
|
+
return 1/coef
|
|
26
|
+
|
|
27
|
+
#inv_binom_coef(d_test)
|
|
28
|
+
|
|
29
|
+
def complex_v_invc_degree(d):
|
|
30
|
+
"""
|
|
31
|
+
Pre store v_invc: v(z)^-1 @ c / d at degree d where z are complex roots of unity
|
|
32
|
+
|
|
33
|
+
Parameters:
|
|
34
|
+
-d: degree
|
|
35
|
+
-c: the coefficients matrix
|
|
36
|
+
"""
|
|
37
|
+
omega_inv = np.exp(-2 * np.pi * 1j * np.arange(d) / d)
|
|
38
|
+
v_omega_inv = np.vander(omega_inv, increasing=True)
|
|
39
|
+
v_inv_omega_theo = v_omega_inv / d
|
|
40
|
+
res = v_inv_omega_theo @ inv_binom_coef(d-1) / d
|
|
41
|
+
return res
|
|
42
|
+
|
|
43
|
+
#print(complex_v_invc_degree(d_test))
|
|
44
|
+
|
|
45
|
+
def store_complex_v_invc(d):
|
|
46
|
+
"""
|
|
47
|
+
Pre store v_invc: v(z)^-1 @ c / d up to maximum tree depth where z are complex roots of unity
|
|
48
|
+
|
|
49
|
+
Parameters:
|
|
50
|
+
-d: max treedepth
|
|
51
|
+
"""
|
|
52
|
+
res = np.zeros((d+1, d), dtype=complex)
|
|
53
|
+
|
|
54
|
+
for i in range(1, d+1):
|
|
55
|
+
res[i, :i] = complex_v_invc_degree(i)
|
|
56
|
+
|
|
57
|
+
return res
|
|
58
|
+
|
|
59
|
+
# how to retrive the degree d_test pre_stored value
|
|
60
|
+
# print(store_complex_v_invc(20)[d_test, :d_test])
|
|
61
|
+
|
|
62
|
+
def store_complex_root(d):
|
|
63
|
+
"""
|
|
64
|
+
Prestore the complex root of unity z
|
|
65
|
+
|
|
66
|
+
Parameters:
|
|
67
|
+
-d: max treedepth
|
|
68
|
+
"""
|
|
69
|
+
res = np.zeros((d+1, d), dtype=complex)
|
|
70
|
+
|
|
71
|
+
for i in range(1, d+1):
|
|
72
|
+
res[i, :i] = np.exp(2 * np.pi * 1j * np.arange(i) / i)
|
|
73
|
+
return res
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@njit
|
|
77
|
+
def complex_dot_v2(p, v_invc, d):
|
|
78
|
+
"""
|
|
79
|
+
Return the dot product: C(z) * P(z) / d where z are the complex roots of unity, using the fact that P(w) and v_invc both:
|
|
80
|
+
except for the 0 index and possibly the last when d is odd, the rest are complex conjugate by head and tail, etc...
|
|
81
|
+
|
|
82
|
+
Parameters
|
|
83
|
+
- p: a polynomial vector evaluated at complex root of unity
|
|
84
|
+
- v_invc: pre-calculated inverse coefficients
|
|
85
|
+
- d: the original degree before cut by half.
|
|
86
|
+
"""
|
|
87
|
+
len_p = len(p)
|
|
88
|
+
res = p[0] * v_invc[0]
|
|
89
|
+
if d % 2 == 0:
|
|
90
|
+
res += 2 * np.dot(p[1:(len_p-1)], v_invc[1:(len_p-1)]) + p[-1] * v_invc[-1]
|
|
91
|
+
else:
|
|
92
|
+
res += 2 * np.dot(p[1:len_p], v_invc[1:len_p])
|
|
93
|
+
return res.real
|
|
94
|
+
|
|
95
|
+
# v1 = np.array([3, 6-1j, 5+1j, 6+1j])
|
|
96
|
+
# v2 = np.array([5, 4-1j, 6+1j, 4+1j])
|
|
97
|
+
# v3 = np.array([3, 6-1j, 5+1j])
|
|
98
|
+
# v4 = np.array([5, 4-1j, 6+1j])
|
|
99
|
+
# print(complex_dot_v2(v3, v4, len(v1)))
|
|
100
|
+
|
|
101
|
+
# v5 = np.array([3, 6-1j, 5+1j, 5-1j, 6+1j])
|
|
102
|
+
# v6 = np.array([5, 4-1j, 6+1j, 6-1j, 4+1j])
|
|
103
|
+
# v7 = np.array([3, 6-1j, 5+1j])
|
|
104
|
+
# v8 = np.array([5, 4-1j, 6+1j])
|
|
105
|
+
# print(complex_dot_v2(v7, v8, len(v5)))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@dataclass(frozen=True)
|
|
109
|
+
class simple_tree:
|
|
110
|
+
"""
|
|
111
|
+
dataclass for a simple tree, in the scikit learn format that is necessary for computation
|
|
112
|
+
|
|
113
|
+
Data:
|
|
114
|
+
children_left: left children_index
|
|
115
|
+
children_right: right children_index
|
|
116
|
+
feature: array of features splitted at each node
|
|
117
|
+
threshold: array of threshols for corresponding splitting features
|
|
118
|
+
max_depth: max_depth of the tree
|
|
119
|
+
n_node_samples: array of sample size for each node
|
|
120
|
+
value: array of values for each node, only leaf value is used, so only keep leaf value is fine
|
|
121
|
+
node_count: total number of leaves
|
|
122
|
+
"""
|
|
123
|
+
children_left: np.ndarray
|
|
124
|
+
children_right: np.ndarray
|
|
125
|
+
feature: np.ndarray
|
|
126
|
+
threshold: np.ndarray
|
|
127
|
+
max_depth: int
|
|
128
|
+
n_node_samples: np.ndarray
|
|
129
|
+
value: np.ndarray
|
|
130
|
+
node_count: int
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@dataclass(frozen=True)
|
|
134
|
+
class tree_summary:
|
|
135
|
+
"""
|
|
136
|
+
dataclass for the calculation of cd-treeshap family
|
|
137
|
+
|
|
138
|
+
Data:
|
|
139
|
+
- children_left: left children_index
|
|
140
|
+
- children_right: right children_index
|
|
141
|
+
- feature: array of features splitted at each node
|
|
142
|
+
- feature_uniq: array of uniq features
|
|
143
|
+
- threshold: array of threshols for corresponding splitting features
|
|
144
|
+
- max_depth: the max_depth of the tree
|
|
145
|
+
- sample_weight: list of sample size of parent/sample size of current node
|
|
146
|
+
- init_prediction: initial prediction from each leaf
|
|
147
|
+
- node_count: number of nodes
|
|
148
|
+
"""
|
|
149
|
+
children_left: np.ndarray
|
|
150
|
+
children_right: np.ndarray
|
|
151
|
+
feature: np.ndarray
|
|
152
|
+
feature_uniq: np.ndarray
|
|
153
|
+
threshold: np.ndarray
|
|
154
|
+
max_depth: int
|
|
155
|
+
sample_weight: np.ndarray
|
|
156
|
+
init_prediction: np.ndarray
|
|
157
|
+
node_count: int
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def summarize_tree(tree):
|
|
162
|
+
"""
|
|
163
|
+
Summarize the data needed for tree_summary. The tree object should have:
|
|
164
|
+
children_left: left children_index
|
|
165
|
+
children_right: right children_index
|
|
166
|
+
feature: array of features splitted at each node
|
|
167
|
+
threshold: array of threshols for corresponding splitting features
|
|
168
|
+
max_depth: max_depth of the tree
|
|
169
|
+
n_node_samples: array of sample size for each node
|
|
170
|
+
value: array of values for each node, only leaf value is used, so only keep leaf value is fine
|
|
171
|
+
node_count: total number of leaves
|
|
172
|
+
"""
|
|
173
|
+
sample_weight = np.ones_like(tree.threshold)
|
|
174
|
+
init_prediction = np.zeros_like(tree.threshold)
|
|
175
|
+
n = tree.n_node_samples[0]
|
|
176
|
+
|
|
177
|
+
def traversal_summarize_tree(v):
|
|
178
|
+
v_l, v_r = tree.children_left[v], tree.children_right[v]
|
|
179
|
+
n_v, n_l, n_r = tree.n_node_samples[v], tree.n_node_samples[v_l], tree.n_node_samples[v_r]
|
|
180
|
+
|
|
181
|
+
if v_l < 0: #leaf
|
|
182
|
+
init_prediction[v] = tree.value[v] * n_v/n
|
|
183
|
+
else:
|
|
184
|
+
sample_weight[v_l], sample_weight[v_r] = n_v/n_l, n_v/n_r
|
|
185
|
+
traversal_summarize_tree(v_l)
|
|
186
|
+
traversal_summarize_tree(v_r)
|
|
187
|
+
|
|
188
|
+
# travel from the root
|
|
189
|
+
traversal_summarize_tree(0)
|
|
190
|
+
|
|
191
|
+
feature_uniq = np.unique(tree.feature[tree.feature >= 0])
|
|
192
|
+
|
|
193
|
+
return tree_summary(tree.children_left, tree.children_right, tree.feature, feature_uniq, tree.threshold, tree.max_depth, sample_weight, init_prediction, tree.node_count)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@njit
|
|
197
|
+
def traversal_weight(x, v, w, children_left, children_right, feature, threshold, sample_weight, leaf_ind, w_res, w_ind, depth, met_feature):
|
|
198
|
+
"""
|
|
199
|
+
Calculate the weight in the treeSHAP.
|
|
200
|
+
|
|
201
|
+
Parameters:
|
|
202
|
+
- x: one sample to be explained
|
|
203
|
+
- v: node index
|
|
204
|
+
- w: weight vector passed to the current node, for temporary usage
|
|
205
|
+
- children_left: left children_index
|
|
206
|
+
- children_right: right children_index
|
|
207
|
+
- feature: list of features splitted at each node
|
|
208
|
+
- threshold: list of threshold for corresponding features
|
|
209
|
+
- sample_weight: a list of sample size of parent/sample size of current node
|
|
210
|
+
- leaf_ind: leaf indices
|
|
211
|
+
- w_res, L * p matrix of weights, which records the modified weight for each leaf and each feature
|
|
212
|
+
- w_ind, L * p matrix of indicator matrix, which records the met of features for each leaf
|
|
213
|
+
- depth: current depth
|
|
214
|
+
- met_feature: record all the features met to now
|
|
215
|
+
|
|
216
|
+
Update:
|
|
217
|
+
w_res
|
|
218
|
+
w_ind
|
|
219
|
+
met_feature
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
v_l, v_r = children_left[v], children_right[v]
|
|
223
|
+
|
|
224
|
+
if v_l < 0:
|
|
225
|
+
# match to the right location so the value for w_res corresponds to the same order of leaf_ind
|
|
226
|
+
ind = (leaf_ind == v)
|
|
227
|
+
#feature_tmp = met_feature[0:depth]
|
|
228
|
+
for tmp_depth in range(depth):
|
|
229
|
+
w_res[ind, met_feature[tmp_depth]] = w[tmp_depth]
|
|
230
|
+
w_ind[ind, met_feature[tmp_depth]] = 1
|
|
231
|
+
else:
|
|
232
|
+
split_feature = feature[v]
|
|
233
|
+
split_threshold = threshold[v]
|
|
234
|
+
|
|
235
|
+
former_depth = np.arange(depth)[met_feature[0:depth] == split_feature]
|
|
236
|
+
|
|
237
|
+
if len(former_depth) != 0:
|
|
238
|
+
former_depth = former_depth[-1]
|
|
239
|
+
else:
|
|
240
|
+
former_depth = depth
|
|
241
|
+
w[depth] = 1
|
|
242
|
+
|
|
243
|
+
met_feature = met_feature.copy()
|
|
244
|
+
met_feature[depth] = split_feature
|
|
245
|
+
|
|
246
|
+
w_r = w.copy()
|
|
247
|
+
|
|
248
|
+
if x[split_feature] <= split_threshold:
|
|
249
|
+
w[depth] = w[former_depth] * sample_weight[v_l]
|
|
250
|
+
w_r[depth] = 0
|
|
251
|
+
else:
|
|
252
|
+
w_r[depth] = w_r[former_depth] * sample_weight[v_r]
|
|
253
|
+
w[depth] = 0
|
|
254
|
+
|
|
255
|
+
traversal_weight(x, v_l, w, children_left, children_right, feature, threshold, sample_weight, leaf_ind, w_res, w_ind, depth+1, met_feature)
|
|
256
|
+
traversal_weight(x, v_r, w_r, children_left, children_right, feature, threshold, sample_weight, leaf_ind, w_res, w_ind, depth+1, met_feature)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def weight(x, summary_tree):
|
|
260
|
+
p = len(x)
|
|
261
|
+
d = summary_tree.max_depth
|
|
262
|
+
|
|
263
|
+
feature_uniq = summary_tree.feature_uniq
|
|
264
|
+
|
|
265
|
+
leaf_ind = np.arange(summary_tree.node_count)[summary_tree.children_left==-1]
|
|
266
|
+
|
|
267
|
+
# L * p matrix. Note that unused features are also labeled as 0 here for efficient storage.
|
|
268
|
+
w_res = np.empty((len(leaf_ind), p))
|
|
269
|
+
w_res[:, feature_uniq] = 1
|
|
270
|
+
|
|
271
|
+
w = np.empty(d)
|
|
272
|
+
|
|
273
|
+
# L * p matrix. [i, j] = 1 if the feature j is used by leaf i. [i, j] = 0 corresponds to 1 in the
|
|
274
|
+
# above matrix. This two sparse matrices together could make the weight matrix well-defined, and save the
|
|
275
|
+
# storage at the same time
|
|
276
|
+
w_ind = np.empty((len(leaf_ind), p))
|
|
277
|
+
w_ind[:, feature_uniq] = 0
|
|
278
|
+
|
|
279
|
+
met_feature = np.full(d, -1, dtype=int)
|
|
280
|
+
|
|
281
|
+
# begin traversal from root
|
|
282
|
+
traversal_weight(x, 0, w, summary_tree.children_left, summary_tree.children_right, summary_tree.feature, summary_tree.threshold, summary_tree.sample_weight, leaf_ind, w_res, w_ind, 0, met_feature)
|
|
283
|
+
return w_res, w_ind
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def xgb_formatter(model_data, max_depth):
|
|
287
|
+
"""
|
|
288
|
+
This function takes the json format of the xgboost output and transform it to a list that treeshap rsq can understand
|
|
289
|
+
|
|
290
|
+
Parameters:
|
|
291
|
+
model_data: json file
|
|
292
|
+
max_depth: the max tree depth
|
|
293
|
+
|
|
294
|
+
Examples:
|
|
295
|
+
import json
|
|
296
|
+
xgb_regressor.save_model("model.json")
|
|
297
|
+
with open('model.json', 'r') as file:
|
|
298
|
+
model_data = json.load(file)
|
|
299
|
+
xgb_tree_res = xgb_formatter(model_data, 4)
|
|
300
|
+
"""
|
|
301
|
+
trees_data = model_data["learner"]["gradient_booster"]["model"]["trees"]
|
|
302
|
+
|
|
303
|
+
xgb_tree = []
|
|
304
|
+
|
|
305
|
+
for tree in trees_data:
|
|
306
|
+
xgb_tree.append(simple_tree(np.array(tree["left_children"]),
|
|
307
|
+
np.array(tree["right_children"]),
|
|
308
|
+
np.array(tree["split_indices"]),
|
|
309
|
+
np.array(tree["split_conditions"]),
|
|
310
|
+
max_depth,
|
|
311
|
+
np.array(tree["sum_hessian"]),
|
|
312
|
+
np.array(tree["base_weights"]),
|
|
313
|
+
int(tree["tree_param"]["num_nodes"])))
|
|
314
|
+
|
|
315
|
+
return(xgb_tree)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
# Define a function to divide the dataset into chunks
|
|
320
|
+
def divide_chunks(data, n_chunks):
|
|
321
|
+
total_elements = data.shape[0]
|
|
322
|
+
chunk_size = total_elements // n_chunks
|
|
323
|
+
remainder = total_elements % n_chunks
|
|
324
|
+
|
|
325
|
+
chunks = []
|
|
326
|
+
for i in range(n_chunks):
|
|
327
|
+
start_index = i * chunk_size
|
|
328
|
+
# For the last chunk, add the remainder to it
|
|
329
|
+
end_index = start_index + chunk_size + (remainder if i == n_chunks - 1 else 0)
|
|
330
|
+
chunks.append(data[start_index:end_index])
|
|
331
|
+
|
|
332
|
+
return chunks
|
|
333
|
+
|
qshap/vis_module.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import ipywidgets as widgets
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
from ipywidgets import BoundedIntText, Layout, interactive_output, VBox, HBox
|
|
5
|
+
from IPython.display import display, clear_output
|
|
6
|
+
|
|
7
|
+
class vis:
|
|
8
|
+
|
|
9
|
+
@staticmethod
|
|
10
|
+
def rsq(x, color_map_name="Blues", horizontal=False, model_rsq=True, max_feature=10, cutoff=0, title="Shapley R²", xtitle="Feature index", ytitle="R²", rotation=0, label=None, decimal=3, save_name=None):
|
|
11
|
+
"""
|
|
12
|
+
Visualize shapley rsq
|
|
13
|
+
|
|
14
|
+
Parameters:
|
|
15
|
+
-x: 1 dim array, but we recommend array from shapley rsq OvO ...
|
|
16
|
+
-color_map_name: Color map allows to you take a variety color map from matplotlib to customize your visualization color.
|
|
17
|
+
You can try to use "Pastel1", "Pasterl2", "PuBu", "Reds", "Greens" ...
|
|
18
|
+
-horizontal: horizontal plot or not
|
|
19
|
+
-model_rsq: display model rsq or not
|
|
20
|
+
-max_feature: maximum number of features to show
|
|
21
|
+
-cutoff: only value greater than or equal to the cutoff will be displayed
|
|
22
|
+
-xtitle: xtitle, note that it's reversed for horizontal
|
|
23
|
+
-ytitle: ytitle, note that it's reversed for horizontal
|
|
24
|
+
-rotation: rotation of the tick
|
|
25
|
+
-label: label for the features
|
|
26
|
+
-title: plot title
|
|
27
|
+
-decimal: decimals to show
|
|
28
|
+
-save_name: the name of the file if you want to save. None as default without saving.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
# Calculate the sum of x
|
|
32
|
+
x_sum = np.sum(x)
|
|
33
|
+
x_len = np.size(x)
|
|
34
|
+
cutoff_feature = np.sum(x >= cutoff)
|
|
35
|
+
show_len = min(x_len, max_feature, cutoff_feature)
|
|
36
|
+
|
|
37
|
+
# Sort x and keep track of the indices
|
|
38
|
+
indices = np.argsort(x)[::-1] # Indices of sorted elements (in descending order)
|
|
39
|
+
|
|
40
|
+
sorted_x = x[indices] # Sorted x
|
|
41
|
+
indices = indices[:show_len]
|
|
42
|
+
if label is not None:
|
|
43
|
+
if len(x)!=len(label):
|
|
44
|
+
#error: the length of label and and x must match
|
|
45
|
+
raise ValueError("The length of the label and x must match")
|
|
46
|
+
else:
|
|
47
|
+
sorted_label = label[indices]
|
|
48
|
+
sorted_label = sorted_label[:show_len]
|
|
49
|
+
|
|
50
|
+
sorted_x = sorted_x[:show_len]
|
|
51
|
+
|
|
52
|
+
# Creating a color map based on the sorted values
|
|
53
|
+
# Normalize the sorted_x values to the range [0, 1]
|
|
54
|
+
normalized_x = (sorted_x - min(sorted_x)) / (max(sorted_x) - min(sorted_x))
|
|
55
|
+
|
|
56
|
+
# Get the colormap
|
|
57
|
+
cmap = plt.get_cmap(color_map_name)
|
|
58
|
+
|
|
59
|
+
# Generate colors from the colormap
|
|
60
|
+
colors = cmap(normalized_x)
|
|
61
|
+
|
|
62
|
+
# Define an offset for the text to ensure it doesn't touch the bars
|
|
63
|
+
text_offset = max(sorted_x) * 0.02 # 2% of the max value as the offset
|
|
64
|
+
|
|
65
|
+
if not horizontal:
|
|
66
|
+
# Create the bar chart
|
|
67
|
+
bars = plt.bar(range(len(sorted_x)), sorted_x, color=colors)
|
|
68
|
+
|
|
69
|
+
# Label the x-ticks with the original indices
|
|
70
|
+
if label is not None:
|
|
71
|
+
plt.xticks(range(len(sorted_x)), sorted_label, rotation=rotation)
|
|
72
|
+
else:
|
|
73
|
+
plt.xticks(range(len(sorted_x)), indices, rotation=rotation)
|
|
74
|
+
|
|
75
|
+
# Adding the text on top of the bars
|
|
76
|
+
for bar in bars:
|
|
77
|
+
height = bar.get_height() # Get the height of the bar
|
|
78
|
+
# Place the text at the top of the bar, slightly above
|
|
79
|
+
plt.text(bar.get_x() + bar.get_width() / 2, height + text_offset, f'{height:.{decimal}f}', ha='center', va='bottom')
|
|
80
|
+
|
|
81
|
+
plt.ylim(0, max(sorted_x) * 1.1)
|
|
82
|
+
|
|
83
|
+
if model_rsq:
|
|
84
|
+
# Add text for the sum of values
|
|
85
|
+
plt.text(len(sorted_x) - 1, max(sorted_x), f'Model R²: {round(x_sum, 3)}',
|
|
86
|
+
horizontalalignment='right', verticalalignment='top', fontsize=12)
|
|
87
|
+
|
|
88
|
+
plt.xlabel(xtitle)
|
|
89
|
+
plt.ylabel(ytitle)
|
|
90
|
+
|
|
91
|
+
else:
|
|
92
|
+
bars = plt.barh(range(len(sorted_x)), sorted_x, color=colors)
|
|
93
|
+
|
|
94
|
+
# Label the y-ticks with the original indices
|
|
95
|
+
if label is not None:
|
|
96
|
+
plt.yticks(range(len(sorted_x)), sorted_label, rotation=rotation)
|
|
97
|
+
else:
|
|
98
|
+
plt.yticks(range(len(sorted_x)), indices, rotation=rotation)
|
|
99
|
+
plt.gca().invert_yaxis()
|
|
100
|
+
|
|
101
|
+
# Adding the text beside the bars
|
|
102
|
+
for bar in bars:
|
|
103
|
+
width = bar.get_width() # Get the width of the bar (since it's horizontal)
|
|
104
|
+
# Place the text to the right of the bar, slightly beyond its end
|
|
105
|
+
plt.text(width + text_offset, bar.get_y() + bar.get_height() / 2, f'{width:.{decimal}f}',
|
|
106
|
+
ha='left', va='center')
|
|
107
|
+
|
|
108
|
+
plt.xlim(0, max(sorted_x) * 1.2)
|
|
109
|
+
|
|
110
|
+
if model_rsq:
|
|
111
|
+
plt.text(max(sorted_x), len(sorted_x) - 1,f'Model R²: {round(x_sum, 3)}',
|
|
112
|
+
horizontalalignment='right', verticalalignment='bottom', fontsize=12)
|
|
113
|
+
|
|
114
|
+
plt.xlabel(ytitle)
|
|
115
|
+
plt.ylabel(xtitle)
|
|
116
|
+
|
|
117
|
+
# Add title
|
|
118
|
+
plt.title(title)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if save_name is not None:
|
|
122
|
+
name = save_name + ".pdf"
|
|
123
|
+
plt.savefig(name, bbox_inches='tight')
|
|
124
|
+
|
|
125
|
+
plt.show()
|
|
126
|
+
|
|
127
|
+
plt.close()
|
|
128
|
+
|
|
129
|
+
# vis.rsq(rsq_res)
|
|
130
|
+
# # Change color
|
|
131
|
+
# vis.rsq(rsq_res, color_map_name="Pastel2")
|
|
132
|
+
|
|
133
|
+
# import numpy as np
|
|
134
|
+
|
|
135
|
+
# # Generate feature names using list comprehension and format them
|
|
136
|
+
# feature_names = np.array([f"feature{i}" for i in range(1, rsq_res.shape[0]+1)])
|
|
137
|
+
|
|
138
|
+
# # Give it a name and rotate
|
|
139
|
+
# vis.rsq(rsq_res, color_map_name="Pastel2", label=feature_names, rotation=45)
|
|
140
|
+
|
|
141
|
+
# # Give a horizontal plot, hide model rsq, change the number of features to show
|
|
142
|
+
# vis.rsq(rsq_res, color_map_name="PuBu", horizontal=True, model_rsq=False, max_feature=15, save_name="rsq_eg")
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def loss(loss, save_ind=None, save_prefix="SHAP loss sample", title="SHAP Loss", color_map_name="Blues", model_rsq=False, decimal=0, xtitle="Feature Index", ytitle="Loss"):
|
|
146
|
+
"""
|
|
147
|
+
Visualize the loss function for each sample
|
|
148
|
+
|
|
149
|
+
Parameters:
|
|
150
|
+
-loss: multidimensonal loss matrix. usually n*p
|
|
151
|
+
-save_ind: index of sample you want to save, default None.
|
|
152
|
+
-save_prefix: prefix if you want to save
|
|
153
|
+
other parameters inherite from vis_rsq
|
|
154
|
+
"""
|
|
155
|
+
def sample_loss(i):
|
|
156
|
+
plt.cla() # Clear the current axes
|
|
157
|
+
if 0 <= i < loss.shape[0]: # Check if i is within the valid range
|
|
158
|
+
vis.rsq(loss[i], title=title, color_map_name=color_map_name, model_rsq=model_rsq, decimal=decimal, xtitle=xtitle, ytitle=ytitle)
|
|
159
|
+
|
|
160
|
+
# # Slider for quick navigation
|
|
161
|
+
# i_slider = widgets.IntSlider(
|
|
162
|
+
# value=0,
|
|
163
|
+
# min=0,
|
|
164
|
+
# max=x.shape[1]-1,
|
|
165
|
+
# step=1,
|
|
166
|
+
# description='Slider:',
|
|
167
|
+
# continuous_update=False
|
|
168
|
+
# )
|
|
169
|
+
|
|
170
|
+
# Text input for precise entry
|
|
171
|
+
i_text = widgets.BoundedIntText(
|
|
172
|
+
value=0,
|
|
173
|
+
min=0,
|
|
174
|
+
max=loss.shape[0]-1,
|
|
175
|
+
step=1,
|
|
176
|
+
description='Sample Index',
|
|
177
|
+
style={'description_width': 'initial'}, # This ensures the description is not cut off
|
|
178
|
+
layout=Layout(width='150px'),
|
|
179
|
+
continuous_update=True
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Link the slider and text input to keep them in sync
|
|
183
|
+
# widgets.jslink((i_slider, 'value'), (i_text, 'value'))
|
|
184
|
+
|
|
185
|
+
# # Display both the slider and the text input
|
|
186
|
+
# ui = widgets.VBox([i_slider, i_text])
|
|
187
|
+
ui = widgets.VBox([i_text])
|
|
188
|
+
|
|
189
|
+
# Use interactive_output to update the plot, linking both controls
|
|
190
|
+
out = widgets.interactive_output(sample_loss, {'i': i_text})
|
|
191
|
+
|
|
192
|
+
if save_ind is not None:
|
|
193
|
+
save_name = save_prefix + " " + str(save_ind)
|
|
194
|
+
vis.rsq(loss[save_ind], title=title, color_map_name=color_map_name, model_rsq=model_rsq, decimal=decimal, xtitle=xtitle, ytitle=ytitle,
|
|
195
|
+
save_name=save_name)
|
|
196
|
+
else:
|
|
197
|
+
display(ui, out)
|
|
198
|
+
|
|
199
|
+
# vis.loss(loss)
|
|
200
|
+
|
|
201
|
+
# # Find a lovely plot and save it, say for the 5-th sample
|
|
202
|
+
# vis.loss(loss, save_ind=10)
|
|
203
|
+
|
|
204
|
+
@staticmethod
|
|
205
|
+
def elbow(x, xtitle="Feature Number", ytitle="Explained Variance",
|
|
206
|
+
max_comp=10, title='Explained Variance by Top Features', marker='o', linestyle='--'):
|
|
207
|
+
"""
|
|
208
|
+
Construct elbow plot for top features.
|
|
209
|
+
|
|
210
|
+
Parameters:
|
|
211
|
+
-x: Shapley R squared
|
|
212
|
+
-max_comp: maximum number of components in the plot
|
|
213
|
+
|
|
214
|
+
Return:
|
|
215
|
+
An elbow plot and the top indices in x that have the highest variance explained
|
|
216
|
+
"""
|
|
217
|
+
# Ensure max_comp is not greater than the length of x
|
|
218
|
+
max_comp = int(min(max_comp, len(x)))
|
|
219
|
+
|
|
220
|
+
# Get indices of the sorted variances in descending order
|
|
221
|
+
indices_sorted_variances = np.argsort(x)[::-1]
|
|
222
|
+
|
|
223
|
+
# Select the indices corresponding to the highest "max_comp" variances
|
|
224
|
+
selected_indices = indices_sorted_variances[:max_comp]
|
|
225
|
+
|
|
226
|
+
# Calculate cumulative explained variance for the selected components
|
|
227
|
+
sorted_variances = x[selected_indices]
|
|
228
|
+
#cumulative_explained_variance = np.cumsum(sorted_variances / np.sum(x))
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
plt.plot(range(1, max_comp + 1), sorted_variances, marker=marker, linestyle=linestyle)
|
|
232
|
+
plt.title(title)
|
|
233
|
+
plt.xlabel(xtitle)
|
|
234
|
+
plt.ylabel(ytitle)
|
|
235
|
+
|
|
236
|
+
# Setting integer ticks on the x-axis
|
|
237
|
+
plt.xticks(range(1, max_comp + 1), range(1, max_comp + 1))
|
|
238
|
+
plt.show()
|
|
239
|
+
|
|
240
|
+
plt.close()
|
|
241
|
+
|
|
242
|
+
return selected_indices
|
|
243
|
+
|
|
244
|
+
# Call the elbow_plot_indices function
|
|
245
|
+
#indices_for_max_comp = vis_elbow(rsq_res, max_comp = 15)
|
|
246
|
+
#indices_for_max_comp
|
|
247
|
+
|
|
248
|
+
@staticmethod
|
|
249
|
+
def gcorr(x, color_map_name="Blues", horizontal=False, max_feature=10, cutoff=0, title="Genalized Shapley Correlation", xtitle="Feature index", ytitle="Generalized Correlation", rotation=0, label=None, decimal=3, save_name=None):
|
|
250
|
+
"""
|
|
251
|
+
Visualize Generalized Shapley correlation
|
|
252
|
+
|
|
253
|
+
Parameters:
|
|
254
|
+
-x: 1 dim array, but we recommend array from shapley rsq OvO ...
|
|
255
|
+
-color_map_name: Color map allows to you take a variety color map from matplotlib to customize your visualization color.
|
|
256
|
+
You can try to use "Pastel1", "Pasterl2", "PuBu", "Reds", "Greens" ...
|
|
257
|
+
-horizontal: horizontal plot or not
|
|
258
|
+
-max_feature: maximum number of features to show
|
|
259
|
+
-cutoff: only value greater than or equal to the cutoff will be displayed
|
|
260
|
+
-xtitle: xtitle, note that it's reversed for horizontal
|
|
261
|
+
-ytitle: ytitle, note that it's reversed for horizontal
|
|
262
|
+
-rotation: rotation of the tick
|
|
263
|
+
-label: label for the features
|
|
264
|
+
-title: plot title
|
|
265
|
+
-decimal: decimals to show
|
|
266
|
+
-save_name: the name of the file if you want to save. None as default without saving.
|
|
267
|
+
"""
|
|
268
|
+
vis.rsq(np.sqrt(x), color_map_name=color_map_name, horizontal=horizontal, model_rsq=False, max_feature=max_feature, cutoff=cutoff, title=title, xtitle=xtitle, ytitle=ytitle, rotation=0, label=None, decimal=3, save_name=None)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: qshap
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Exact computation of shapley R-squared in polynomial time
|
|
5
|
+
Home-page: https://github.com/catstats/Q-SHAP
|
|
6
|
+
Author: Zhongli Jiang, Dabao Zhang
|
|
7
|
+
Author-email: jiang548@purdue.edu, zdb969@hs.uci.edu
|
|
8
|
+
License: GPL-2.0
|
|
9
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
10
|
+
Classifier: Operating System :: Unix
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.7
|
|
20
|
+
Requires-Dist: numpy
|
|
21
|
+
Requires-Dist: scikit-learn
|
|
22
|
+
Requires-Dist: shap
|
|
23
|
+
Requires-Dist: numba
|
|
24
|
+
Requires-Dist: ipywidgets
|
|
25
|
+
Requires-Dist: pandas
|
|
26
|
+
Requires-Dist: matplotlib
|
|
27
|
+
|
|
28
|
+
## Q-SHAP: Feature-Specific $R^2$ Values for Tree Ensembles
|
|
29
|
+
This package is used to compute feature-specific $R^2$ values, following Shapley decomposition of the total $R^2$, for tree ensembles in polynomial time.
|
|
30
|
+
|
|
31
|
+
This version only takes output from **XGBoost**, **scikit-learn Decision Tree**, and **scikit-learn GBDT**. We are working to update it for random forest in the next version. Please check **Q-SHAP Tutorial.ipynb** for more usage.
|
|
32
|
+
|
|
33
|
+
### Citation
|
|
34
|
+
```bibtex
|
|
35
|
+
@article{jiang2024feature,
|
|
36
|
+
title={Feature-Specific Coefficients of Determination in Tree Ensembles},
|
|
37
|
+
author={Jiang, Zhongli and Zhang, Dabao and Zhang, Min},
|
|
38
|
+
journal={arXiv preprint arXiv:2407.03515},
|
|
39
|
+
year={2024}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### References
|
|
45
|
+
- Jiang, Z., Zhang, D., & Zhang, M. (2024). Feature-Specific Coefficients of Determination in Tree Ensembles. arXiv preprint arXiv:2407.03515.
|
|
46
|
+
- Lundberg, Scott M., et al. "From local explanations to global understanding with explainable AI for trees." Nature machine intelligence 2.1 (2020): 56-67.
|
|
47
|
+
- Karczmarz, Adam, et al. "Improved feature importance computation for tree models based on the Banzhaf value." Uncertainty in Artificial Intelligence. PMLR, 2022.
|
|
48
|
+
- Bifet, Albert, Jesse Read, and Chao Xu. "Linear tree shap." Advances in Neural Information Processing Systems 35 (2022): 25818-25828.
|
|
49
|
+
- Chen, Tianqi, and Carlos Guestrin. "Xgboost: A scalable tree boosting system." Proceedings of the 22nd acm sigkdd international conference on knowledge discovery and data mining. 2016.
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
### Container images
|
|
54
|
+
We provide pre-built images with all necessary packages for Q-SHAP in Python 3.11, available for both Docker and Singularity:
|
|
55
|
+
|
|
56
|
+
- **Docker:**
|
|
57
|
+
You can pull the Docker image using the following command:
|
|
58
|
+
```sh
|
|
59
|
+
docker pull catstat/xai
|
|
60
|
+
```
|
|
61
|
+
- **Singularity:**
|
|
62
|
+
You can pull the Docker image using the following command:
|
|
63
|
+
```sh
|
|
64
|
+
singularity pull docker://catstat/xai:0.0
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Task List
|
|
69
|
+
|
|
70
|
+
- [ ] Task 1: Lightgbm version
|
|
71
|
+
- [ ] Task 2: Catboost version
|
|
72
|
+
|
|
73
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
qshap/__init__.py,sha256=lLb39q6xful1a9ONCWsd0vZqu5NHQhNU50vu4hb5LuA,61
|
|
2
|
+
qshap/main.py,sha256=Soh7FG1njHi4sBKuFmK0p6kKKUDh15T4IvtlDalJbHI,6784
|
|
3
|
+
qshap/plot.py,sha256=wTUYhMRrDPL1bFGuJlrr0eCh1DrnSVwOD3T6W6IInjg,6447
|
|
4
|
+
qshap/qshap.py,sha256=yOL6cz9V-rFTZlHGHXHmllMv2zzXs88fdW9wSlehaBA,5198
|
|
5
|
+
qshap/test.py,sha256=jyZh3Z9H-OGPr2VxaaKcj_Or-ApbTcsbNvlLKl9UJ5A,5221
|
|
6
|
+
qshap/utils.py,sha256=eXRzDtwoQ4fFf_Ftp_c_3r8crwoGeB8SlmRmnYtWjrg,11078
|
|
7
|
+
qshap/vis_module.py,sha256=KawzBx8jdI18XSeODzZlp2qOnwgWjXvyqJLmChsMY4M,11392
|
|
8
|
+
qshap-0.1.0.dist-info/METADATA,sha256=vXJBBPAvsixWawXwuRGWBQzvkp7MolUsy48DrrAt2Ns,2919
|
|
9
|
+
qshap-0.1.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
10
|
+
qshap-0.1.0.dist-info/top_level.txt,sha256=pi2d6FDuzcHnaepQRaPXe1OCRDFiWWCcdYH6mXnIG1E,6
|
|
11
|
+
qshap-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
qshap
|