|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +''' |
| 15 | +process the Ali-CCP (Alibaba Click and Conversion Prediction) dataset. |
| 16 | +https://tianchi.aliyun.com/datalab/dataSet.html?dataId=408 |
| 17 | +
|
| 18 | +@The author: |
| 19 | + |
| 20 | +''' |
| 21 | +import numpy as np |
| 22 | +import joblib |
| 23 | +import re |
| 24 | +import random |
| 25 | +random.seed(2020) |
| 26 | +np.random.seed(2020) |
| 27 | +data_path = 'data/sample_skeleton_{}.csv' |
| 28 | +common_feat_path = 'data/common_features_{}.csv' |
| 29 | +enum_path = 'data/ctrcvr_enum.pkl' |
| 30 | +write_path = 'data/ctr_cvr' |
| 31 | +use_columns = [ |
| 32 | + '101', '121', '122', '124', '125', '126', '127', '128', '129', '205', |
| 33 | + '206', '207', '216', '508', '509', '702', '853', '301' |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +class process(object): |
| 38 | + def __init__(self): |
| 39 | + pass |
| 40 | + |
| 41 | + def process_train(self): |
| 42 | + c = 0 |
| 43 | + common_feat_dict = {} |
| 44 | + with open(common_feat_path.format('train'), 'r') as fr: |
| 45 | + for line in fr: |
| 46 | + line_list = line.strip().split(',') |
| 47 | + kv = np.array(re.split('\x01|\x02|\x03', line_list[2])) |
| 48 | + key = kv[range(0, len(kv), 3)] |
| 49 | + value = kv[range(1, len(kv), 3)] |
| 50 | + feat_dict = dict(zip(key, value)) |
| 51 | + common_feat_dict[line_list[0]] = feat_dict |
| 52 | + c += 1 |
| 53 | + if c % 100000 == 0: |
| 54 | + print(c) |
| 55 | + print('join feats...') |
| 56 | + c = 0 |
| 57 | + vocabulary = dict( |
| 58 | + zip(use_columns, [{} for _ in range(len(use_columns))])) |
| 59 | + with open(data_path.format('train') + '.tmp', 'w') as fw: |
| 60 | + fw.write('click,purchase,' + ','.join(use_columns) + '\n') |
| 61 | + with open(data_path.format('train'), 'r') as fr: |
| 62 | + for line in fr: |
| 63 | + line_list = line.strip().split(',') |
| 64 | + if line_list[1] == '0' and line_list[2] == '1': |
| 65 | + continue |
| 66 | + kv = np.array(re.split('\x01|\x02|\x03', line_list[5])) |
| 67 | + key = kv[range(0, len(kv), 3)] |
| 68 | + value = kv[range(1, len(kv), 3)] |
| 69 | + feat_dict = dict(zip(key, value)) |
| 70 | + feat_dict.update(common_feat_dict[line_list[3]]) |
| 71 | + feats = line_list[1:3] |
| 72 | + for k in use_columns: |
| 73 | + feats.append(feat_dict.get(k, '0')) |
| 74 | + fw.write(','.join(feats) + '\n') |
| 75 | + for k, v in feat_dict.items(): |
| 76 | + if k in use_columns: |
| 77 | + if v in vocabulary[k]: |
| 78 | + vocabulary[k][v] += 1 |
| 79 | + else: |
| 80 | + vocabulary[k][v] = 0 |
| 81 | + c += 1 |
| 82 | + if c % 100000 == 0: |
| 83 | + print(c) |
| 84 | + print('before filter low freq:') |
| 85 | + for k, v in vocabulary.items(): |
| 86 | + print(k + ':' + str(len(v))) |
| 87 | + new_vocabulary = dict( |
| 88 | + zip(use_columns, [set() for _ in range(len(use_columns))])) |
| 89 | + for k, v in vocabulary.items(): |
| 90 | + for k1, v1 in v.items(): |
| 91 | + if v1 > 10: |
| 92 | + new_vocabulary[k].add(k1) |
| 93 | + vocabulary = new_vocabulary |
| 94 | + print('after filter low freq:') |
| 95 | + for k, v in vocabulary.items(): |
| 96 | + print(k + ':' + str(len(v))) |
| 97 | + joblib.dump(vocabulary, enum_path, compress=3) |
| 98 | + |
| 99 | + print('encode feats...') |
| 100 | + vocabulary = joblib.load(enum_path) |
| 101 | + feat_map = {} |
| 102 | + for feat in use_columns: |
| 103 | + feat_map[feat] = dict( |
| 104 | + zip(vocabulary[feat], range(1, len(vocabulary[feat]) + 1))) |
| 105 | + c = 0 |
| 106 | + with open(write_path + '.train', 'w') as fw1: |
| 107 | + with open(write_path + '.dev', 'w') as fw2: |
| 108 | + fw1.write('click,purchase,' + ','.join(use_columns) + '\n') |
| 109 | + fw2.write('click,purchase,' + ','.join(use_columns) + '\n') |
| 110 | + with open(data_path.format('train') + '.tmp', 'r') as fr: |
| 111 | + fr.readline() # remove header |
| 112 | + for line in fr: |
| 113 | + line_list = line.strip().split(',') |
| 114 | + new_line = line_list[:2] |
| 115 | + for value, feat in zip(line_list[2:], use_columns): |
| 116 | + new_line.append( |
| 117 | + str(feat_map[feat].get(value, '0'))) |
| 118 | + if random.random() >= 0.9: |
| 119 | + fw2.write(','.join(new_line) + '\n') |
| 120 | + else: |
| 121 | + fw1.write(','.join(new_line) + '\n') |
| 122 | + c += 1 |
| 123 | + if c % 100000 == 0: |
| 124 | + print(c) |
| 125 | + |
| 126 | + def process_test(self): |
| 127 | + c = 0 |
| 128 | + common_feat_dict = {} |
| 129 | + with open(common_feat_path.format('test'), 'r') as fr: |
| 130 | + for line in fr: |
| 131 | + line_list = line.strip().split(',') |
| 132 | + kv = np.array(re.split('\x01|\x02|\x03', line_list[2])) |
| 133 | + key = kv[range(0, len(kv), 3)] |
| 134 | + value = kv[range(1, len(kv), 3)] |
| 135 | + feat_dict = dict(zip(key, value)) |
| 136 | + common_feat_dict[line_list[0]] = feat_dict |
| 137 | + c += 1 |
| 138 | + if c % 100000 == 0: |
| 139 | + print(c) |
| 140 | + print('join feats...') |
| 141 | + c = 0 |
| 142 | + with open(data_path.format('test') + '.tmp', 'w') as fw: |
| 143 | + fw.write('click,purchase,' + ','.join(use_columns) + '\n') |
| 144 | + with open(data_path.format('test'), 'r') as fr: |
| 145 | + for line in fr: |
| 146 | + line_list = line.strip().split(',') |
| 147 | + if line_list[1] == '0' and line_list[2] == '1': |
| 148 | + continue |
| 149 | + kv = np.array(re.split('\x01|\x02|\x03', line_list[5])) |
| 150 | + key = kv[range(0, len(kv), 3)] |
| 151 | + value = kv[range(1, len(kv), 3)] |
| 152 | + feat_dict = dict(zip(key, value)) |
| 153 | + feat_dict.update(common_feat_dict[line_list[3]]) |
| 154 | + feats = line_list[1:3] |
| 155 | + for k in use_columns: |
| 156 | + feats.append(str(feat_dict.get(k, '0'))) |
| 157 | + fw.write(','.join(feats) + '\n') |
| 158 | + c += 1 |
| 159 | + if c % 100000 == 0: |
| 160 | + print(c) |
| 161 | + |
| 162 | + print('encode feats...') |
| 163 | + vocabulary = joblib.load(enum_path) |
| 164 | + feat_map = {} |
| 165 | + for feat in use_columns: |
| 166 | + feat_map[feat] = dict( |
| 167 | + zip(vocabulary[feat], range(1, len(vocabulary[feat]) + 1))) |
| 168 | + c = 0 |
| 169 | + with open(write_path + '.test', 'w') as fw: |
| 170 | + fw.write('click,purchase,' + ','.join(use_columns) + '\n') |
| 171 | + with open(data_path.format('test') + '.tmp', 'r') as fr: |
| 172 | + fr.readline() # remove header |
| 173 | + for line in fr: |
| 174 | + line_list = line.strip().split(',') |
| 175 | + new_line = line_list[:2] |
| 176 | + for value, feat in zip(line_list[2:], use_columns): |
| 177 | + new_line.append(str(feat_map[feat].get(value, '0'))) |
| 178 | + fw.write(','.join(new_line) + '\n') |
| 179 | + c += 1 |
| 180 | + if c % 100000 == 0: |
| 181 | + print(c) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + pros = process() |
| 186 | + pros.process_train() |
| 187 | + pros.process_test() |
0 commit comments