forked from federicoparroni/recsys2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
372 lines (298 loc) · 13.2 KB
/
data.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import pandas as pd
import scipy.sparse as sps
import numpy as np
import pickle
import os
import dask.dataframe as ddf
# original files
TRAIN_ORIGINAL_PATH = 'dataset/original/train.csv'
TEST_ORIGINAL_PATH = 'dataset/original/test.csv'
ITEMS_ORIGINAL_PATH = 'dataset/original/item_metadata.csv'
# full df
FULL_PATH = 'dataset/preprocessed/full.csv'
URM_PATH = ['dataset/matrices/full/', 'dataset/matrices/local/', 'dataset/matrices/small/']
DICT_ROW_PATH = ['dataset/matrices/full/dict_row.npy', 'dataset/matrices/local/dict_row.npy',
'dataset/matrices/small/dict_row.npy']
DICT_COL_PATH = ['dataset/matrices/full/dict_col.npy', 'dataset/matrices/local/dict_col.npy',
'dataset/matrices/small/dict_col.npy']
ITEMS_PATH = 'dataset/preprocessed/item_metadata.csv'
ACCOMODATIONS_1HOT_PATH = 'dataset/preprocessed/accomodations_1hot.csv'
# config file
CONFIG_FILE_PATH = 'dataset/config.pkl'
TRAIN_LEN_KEY = 'max_train_idx'
_df_train_original = None
_df_test_original = None
_df_original_items = None
_df_full = None
_df_train = {}
_df_test = {}
_target_indices = {}
_df_classification_train = {}
_df_classification_test = {}
_df_catboost_train = {}
_df_catboost_test = {}
_dataset_xgboost_train = {}
_dataset_xgboost_test = {}
_dataset_xgboost_classifier_train = {}
_dataset_xgboost_classifier_test = {}
_df_accomodations_one_hot = None
_user_prop = {}
_df_items = None
_df_items_ids = None
# URM structures
_urm = {}
_dict_row = {}
_dict_col = {}
_icm = None
_full_train_indices = None
_full_test_indices = None
# constants
SPLIT_USED = 'no_cluster'
def full_df():
global _df_full
if _df_full is None:
print('caching df_full...', flush=True)
_df_full = pd.read_csv(FULL_PATH, index_col=0)
print('Done!')
return _df_full
def refresh_full_df():
global _df_full
print('refreshing df_full...', flush=True)
_df_full = pd.read_csv(FULL_PATH, index_col=0)
print('Done!')
def original_train_df():
global _df_train_original
if _df_train_original is None:
_df_train_original = pd.read_csv(TRAIN_ORIGINAL_PATH)
return _df_train_original
def original_test_df():
global _df_test_original
if _df_test_original is None:
_df_test_original = pd.read_csv(TEST_ORIGINAL_PATH)
return _df_test_original
def train_df(mode, cluster='no_cluster'):
global _df_train
path = 'dataset/preprocessed/{}/{}/train.csv'.format(cluster, mode)
if path not in _df_train:
if mode == "full" and cluster == 'no_cluster':
print("Loading {} train_df, it will take a while..".format(mode), flush=True)
_df_train[path] = pd.read_csv(path, index_col=0)
return _df_train[path]
def test_df(mode, cluster='no_cluster'):
global _df_test
path = 'dataset/preprocessed/{}/{}/test.csv'.format(cluster, mode)
if path not in _df_test:
_df_test[path] = pd.read_csv(path, index_col=0)
return _df_test[path]
def target_indices(mode, cluster='no_cluster'):
global _target_indices
path = 'dataset/preprocessed/{}/{}/target_indices.npy'.format(
cluster, mode)
if path not in _target_indices:
_target_indices[path] = np.load(path)
return _target_indices[path]
def dataset_xgboost_train(mode, cluster='no_cluster', kind='kind1', class_weights=False):
global _dataset_xgboost_train
bp = 'dataset/preprocessed/{}/{}/xgboost/{}/'.format(cluster, mode, kind)
if not 'a' in _dataset_xgboost_train:
_dataset_xgboost_train[bp+'a'] = sps.load_npz(
os.path.join(bp, 'X_train.npz'))
_dataset_xgboost_train[bp+'b'] = pd.read_csv(
os.path.join(bp, 'y_train.csv'))['label'].to_dense()
_dataset_xgboost_train[bp+'c'] = np.load(
os.path.join(bp, 'group_train.npy'))
_dataset_xgboost_train[bp+'e'] = np.load(
os.path.join(bp, 'train_indices.npy'))
_dataset_xgboost_train[bp+'f'] = pd.read_csv(
os.path.join(bp, 'user_session_item_train.csv'))
if class_weights:
_dataset_xgboost_train[bp+'d'] = np.load(
os.path.join(bp, 'class_weights.npy'))
if class_weights:
return _dataset_xgboost_train[bp+'a'], \
_dataset_xgboost_train[bp+'b'], \
_dataset_xgboost_train[bp+'c'], \
_dataset_xgboost_train[bp+'e'], \
_dataset_xgboost_train[bp+'f'], \
_dataset_xgboost_train[bp + 'd']
else:
return _dataset_xgboost_train[bp + 'a'], \
_dataset_xgboost_train[bp + 'b'], \
_dataset_xgboost_train[bp + 'c'], \
_dataset_xgboost_train[bp + 'e'], \
_dataset_xgboost_train[bp + 'f']
def dataset_xgboost_test(mode, cluster='no_cluster', kind='kind1'):
global _dataset_xgboost_test
bp = 'dataset/preprocessed/{}/{}/xgboost/{}/'.format(cluster, mode, kind)
if not 'a' in _dataset_xgboost_test:
#if mode == 'full':
_dataset_xgboost_test[bp+'a'] = sps.load_npz(
os.path.join(bp, 'X_test.npz'))
#else:
# _dataset_xgboost_test[bp+'a'] = pd.read_csv(
# os.path.join(bp, 'X_test.csv'), index_col=0)
_dataset_xgboost_test[bp+'b'] = pd.read_csv(
os.path.join(bp, 'y_test.csv'))['label'].to_dense()
_dataset_xgboost_test[bp+'c'] = np.load(
os.path.join(bp, 'group_test.npy'))
_dataset_xgboost_test[bp+'d'] = pd.read_csv(
os.path.join(bp, 'user_session_item_test.csv'))
return _dataset_xgboost_test[bp+'a'], \
_dataset_xgboost_test[bp+'b'], \
_dataset_xgboost_test[bp+'c'], \
_dataset_xgboost_test[bp+'d'],
def dataset_xgboost_classifier_train(mode, cluster='no_cluster'):
global _dataset_xgboost_classifier_train
path = 'dataset/preprocessed/{}/{}/xgboost_classifier/train.csv'.format(cluster, mode)
if path not in _dataset_xgboost_classifier_train:
_dataset_xgboost_classifier_train = pd.read_csv(path)
return _dataset_xgboost_classifier_train
def dataset_xgboost_classifier_test(mode, cluster='no_cluster'):
global _dataset_xgboost_classifier_test
path = 'dataset/preprocessed/{}/{}/xgboost_classifier/test.csv'.format(cluster, mode)
if path not in _dataset_xgboost_classifier_test:
_dataset_xgboost_classifier_test = pd.read_csv(path)
return _dataset_xgboost_classifier_test
def classification_train_df(mode, sparse=True, cluster='no_cluster', algo='xgboost'):
global _df_classification_train
path = 'dataset/preprocessed/{}/{}/{}/classification_train.csv'.format(
cluster, mode, algo)
if sparse:
tot_path = path + 'dense'
else:
tot_path = path + 'sparse'
if tot_path not in _df_classification_train:
if sparse:
data = ddf.read_csv(path, dtype={'1 Star filter active when clickout': 'float64',
'2 Nights filter active when clickout': 'float64',
'impression_position': 'float64',
'interaction_item_deals_session_ref_not_in_impr': 'float64'})
data = data.map_partitions(lambda part: part.to_sparse(fill_value=0))
data = data.compute().reset_index(drop=True)
data = data.drop(['Unnamed: 0'], axis=1)
_df_classification_train[tot_path] = data
else:
_df_classification_train[tot_path] = pd.read_csv(path, index_col=0)
return _df_classification_train[tot_path]
def classification_test_df(mode, sparse=True, cluster='no_cluster', algo='xgboost'):
global _df_classification_test
path = 'dataset/preprocessed/{}/{}/{}/classification_test.csv'.format(cluster, mode, algo)
if sparse:
tot_path = path + 'dense'
else:
tot_path = path + 'sparse'
if tot_path not in _df_classification_test:
if sparse:
data = ddf.read_csv(path, dtype={'1 Night filter active when clickout': 'float64',
'1 Star filter active when clickout': 'float64',
'2 Nights filter active when clickout': 'float64',
'2 Star filter active when clickout': 'float64',
'3 Star filter active when clickout': 'float64',
'4 Star filter active when clickout': 'float64',
'5 Star filter active when clickout': 'float64',
'Accessible Hotel filter active when clickout': 'float64',
'interaction_item_deals_session_ref_not_in_impr': 'float64',
'interaction_item_deals_session_ref_this_impr': 'float64',
'interaction_item_image_session_ref_not_in_impr': 'float64',
'interaction_item_rating_session_ref_not_in_impr': 'float64',
'Accessible Parking filter active when clickout': 'float64'})
data = data.map_partitions(
lambda part: part.to_sparse(fill_value=0))
data = data.compute().reset_index(drop=True)
data = data.drop(['Unnamed: 0'], axis=1)
_df_classification_test[tot_path] = data
else:
_df_classification_test[tot_path] = pd.read_csv(path, index_col=0)
return _df_classification_test[tot_path]
def dataset_catboost_train(mode, cluster='no_cluster'):
global _df_catboost_train
path = 'dataset/preprocessed/{}/{}/{}/train.csv'.format(cluster, mode, 'catboost')
if path not in _df_catboost_train:
_df_catboost_train[path] = pd.read_csv(path)
return _df_catboost_train[path]
def dataset_catboost_test(mode, cluster='no_cluster'):
global _df_catboost_test
path = 'dataset/preprocessed/{}/{}/{}/test.csv'.format(cluster, mode, 'catboost')
if path not in _df_catboost_test:
_df_catboost_test[path] = pd.read_csv(path)
return _df_catboost_test[path]
def train_indices(mode):
global _full_train_indices
path = 'dataset/preprocessed/{}/{}/train_indices.npy'.format(SPLIT_USED, mode)
if _full_train_indices is None:
_full_train_indices = pd.Index(np.load(path))
return _full_train_indices
def test_indices(mode):
global _full_test_indices
path = 'dataset/preprocessed/{}/{}/test_indices.npy'.format(SPLIT_USED, mode)
if _full_test_indices is None:
_full_test_indices = pd.Index(np.load(path))
return _full_test_indices
def accomodations_df():
global _df_items
if _df_items is None:
_df_items = pd.read_csv(ITEMS_PATH)
return _df_items
def accomodations_ids():
global _df_items_ids
if _df_items_ids is None:
_df_items_ids = list(map(int, accomodations_df()['item_id'].values))
return _df_items_ids
def accomodations_original_df():
global _df_original_items
if _df_original_items is None:
_df_original_items = pd.read_csv(ITEMS_ORIGINAL_PATH)
return _df_original_items
def accomodations_one_hot():
global _df_accomodations_one_hot
if not os.path.isfile(ACCOMODATIONS_1HOT_PATH):
print('Accomodations one-hot not found! Creating it...', flush=True)
import preprocess_utils.session2vec as sess2vec
sess2vec.save_accomodations_one_hot(accomodations_df(), ACCOMODATIONS_1HOT_PATH)
if _df_accomodations_one_hot is None:
print('Loading accomodations one-hot...', flush=True)
_df_accomodations_one_hot = pd.read_csv(ACCOMODATIONS_1HOT_PATH, index_col=0).astype('int8')
return _df_accomodations_one_hot
# URM structures
def urm(mode, cluster, type, urm_name='urm_clickout'):
global _urm
path = f'dataset/preprocessed/{cluster}/{mode}/matrices/{type}/{urm_name}.npz'
if path not in _urm:
_urm[path] = sps.load_npz(path)
return _urm[path]
def icm():
global _icm
# note is used the urm path since it is dataset/matrices/full/
icm_path = '{}{}.npz'.format(URM_PATH[0], 'icm')
if _icm is None:
_icm = sps.load_npz(icm_path)
return _icm
def dictionary_row(mode, urm_name, type, cluster='no_cluster'):
global _dict_row
path = f'dataset/preprocessed/{cluster}/{mode}/matrices/{type}/{urm_name}_dict_row.npy'
if path not in _dict_row:
_dict_row[path] = np.load(path).item()
return _dict_row[path]
def dictionary_col(mode, urm_name, type, cluster='no_cluster'):
# global _dict_col
global _dict_col
path = f'dataset/preprocessed/{cluster}/{mode}/matrices/{type}/{urm_name}_dict_col.npy'
if path not in _dict_col:
_dict_col[path] = np.load(path).item()
return _dict_col[path]
# those 2 functions let you save arbitrary fields in this file and recover those back
def read_config():
conf = None
try:
with open(CONFIG_FILE_PATH, 'rb') as file:
conf = pickle.load(file)
except IOError:
with open(CONFIG_FILE_PATH, 'wb') as file:
conf = {TRAIN_LEN_KEY: len(original_train_df())}
pickle.dump(conf, file)
return conf
def save_config(key, value):
conf = read_config()
conf[key] = value
with open(CONFIG_FILE_PATH, 'wb') as file:
pickle.dump(conf, file)