-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_user.py
More file actions
242 lines (217 loc) · 7.48 KB
/
preprocess_user.py
File metadata and controls
242 lines (217 loc) · 7.48 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
import pandas as pd
import copy
import numpy as np
import tqdm
import os
import pickle
# 'bpid',
# 'uniquesessionid',
# 'trans_date',
# 'click',
# 'view',
categorical_cols_old=[
'is_seasonless',
'is_carried_over',
'is_running_item',
'product_type_name',
'product_group_name',
'graphical_appearance_name',
'colour_name',
'assortment_mix_name',
'licence_company_name',
'section_name',
'composition',
'garment_group_name',
]
item_cols=[
'is_seasonless',
'is_carried_over',
'product_group_name',
'graphical_appearance_name',
'assortment_mix_name',
'garment_group_name',
]
u_cols =['year_of_birth','gender_code']
def extract_winners(losers,winners):
left,right=[],[]
y=[]
for w in winners:
for l in losers:
c=np.random.choice([0,1])
if c==0: #winner goes left
left.append(w)
right.append(l)
y.append(-1)
else: #winner goes right
left.append(l)
right.append(w)
y.append(1)
left_chunk=pd.concat(left,axis=1).transpose()
right_chunk=pd.concat(right,axis=1).transpose()
Y = np.array(y)
return left_chunk,right_chunk,Y
def extract_winners_wl(losers,winners):
left,right=[],[]
y=[]
for w in winners:
for l in losers:
right.append(w)
left.append(l)
y.append(1.)
left_chunk=pd.concat(left,axis=1).transpose()
right_chunk=pd.concat(right,axis=1).transpose()
Y = np.array(y)
return left_chunk,right_chunk,Y
def duelling_data(df):
sess_id = df['uniquesessionid'].tolist()
clicks = df['click'].tolist()
init_list_losers = []
init_list_winners = []
big_Y=[]
big_left=[]
big_right=[]
for i in tqdm.tqdm(range(len(sess_id) - 1)):
cur = sess_id[i]
next = sess_id[i + 1]
cl = float(clicks[i])
if cl==1:
init_list_winners.append(df.iloc[i])
else:
init_list_losers.append(df.iloc[i])
#
# init_list.append(float(clicks[i]))
if not cur == next:
if len(init_list_winners) > 1 and len(init_list_losers) > 1:
left_chunk, right_chunk, Y = extract_winners(init_list_losers, init_list_winners)
big_left.append(left_chunk)
big_right.append(right_chunk)
big_Y.append(Y)
init_list_losers = []
init_list_winners = []
left=pd.concat(big_left,axis=0)
right=pd.concat(big_right,axis=0)
final_Y=pd.Series(np.concatenate(big_Y,axis=0))
return left,right,final_Y
def duelling_data_wl(df):
sess_id = df['uniquesessionid'].tolist()
clicks = df['click'].tolist()
init_list_losers = []
init_list_winners = []
big_Y=[]
big_left=[]
big_right=[]
for i in tqdm.tqdm(range(len(sess_id) - 1)):
cur = sess_id[i]
next = sess_id[i + 1]
cl = float(clicks[i])
if cl==1:
init_list_winners.append(df.iloc[i])
else:
init_list_losers.append(df.iloc[i])
#
# init_list.append(float(clicks[i]))
if not cur == next:
if len(init_list_winners) > 1 and len(init_list_losers) > 1:
left_chunk, right_chunk, Y = extract_winners_wl(init_list_losers, init_list_winners)
big_left.append(left_chunk)
big_right.append(right_chunk)
big_Y.append(Y)
init_list_losers = []
init_list_winners = []
left=pd.concat(big_left,axis=0)
right=pd.concat(big_right,axis=0)
final_Y=pd.Series(np.concatenate(big_Y,axis=0))
return left,right,final_Y
def parse_correct_age(x):
if x is None:
return np.nan
else:
p = int(x)
if p>1950 and p<2010:
return p
else:
np.nan
def save_data(df,fn,wl=False):
if not os.path.exists(f'{fn}'):
os.makedirs(f'{fn}')
if not os.path.exists(f'{fn}/l_processed.npy'):
if wl:
l, r, y = duelling_data(df)
else:
l, r, y = duelling_data_wl(df)
l = l.drop(['article_id',
'bpid',
'uniquesessionid',
'trans_date',
'click',
'view',
'is_running_item',
'product_type_name',
'colour_name',
'colour_group_name',
'licence_company_name',
'section_name',
'division_name',
'composition',
'city'
], axis=1)
r = r.drop(['article_id',
'bpid',
'uniquesessionid',
'trans_date',
'click',
'view',
'is_running_item',
'product_type_name',
'colour_name',
'colour_group_name',
'licence_company_name',
'section_name',
'division_name',
'composition',
'city'
], axis=1)
u = pd.get_dummies(l[u_cols], columns=['gender_code']).values
l = l.drop(columns=u_cols, axis=1)
r = r.drop(columns=u_cols, axis=1)
l = l.values
r = r.values
with open(f'{fn}/u.npy', 'wb') as f:
np.save(f, u)
with open(f'{fn}/l_processed.npy', 'wb') as f:
np.save(f, l)
with open(f'{fn}/r_processed.npy', 'wb') as f:
np.save(f, r)
df = pd.read_parquet('pref_user_2.parquet')
S = df.drop_duplicates(subset=['article_id'])
S = pd.get_dummies(S[item_cols]).values
with open(f'{fn}/S.npy', 'wb') as f:
np.save(f, S)
df = pd.read_parquet('pref_user_2.parquet')
df['year_of_birth'] = df['year_of_birth'].apply(lambda x: parse_correct_age(x))
df['year_of_birth'].fillna((df['year_of_birth'].mean()), inplace=True)
df['gender_code'].fillna((3), inplace=True)
S_u = df.drop_duplicates(subset=['bpid'])
S_u = S_u[['year_of_birth', 'gender_code']]
S_u = pd.get_dummies(S_u, columns=['gender_code']).values
with open(f'{fn}/S_u.npy', 'wb') as f:
np.save(f, S_u)
with open(f'{fn}/y.npy', 'wb') as f:
np.save(f, y.values)
if __name__ == '__main__':
df = pd.read_parquet('pref_user_2.parquet')
df = df.sort_values(by=['bpid', 'uniquesessionid'])
df['year_of_birth'] = df['year_of_birth'].apply(lambda x: parse_correct_age(x))
df['year_of_birth'].fillna((df['year_of_birth'].mean()), inplace=True)
df['gender_code'].fillna(('3'), inplace=True)
# df = df.iloc[0:10000,:]
n_unique = df[item_cols].nunique().tolist()
print(n_unique)
df = pd.get_dummies(df,columns=item_cols)
# save_data(df,'website_user_data',False)
save_data(df,'website_user_data_wl',True)
# l, r, y = duelling_data(df)
# l.to_csv(f'{fn}/left.csv')
# r.to_csv(f'{fn}/right.csv')
# y.to_csv(f'{fn}/y.csv')
# cat_df = pd.get_dummies(df, columns=categorical_cols)