-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_user_shap_pipeline.py
332 lines (301 loc) · 14.1 KB
/
local_user_shap_pipeline.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import numpy as np
import torch
from utils.utils import *
from pref_shap.pref_shap import *
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import dill
sns.set()
def cumsum_thingy_2(cumsum_indices,shapley_vals):
cat_parts = []
for i in range(len(cumsum_indices)-1):
part = shapley_vals[:,cumsum_indices[i]:cumsum_indices[i+1]].sum(1,keepdim=True)
cat_parts.append(part)
p_output = torch.cat(cat_parts,dim=1)
return p_output
def process_data(u,shap_l,shap_r,y,job,case,f):
if case==2:
sum_count, features_names, do_sum, coeffs = return_feature_names(job, case=case)
# diff_abs = np.abs(x - x_prime)
winners = y * shap_r + (1 - y) * shap_l
loosers = (1 - y) * shap_r + y * shap_l
diff_abs = winners - loosers
data = cumsum_thingy_2(sum_count, diff_abs)
df = pd.DataFrame(data, columns=features_names)
df['fold'] = f
elif case==1:
sum_count, features_names, do_sum, coeffs = return_feature_names(job, case=case)
data = cumsum_thingy_2(sum_count, u)
df = pd.DataFrame(data, columns=features_names)
df['fold'] = f
return df
def return_feature_names(job,case=2):
if job in ['website_data_user','website_user_data_wl']:
if case==2:
# l1=[2, 2, 32, 29, 5, 23]
l1=[1,1, 1,1]+[1]*89
#redo top 10 featuers only
l1.insert(0,0)
l1=np.cumsum(l1).tolist()
l2 = [
'is_seasonless_N',
'is_seasonless_Y',
'is_carried_over_N',
'is_carried_over_Y',
'product_group_name_Accessories',
'product_group_name_Bags',
'product_group_name_Body care',
'product_group_name_Cleaning & Gardening',
'product_group_name_Cosmetic',
'product_group_name_Cosmetic Tools & Accessories',
'product_group_name_Eyes Cosmetics',
'product_group_name_Face Cosmetics',
'product_group_name_Fragrance',
'product_group_name_Fun',
'product_group_name_Furniture',
'product_group_name_Garment Full body',
'product_group_name_Garment Lower body',
'product_group_name_Garment Upper body',
'product_group_name_Garment and Shoe care',
'product_group_name_Interior decorations',
'product_group_name_Interior textile',
'product_group_name_Items',
'product_group_name_Kitchen products',
'product_group_name_Lighting',
'product_group_name_Lips Cosmetics',
'product_group_name_Nails Cosmetics',
'product_group_name_Nightwear',
'product_group_name_Shoes',
'product_group_name_Skin care',
'product_group_name_Socks & Tights',
'product_group_name_Stationery',
'product_group_name_Storage',
'product_group_name_Swimwear',
'product_group_name_Underwear',
'product_group_name_Underwear/nightwear',
'product_group_name_Unknown',
'graphical_appearance_name_All over pattern',
'graphical_appearance_name_Application/3D',
'graphical_appearance_name_Argyle',
'graphical_appearance_name_Chambray',
'graphical_appearance_name_Check',
'graphical_appearance_name_Colour blocking',
'graphical_appearance_name_Contrast',
'graphical_appearance_name_Denim',
'graphical_appearance_name_Dot',
'graphical_appearance_name_Embroidery',
'graphical_appearance_name_Front print',
'graphical_appearance_name_Glittering/Metallic',
'graphical_appearance_name_Jacquard',
'graphical_appearance_name_Lace',
'graphical_appearance_name_Melange',
'graphical_appearance_name_Mesh',
'graphical_appearance_name_Metallic',
'graphical_appearance_name_Mixed solid/pattern',
'graphical_appearance_name_Neps',
'graphical_appearance_name_Other pattern',
'graphical_appearance_name_Other structure',
'graphical_appearance_name_Placement print',
'graphical_appearance_name_Sequin',
'graphical_appearance_name_Slub',
'graphical_appearance_name_Solid',
'graphical_appearance_name_Stripe',
'graphical_appearance_name_Tie Dye',
'graphical_appearance_name_Transparent',
'graphical_appearance_name_Treatment',
'assortment_mix_name_Ca$h Cow',
'assortment_mix_name_Questionmark',
'assortment_mix_name_Star',
'assortment_mix_name_Unknown',
'assortment_mix_name_Unspecified',
'garment_group_name_Accessories',
'garment_group_name_Blouses',
'garment_group_name_Cosmetic',
'garment_group_name_Dressed',
'garment_group_name_Dresses Ladies',
'garment_group_name_Dresses/Skirts girls',
'garment_group_name_H&M Home',
'garment_group_name_Jersey Basic',
'garment_group_name_Jersey Fancy',
'garment_group_name_Knitwear',
'garment_group_name_Outdoor',
'garment_group_name_Shirts',
'garment_group_name_Shoes',
'garment_group_name_Shorts',
'garment_group_name_Skirts',
'garment_group_name_Socks and Tights',
'garment_group_name_Special Offers ',
'garment_group_name_Swimwear',
'garment_group_name_Trousers',
'garment_group_name_Trousers Denim',
'garment_group_name_Under-, Nightwear',
'garment_group_name_Unknown',
'garment_group_name_Woven/Jersey/Knitted mix Baby',
]
# l2= ['is_seasonless',
# 'is_carried_over',
#
# 'product_group_name',
# 'graphical_appearance_name',
# 'assortment_mix_name',
# 'garment_group_name',
# ]
else:
l1 = [1,1,1,1,1]
l1.insert(0, 0)
l1 = np.cumsum(l1).tolist()
l2 = ['year_of_birth','gender_code_1','gender_code_2','gender_code_0','gender_code_3'
]
coeffs= 10**np.linspace(-9,-3,0)
return l1, l2, True, coeffs
if job in ['tennis_data_processed','tennis_data_processed_wl']:
if case==2:
l1 = [1, 1, 1, 1, 1,1,1,1, 1,1,1]
l1.insert(0, 0)
l1 = np.cumsum(l1).tolist()
l2 = ['birth_year', 'weight_kg', 'height_cm', 'pro_age', 'handedness_Ambidextrous',
'handedness_Left-Handed',
'handedness_Right-Handed',
'handedness_unknown',
'backhand_One-Handed Backhand',
'backhand_Two-Handed Backhand',
'backhand_unknown']
else:
l1 = [1,1, 1,1,1,1]
l1.insert(0, 0)
l1 = np.cumsum(l1).tolist()
l2 = ['tourney_conditions_Indoor', 'tourney_conditions_Outdoor',
'tourney_surface_Carpet', 'tourney_surface_Clay',
'tourney_surface_Grass', 'tourney_surface_Hard']
# coeffs = [1e-4, 1e-3, 2e-3, 3e-3, 4e-3, 5e-3]
coeffs= 10**np.linspace(-9,-3,0)
return l1, l2, True, coeffs
def cumsum_thingy(cumsum_indices,shapley_vals):
cat_parts = []
for i in range(len(cumsum_indices)-1):
part = shapley_vals[cumsum_indices[i]:cumsum_indices[i+1],:].sum(0,keepdim=True)
cat_parts.append(part)
p_output = torch.cat(cat_parts,dim=0)
return p_output
def load_player(j,player):
dataset_string = f'{j}_{player}'
l_load = np.load(dataset_string + '/l_processed.npy', allow_pickle=True)
r_load = np.load(dataset_string + '/r_processed.npy', allow_pickle=True)
y_load = np.load(dataset_string + '/y.npy', allow_pickle=True)
u_load = np.load(dataset_string + '/u.npy', allow_pickle=True)
return l_load,r_load,y_load,u_load
# results = {'test_auc': best_test, 'val_auc': best_val,
# 'ls_i': best_model.kernel.lengthscale_items.detach().cpu(),
# 'ls_u': best_model.kernel.lengthscale_users.detach().cpu(),
# 'lamb': best_model.penalty.detach().cpu(),
# 'alpha': alpha.cpu(),
# 'inducing_points_i': ind_points_all[:, self.ulen:],
# 'inducing_points_u': ind_points_all[:, :self.ulen],
# }
def get_shapley_vals(x,x_prime,u,y, job,model,fold,train_params,num_matches,post_method,interventional,case):
with open( f'{job}_results/{model}/run_{fold}.pickle' , 'rb') as handle:
loaded_model = pickle.load(handle)
best_model = dill.loads(loaded_model)
ls_i = best_model['ls_i']
ls_u = best_model['ls_u']
alpha = best_model['alpha'].float()
ind_points = best_model['inducing_points_i'].float()
x_ind_l,x_ind_r = torch.chunk(ind_points,dim=1,chunks=2)
x_u = best_model['inducing_points_u']
job = train_params['dataset']
model = best_model['model'].to('cuda:0')
c= train_GP(train_params=train_params)
c.load_and_split_data()
inner_kernel=RBF_multiple_ls(d=x_ind_l.shape[1])
inner_kernel._set_lengthscale(ls_i)
inner_kernel=inner_kernel.to('cuda:0')
inner_kernel_u=RBF_multiple_ls(d=x_u.shape[1])
inner_kernel_u._set_lengthscale(ls_u)
inner_kernel_u=inner_kernel_u.to('cuda:0')
alpha=alpha.to('cuda:0')
ps = pref_shap(model=model, alpha=alpha, k=inner_kernel, X_l=x_ind_l, X_r=x_ind_r, X=c.S, k_U=inner_kernel_u, u=x_u,
X_U=c.S_u, max_S=2500, rff_mode=False, eps=1e-3, cg_max_its=10, lamb=1e-3, max_inv_row=2500, cg_bs=5,
post_method=post_method, interventional=interventional, device='cuda:0')
u = c.scaler_u.transform(u)
x,x_prime=c.scaler.transform(x),c.scaler.transform(x_prime)
u_shap = torch.from_numpy(u[:num_matches, :]).float()
shap_l, shap_r = torch.from_numpy(x[:num_matches, :]).float(), torch.from_numpy(x_prime[:num_matches, :]).float()
y = torch.from_numpy(y[:num_matches]).unsqueeze(-1)
df = process_data(u_shap,shap_l, shap_r, y, job, case, f)
Y_target, weights,Z= ps.fit(shap_l,shap_r,u_shap,case)
cooking_dict = {'Y':Y_target.cpu(), 'weights':weights.cpu(),'Z':Z.cpu(),
'n':shap_l.shape[0]}
return cooking_dict,df
def get_shapley_vals_2(cooking_dict,job,post_method,case,big_weight=1e5):
sum_count,features_names,do_sum,coeffs=return_feature_names(job,case)
outputs = construct_values(cooking_dict['Y'],cooking_dict['Z'],
cooking_dict['weights'],coeffs,post_method,big_weight=big_weight
)
big_plot=[]
for key,output in outputs.items():
if do_sum:
p_output = cumsum_thingy(sum_count,output)
else:
p_output = output
tmp = p_output.cpu().numpy().flatten()
tst= np.arange(1,len(features_names)+1).repeat(cooking_dict['n'])
plot = pd.DataFrame(np.stack([tst,tmp,np.ones_like(tst)*key],axis=1),columns=['d','shapley_vals','lambda'])
big_plot.append(plot)
plot = pd.concat(big_plot,axis=0).reset_index()
return plot,features_names
if __name__ == '__main__':
# d_imp = 2
# d=10
# palette =['r']*d_imp+ ['g']*(d-d_imp)
player_name = 'd643' #Djokovic hehe
interventional=f'local_{player_name}'
for j in ['tennis_data_processed_wl']:
if not os.path.exists(f'{interventional}_{j}'):
os.makedirs(f'{interventional}_{j}')
job=j
model='SGD_ukrr'
fold=0
case=2
train_params={
'dataset':job,
'fold':fold,
'epochs':100,
'patience':5,
'model_string':'SGD_ukrr', #krr_vanilla
'bs':1000,
'double_up': False,
'm_factor': 1.,
'seed':2,
'folds':5,
}
folds=[0]
l_load, r_load, y_load, u_load = load_player(j,player_name)
for case in [2, 1]:
abs_data_container = []
for f in folds:
if not os.path.exists(f'{interventional}_{job}/cooking_dict_{f}_{case}.pt'):
cooking_dict,df = get_shapley_vals(x=l_load,x_prime=r_load,u=u_load,y=y_load,
job=job,model=model,fold=fold,train_params=train_params,num_matches=-1,post_method='OLS',interventional=interventional,case=case)
torch.save(cooking_dict,f'{interventional}_{job}/cooking_dict_{f}_{case}.pt')
abs_data_container.append(df)
if case==2:
if not os.path.exists(f'{interventional}_{job}/data_folds.csv'):
big_df = pd.concat(abs_data_container, axis=0).reset_index(drop=True)
big_df.to_csv(f'{interventional}_{job}/data_folds.csv')
if case==1:
if not os.path.exists(f'{interventional}_{job}/data_folds_user.csv'):
big_df = pd.concat(abs_data_container, axis=0).reset_index(drop=True)
big_df.to_csv(f'{interventional}_{job}/data_folds_user.csv')
for post_method in ['lasso']:
for case in [2,1]:
big_plt = []
for f in [0]:
print(job,case)
cooking_dict =torch.load(f'{interventional}_{job}/cooking_dict_{f}_{case}.pt')
data, features_names = get_shapley_vals_2(cooking_dict, job, post_method,case,big_weight=1e5)
data['fold'] = f
big_plt.append(data)
plot = pd.concat(big_plt, axis=0).reset_index(drop=True)
plot['d'] = plot['d'].apply(lambda x: features_names[int(x - 1)])
plot.to_csv(f'{interventional}_{job}/{interventional}_{job}_{post_method}_{case}.csv')