|
| 1 | + |
| 2 | +import os |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +from deepctr.utils import SingleFeat |
| 7 | +from sklearn.preprocessing import LabelEncoder, StandardScaler |
| 8 | +from tensorflow.python.keras.preprocessing.sequence import pad_sequences |
| 9 | +from tqdm import tqdm |
| 10 | + |
| 11 | +from config import DSIN_SESS_COUNT, DSIN_SESS_MAX_LEN, FRAC |
| 12 | + |
| 13 | +FRAC = FRAC |
| 14 | +SESS_COUNT = DSIN_SESS_COUNT |
| 15 | + |
| 16 | + |
| 17 | +def gen_sess_feature_dsin(row): |
| 18 | + sess_count = DSIN_SESS_COUNT |
| 19 | + sess_max_len = DSIN_SESS_MAX_LEN |
| 20 | + sess_input_dict = {} |
| 21 | + sess_input_length_dict = {} |
| 22 | + for i in range(sess_count): |
| 23 | + sess_input_dict['sess_' + str(i)] = {'cate_id': [], 'brand': []} |
| 24 | + sess_input_length_dict['sess_' + str(i)] = 0 |
| 25 | + sess_length = 0 |
| 26 | + user, time_stamp = row[1]['user'], row[1]['time_stamp'] |
| 27 | + # sample_time = pd.to_datetime(timestamp_datetime(time_stamp )) |
| 28 | + if user not in user_hist_session: |
| 29 | + for i in range(sess_count): |
| 30 | + sess_input_dict['sess_' + str(i)]['cate_id'] = [0] |
| 31 | + sess_input_dict['sess_' + str(i)]['brand'] = [0] |
| 32 | + sess_input_length_dict['sess_' + str(i)] = 0 |
| 33 | + sess_length = 0 |
| 34 | + else: |
| 35 | + valid_sess_count = 0 |
| 36 | + last_sess_idx = len(user_hist_session[user]) - 1 |
| 37 | + for i in reversed(range(len(user_hist_session[user]))): |
| 38 | + cur_sess = user_hist_session[user][i] |
| 39 | + if cur_sess[0][2] < time_stamp: |
| 40 | + in_sess_count = 1 |
| 41 | + for j in range(1, len(cur_sess)): |
| 42 | + if cur_sess[j][2] < time_stamp: |
| 43 | + in_sess_count += 1 |
| 44 | + if in_sess_count > 2: |
| 45 | + sess_input_dict['sess_0']['cate_id'] = [e[0] for e in cur_sess[max(0, |
| 46 | + in_sess_count - sess_max_len):in_sess_count]] |
| 47 | + sess_input_dict['sess_0']['brand'] = [e[1] for e in |
| 48 | + cur_sess[max(0, in_sess_count - sess_max_len):in_sess_count]] |
| 49 | + sess_input_length_dict['sess_0'] = min( |
| 50 | + sess_max_len, in_sess_count) |
| 51 | + last_sess_idx = i |
| 52 | + valid_sess_count += 1 |
| 53 | + break |
| 54 | + for i in range(1, sess_count): |
| 55 | + if last_sess_idx - i >= 0: |
| 56 | + cur_sess = user_hist_session[user][last_sess_idx - i] |
| 57 | + sess_input_dict['sess_' + str(i)]['cate_id'] = [e[0] |
| 58 | + for e in cur_sess[-sess_max_len:]] |
| 59 | + sess_input_dict['sess_' + str(i)]['brand'] = [e[1] |
| 60 | + for e in cur_sess[-sess_max_len:]] |
| 61 | + sess_input_length_dict['sess_' + |
| 62 | + str(i)] = min(sess_max_len, len(cur_sess)) |
| 63 | + valid_sess_count += 1 |
| 64 | + else: |
| 65 | + sess_input_dict['sess_' + str(i)]['cate_id'] = [0] |
| 66 | + sess_input_dict['sess_' + str(i)]['brand'] = [0] |
| 67 | + sess_input_length_dict['sess_' + str(i)] = 0 |
| 68 | + |
| 69 | + sess_length = valid_sess_count |
| 70 | + return sess_input_dict, sess_input_length_dict, sess_length |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + |
| 75 | + user_hist_session = {} |
| 76 | + FILE_NUM = len( |
| 77 | + list(filter(lambda x: x.startswith('user_hist_session_' + str(FRAC) + '_dsin_'), |
| 78 | + os.listdir('../sampled_data/')))) |
| 79 | + |
| 80 | + print('total', FILE_NUM, 'files') |
| 81 | + |
| 82 | + for i in range(FILE_NUM): |
| 83 | + user_hist_session_ = pd.read_pickle( |
| 84 | + '../sampled_data/user_hist_session_' + str(FRAC) + '_dsin_' + str(i) + '.pkl') # 19,34 |
| 85 | + user_hist_session.update(user_hist_session_) |
| 86 | + del user_hist_session_ |
| 87 | + |
| 88 | + sample_sub = pd.read_pickle( |
| 89 | + '../sampled_data/raw_sample_' + str(FRAC) + '.pkl') |
| 90 | + |
| 91 | + index_list = [] |
| 92 | + sess_input_dict = {} |
| 93 | + sess_input_length_dict = {} |
| 94 | + for i in range(SESS_COUNT): |
| 95 | + sess_input_dict['sess_' + str(i)] = {'cate_id': [], 'brand': []} |
| 96 | + sess_input_length_dict['sess_' + str(i)] = [] |
| 97 | + |
| 98 | + sess_length_list = [] |
| 99 | + for row in tqdm(sample_sub[['user', 'time_stamp']].iterrows()): |
| 100 | + sess_input_dict_, sess_input_length_dict_, sess_length = gen_sess_feature_dsin( |
| 101 | + row) |
| 102 | + # index_list.append(index) |
| 103 | + for i in range(SESS_COUNT): |
| 104 | + sess_name = 'sess_' + str(i) |
| 105 | + sess_input_dict[sess_name]['cate_id'].append( |
| 106 | + sess_input_dict_[sess_name]['cate_id']) |
| 107 | + sess_input_dict[sess_name]['brand'].append( |
| 108 | + sess_input_dict_[sess_name]['brand']) |
| 109 | + sess_input_length_dict[sess_name].append( |
| 110 | + sess_input_length_dict_[sess_name]) |
| 111 | + sess_length_list.append(sess_length) |
| 112 | + |
| 113 | + print('done') |
| 114 | + |
| 115 | + user = pd.read_pickle('../sampled_data/user_profile_' + str(FRAC) + '.pkl') |
| 116 | + ad = pd.read_pickle('../sampled_data/ad_feature_enc_' + str(FRAC) + '.pkl') |
| 117 | + user = user.fillna(-1) |
| 118 | + user.rename( |
| 119 | + columns={'new_user_class_level ': 'new_user_class_level'}, inplace=True) |
| 120 | + |
| 121 | + sample_sub = pd.read_pickle( |
| 122 | + '../sampled_data/raw_sample_' + str(FRAC) + '.pkl') |
| 123 | + sample_sub.rename(columns={'user': 'userid'}, inplace=True) |
| 124 | + |
| 125 | + data = pd.merge(sample_sub, user, how='left', on='userid', ) |
| 126 | + data = pd.merge(data, ad, how='left', on='adgroup_id') |
| 127 | + |
| 128 | + sparse_features = ['userid', 'adgroup_id', 'pid', 'cms_segid', 'cms_group_id', 'final_gender_code', 'age_level', |
| 129 | + 'pvalue_level', 'shopping_level', 'occupation', 'new_user_class_level', 'campaign_id', |
| 130 | + 'customer'] # sparse feature for user and ads |
| 131 | + |
| 132 | + dense_features = ['price'] # dense feature for user and ads |
| 133 | + |
| 134 | + for feat in tqdm(sparse_features): |
| 135 | + lbe = LabelEncoder() # or Hash |
| 136 | + data[feat] = lbe.fit_transform(data[feat]) # 将不同的取值转换为对应的编号 |
| 137 | + mms = StandardScaler() |
| 138 | + data[dense_features] = mms.fit_transform(data[dense_features]) |
| 139 | + |
| 140 | + # class SingleFeat(namedtuple('SingleFeat', ['name', 'dimension', 'hash_flag', 'dtype'])): |
| 141 | + sparse_feature_list = [SingleFeat(feat, data[feat].nunique( |
| 142 | + ) + 1) for feat in sparse_features + ['cate_id', 'brand']] |
| 143 | + dense_feature_list = [SingleFeat(feat, 1) for feat in dense_features] |
| 144 | + sess_feature = ['cate_id', 'brand'] # sess feature for ad |
| 145 | + |
| 146 | + sess_input = [] |
| 147 | + sess_input_length = [] |
| 148 | + for i in tqdm(range(SESS_COUNT)): |
| 149 | + sess_name = 'sess_' + str(i) |
| 150 | + for feat in sess_feature: |
| 151 | + sess_input.append(pad_sequences( |
| 152 | + sess_input_dict[sess_name][feat], maxlen=SESS_COUNT, padding='post')) |
| 153 | + sess_input_length.append(sess_input_length_dict[sess_name]) |
| 154 | + |
| 155 | + model_input = [data[feat.name].values for feat in sparse_feature_list] + \ |
| 156 | + [data[feat.name].values for feat in dense_feature_list] |
| 157 | + sess_lists = sess_input + [np.array(sess_length_list)] |
| 158 | + model_input += sess_lists |
| 159 | + |
| 160 | + if not os.path.exists('../model_input/'): |
| 161 | + os.mkdir('../model_input/') |
| 162 | + |
| 163 | + pd.to_pickle(model_input, '../model_input/dsin_input_' + |
| 164 | + str(FRAC) + '_' + str(SESS_COUNT) + '.pkl') |
| 165 | + pd.to_pickle(data['clk'].values, '../model_input/dsin_label_' + |
| 166 | + str(FRAC) + '_' + str(SESS_COUNT) + '.pkl') |
| 167 | + pd.to_pickle({'sparse': sparse_feature_list, 'dense': dense_feature_list}, |
| 168 | + '../model_input/dsin_fd_' + str(FRAC) + '_' + str(SESS_COUNT) + '.pkl') |
| 169 | + print("gen dsin input done") |
0 commit comments