-
Notifications
You must be signed in to change notification settings - Fork 33
/
export-bin-data-p1.cpp
292 lines (212 loc) · 10.5 KB
/
export-bin-data-p1.cpp
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
#include "util/io.h"
#include "util/data.h"
#include "util/generation.h"
#include "util/helpers.h"
#include "ffm.h"
std::vector<std::pair<std::string, std::string>> files = {
{ "cache/clicks_cv2_train.csv.gz", "cv2_train" },
{ "cache/clicks_cv2_test.csv.gz", "cv2_test" },
{ "cache/clicks_cv1_train.csv.gz", "cv1_train" },
{ "cache/clicks_cv1_test.csv.gz", "cv1_test" },
{ "../input/clicks_train.csv.gz", "full_train" },
{ "../input/clicks_test.csv.gz", "full_test" },
};
std::vector<std::string> features = {
"leak",
"viewed_docs", "viewed_categories", "viewed_topics",
"uid_viewed_ads", "uid_viewed_ad_srcs", "uid_viewed_ad_cats", "uid_viewed_ad_tops",
"rivals"
};
std::string cur_dataset;
std::unordered_map<int, int> ad_counts;
std::unordered_map<int, int> ad_campaign_counts;
std::unordered_map<int, int> ad_advertiser_counts;
std::unordered_map<int, int> ad_doc_counts;
std::unordered_map<int, int> ad_doc_source_counts;
std::unordered_map<int, int> ad_doc_publisher_counts;
std::unordered_map<int, int> ev_doc_counts;
std::unordered_map<int, int> ev_doc_source_counts;
std::unordered_map<int, int> ev_doc_publisher_counts;
std::unordered_map<int, int> uid_counts;
void load_dataset_data(const std::string & dataset) {
if (cur_dataset == dataset)
return;
cur_dataset = dataset;
std::cout << "Loading " << dataset << " data..." << std::endl;
ad_counts = read_map(std::string("cache/ad_counts_") + dataset + std::string(".csv.gz"), read_count);
ad_campaign_counts = read_map(std::string("cache/ad_campaign_counts_") + dataset + std::string(".csv.gz"), read_count);
ad_advertiser_counts = read_map(std::string("cache/ad_advertiser_counts_") + dataset + std::string(".csv.gz"), read_count);
ad_doc_counts = read_map(std::string("cache/ad_doc_counts_") + dataset + std::string(".csv.gz"), read_count);
ad_doc_source_counts = read_map(std::string("cache/ad_doc_source_counts_") + dataset + std::string(".csv.gz"), read_count);
ad_doc_publisher_counts = read_map(std::string("cache/ad_doc_publisher_counts_") + dataset + std::string(".csv.gz"), read_count);
ev_doc_counts = read_map(std::string("cache/ev_doc_counts_") + dataset + std::string(".csv.gz"), read_count);
ev_doc_source_counts = read_map(std::string("cache/ev_doc_source_counts_") + dataset + std::string(".csv.gz"), read_count);
ev_doc_publisher_counts = read_map(std::string("cache/ev_doc_publisher_counts_") + dataset + std::string(".csv.gz"), read_count);
uid_counts = read_map(std::string("cache/uid_counts_") + dataset + std::string(".csv.gz"), read_count);
}
class writer {
std::string file_name;
ffm_stream_data_writer data_out;
ffm_index index;
public:
writer(const std::string & file_name): file_name(file_name), data_out(file_name + ".data") {
index.size = 0;
index.offsets.push_back(0);
load_dataset_data(file_name.substr(6, file_name.find("_") - 6));
}
void write(const reference_data & data, const std::vector<std::vector<std::string>> & rows);
void finish();
};
void writer::write(const reference_data & data, const std::vector<std::vector<std::string>> & rows) {
int event_id = stoi(rows[0][0]);
int ad_id = stoi(rows[0][1]);
//
auto ad = data.ads[ad_id];
auto event = data.events[event_id];
auto ad_doc = data.documents.at(ad.document_id);
auto ad_doc_categories = data.document_categories.equal_range(ad.document_id);
//auto ad_doc_topics = data.document_topics.equal_range(ad.document_id);
//auto ad_doc_entities = data.document_entities.equal_range(ad.document_id);
auto ev_doc = data.documents.at(event.document_id);
auto ev_doc_categories = data.document_categories.equal_range(event.document_id);
//auto ev_doc_topics = data.document_topics.equal_range(event.document_id);
//auto ev_doc_entities = data.document_entities.equal_range(event.document_id);
// Get counts
auto ad_count = ad_counts.at(ad_id);
auto ad_campaign_count = ad_campaign_counts.at(ad.campaign_id);
auto ad_advertiser_count = ad_advertiser_counts.at(ad.advertiser_id);
auto ad_doc_count = ad_doc_counts.at(ad.document_id);
auto ad_doc_source_count = ad_doc_source_counts.at(ad_doc.source_id);
auto ad_doc_publisher_count = ad_doc_publisher_counts.at(ad_doc.publisher_id);
auto ev_doc_count = ev_doc_counts.at(event.document_id);
auto ev_doc_source_count = ev_doc_source_counts.at(ev_doc.source_id);
auto ev_doc_publisher_count = ev_doc_publisher_counts.at(ev_doc.publisher_id);
auto uid_count = uid_counts.at(event.uid);
// Start building line
ffm_feature_vector_builder features(200);
// Event features
features.hashed(0, event.platform);
features.hashed(1, event.country);
features.hashed(2, event.state);
//features.hashed(, event.region);
features.hashed(3, uid_count < 50 ? uid_count : event.uid + 100);
// Document info
features.hashed(4, ev_doc_count < 50 ? ev_doc_count : event.document_id + 100);
features.hashed(5, ev_doc_source_count < 10 ? ev_doc_source_count : ev_doc.source_id + 10);
features.hashed(6, ev_doc_publisher_count < 10 ? ev_doc_publisher_count : ev_doc.publisher_id + 10);
for (auto it = ev_doc_categories.first; it != ev_doc_categories.second; ++ it)
features.hashed(7, it->second.first, it->second.second);
/*
for (auto it = ev_doc_topics.first; it != ev_doc_topics.second; ++ it)
features.hashed(14, it->second.first, it->second.second);
for (auto it = ev_doc_entities.first; it != ev_doc_entities.second; ++ it)
features.hashed(16, it->second.first, it->second.second);
*/
// Common features
// Same feature markers
if (ad_doc.publisher_id == ev_doc.publisher_id)
features.raw(10, 0); // Same publisher
if (ad_doc.source_id == ev_doc.source_id)
features.raw(10, 1); // Same source
// Document view features (including leak)
if (stoi(rows[1][0]) > 0)
features.raw(11, 2); // Viewed ad document (leak)
if (stoi(rows[1][1]) > 0)
features.raw(11, 3); // Not viewed ad document (leak)
if (stoi(rows[2][0]) > 0)
features.raw(11, 4); // Viewed documents of same publisher
if (stoi(rows[2][1]) > 0)
features.raw(11, 5); // Viewed documents of same source
if (stof(rows[3][0]) > 0)
features.raw(11, 6); // Viewed documents of the similar category
if (stof(rows[4][0]) > 0)
features.raw(11, 7); // Viewed documents of the similar topic
// Ad view/click features
auto & v_ad_row = rows[5];
auto & v_ad_src_row = rows[6];
auto & v_ad_cat_row = rows[7];
auto & v_ad_top_row = rows[8];
if (stoi(v_ad_row[2]) > 0)
features.raw(12, 20); // Viewed this ad earlier
if (stoi(v_ad_row[1]) > 0)
features.raw(12, 21); // Clicked this ad earlier
if (stoi(v_ad_row[5]) > 0)
features.raw(12, 22); // Viewed this ad doc earlier
if (stoi(v_ad_row[4]) > 0)
features.raw(12, 23); // Clicked this ad doc earlier
if (stoi(v_ad_src_row[2]) > 0)
features.raw(12, 24); // Viewed ad of the same publisher earlier
if (stoi(v_ad_src_row[1]) > 0)
features.raw(12, 25); // Clicked ad of the same publisher earlier
if (stoi(v_ad_src_row[5]) > 0)
features.raw(12, 26); // Viewed ad of the same source earlier
if (stoi(v_ad_src_row[4]) > 0)
features.raw(12, 27); // Clicked ad of the same source earlier
if (stof(v_ad_cat_row[2]) > 0)
features.raw(12, 28); // Viewed ad of the similar category
if (stof(v_ad_cat_row[1]) > 0)
features.raw(12, 29); // Clicked ad of the similar category
if (stof(v_ad_top_row[2]) > 0)
features.raw(12, 30); // Viewed ad of the similar topic
if (stof(v_ad_top_row[1]) > 0)
features.raw(12, 32); // Clicked ad of the similar topic
features.raw(13, event.weekday + 50);
features.raw(13, event.hour + 70);
features.raw(14, 80, pos_time_diff(event.timestamp - ad_doc.publish_timestamp));
features.raw(14, 81, time_diff(ev_doc.publish_timestamp - ad_doc.publish_timestamp));
features.raw(18, stoi(rows[9][0]) + 180); // Rival count
// Rival ids
auto rival_ids = split(rows[9][1], ' ');
for (uint ri = 0; ri < rival_ids.size(); ++ ri) {
auto rival_id = stoi(rival_ids[ri]);
if (rival_id != ad_id)
features.hashed(20, rival_id);
}
auto doc_ad_others_it = data.doc_ad_others.find(event_id);
if (doc_ad_others_it != data.doc_ad_others.end()) {
auto ids = doc_ad_others_it->second;
for (uint i = 0; i < ids.size(); ++ i)
features.hashed(21, ids[i]);
}
// Similarity features
/*
for (uint i = 0; i < rows[2].size(); ++ i)
if (stof(rows[2][i]) > 0)
features.raw(26 + i, 6 + i, stof(rows[2][i]));
*/
// Ad features
features.hashed(30, ad_count < 50 ? ad_count : ad_id + 100);
features.hashed(31, ad_campaign_count < 50 ? ad_campaign_count : ad.campaign_id + 100);
features.hashed(32, ad_advertiser_count < 50 ? ad_advertiser_count : ad.advertiser_id + 100);
// Promoted document info
features.hashed(33, ad_doc_count < 50 ? ad_doc_count : ad.document_id + 100);
features.hashed(34, ad_doc_source_count < 10 ? ad_doc_source_count : ad_doc.source_id + 10);
features.hashed(35, ad_doc_publisher_count < 10 ? ad_doc_publisher_count : ad_doc.publisher_id + 10);
for (auto it = ad_doc_categories.first; it != ad_doc_categories.second; ++ it)
features.hashed(36, it->second.first, it->second.second);
/*
for (auto it = ad_doc_topics.first; it != ad_doc_topics.second; ++ it)
features.hashed(15, it->second.first, it->second.second);
for (auto it = ad_doc_entities.first; it != ad_doc_entities.second; ++ it)
features.hashed(17, it->second.first, it->second.second);
*/
// Write data
auto offset = data_out.write(features.data());
// Update index
index.size ++;
index.labels.push_back(rows[0].size() == 3 ? stof(rows[0][2]) * 2 - 1 : 0);
index.offsets.push_back(offset);
index.norms.push_back(features.norm());
index.groups.push_back(event_id);
}
void writer::finish() {
ffm_write_index(file_name + ".index", index);
}
int main() {
using namespace std;
cout << "Loading reference data..." << endl;
auto data = load_reference_data();
cout << "Generating files..." << endl;
generate_files<reference_data, writer>(data, build_filesets(files, features, "_bin_p1"));
cout << "Done." << endl;
}