-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpctr.rb.3
194 lines (162 loc) · 5 KB
/
pctr.rb.3
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
require 'statsample'
total_clicks = 0
total_impressions = 0
ads = {}
advertisers = {}
keywords = {}
queries = {}
def log(msg)
warn("#{Time.now}: #{msg}")
end
log("Loading keywords...")
keywords_file = File.new("purchasedkeywordid_tokensid.txt", "r")
while (line = keywords_file.gets)
elements = line.split("\t")
keyword_id = elements[0]
keyword_tokens = elements[1].split("|")
keywords[keyword_id] = keyword_tokens
end
keywords_file.close
log("OK")
log("Loading queries...")
queries_file = File.new("queryid_tokensid.txt", "r")
while (line = queries_file.gets)
elements = line.split("\t")
query_id = elements[0]
query_tokens = elements[1].split("|")
queries[query_id] = query_tokens
end
queries_file.close
log("OK")
log("Loading training data...")
training_file = File.new("training.txt", "r")
while (line = training_file.gets)
elements = line.split("\t")
clicks = elements[0].to_i
impressions = elements[1].to_i
display_url = elements[2]
ad_id = elements[3]
advertiser_id = elements[4]
depth = elements[5]
position = elements[6]
query_id = elements[7]
keyword_id = elements[8]
title_id = elements[9]
description_id = elements[10]
user_id = elements[11]
ad = ads[ad_id]
if ad.nil?
ad = {}
ad['clicks'] = clicks
ad['impressions'] = impressions
ads[ad_id] = ad
else
ad['clicks'] += clicks
ad['impressions'] += impressions
end
advertiser = advertisers[advertiser_id]
if advertiser.nil?
advertiser = {}
advertiser['clicks'] = clicks
advertiser['impressions'] = impressions
advertisers[advertiser_id] = advertiser
else
advertiser['clicks'] += clicks
advertiser['impressions'] += impressions
end
total_clicks += clicks
total_impressions += impressions
end
training_file.close
log("OK")
mean_ctr = total_clicks/total_impressions.to_f
ads.each_pair do |ad_id, ad|
ad['pctr'] = ad['clicks']/ad['impressions'].to_f
end
advertisers.each_pair do |advertiser_id, advertiser|
advertiser['pctr'] = advertiser['clicks']/advertiser['impressions'].to_f
end
observed_ctrs = []
ad_pctrs = []
advertiser_pctrs = []
keyword_match_vals = []
log("Building regression vectors...")
training_file = File.new("training.txt", "r")
while (line = training_file.gets)
elements = line.split("\t")
clicks = elements[0].to_i
impressions = elements[1].to_i
display_url = elements[2]
ad_id = elements[3]
advertiser_id = elements[4]
depth = elements[5]
position = elements[6]
query_id = elements[7]
keyword_id = elements[8]
title_id = elements[9]
description_id = elements[10]
user_id = elements[11]
observed_ctrs.push(clicks / impressions.to_f)
ad = ads[ad_id]
ad_pctr = ad['pctr'] || mean_ctr
ad_pctrs.push(ad_pctr)
advertiser = advertisers[advertiser_id]
advertiser_pctr = advertiser['pctr'] || mean_ctr
advertiser_pctrs.push(advertiser_pctr)
keyword_tokens = keywords[keyword_id] || []
query_tokens = queries[query_id] || []
keyword_matches = (keyword_tokens & query_tokens).length
keyword_match_val = keyword_matches / [keyword_tokens.length, 3].min.to_f
keyword_match_vals.push(keyword_match_val)
end
training_file.close
log("OK")
log("Calculating regression coefficients...")
ds = {"observed_ctr" => observed_ctrs.to_scale,
"ad_pctr" => ad_pctrs.to_scale,
"advertiser_pctr" => advertiser_pctrs.to_scale,
"keyword_match_val" => keyword_match_vals.to_scale}.to_dataset
regression = Statsample::Regression.multiple(ds, "observed_ctr")
log(regression.summary)
log("OK")
constant = regression.constant
ad_pctr_coef = regression.coeffs["ad_pctr"]
advertiser_pctr_coef= regression.coeffs["advertiser_pctr"]
keyword_match_val_coef = regression.coeffs["keyword_match_val"]
# log("Constant: #{constant}")
# log("ad_pctr_coef: #{ad_pctr_coef}")
# log("advertiser_pctr_coef: #{advertiser_pctr_coef}")
# log("keyword_match_val_coef: #{keyword_match_val_coef}")
submission_file = File.new("submission.txt.3", "w")
log("Calculating pctrs...")
test_file = File.new("test.txt", "r")
while (line = test_file.gets)
elements = line.split("\t")
display_url = elements[0]
ad_id = elements[1]
advertiser_id = elements[2]
depth = elements[3]
position = elements[4]
query_id = elements[5]
keyword_id = elements[6]
title_id = elements[7]
description_id = elements[8]
user_id = elements[9]
ad = ads[ad_id] || {}
ad_pctr = ad['pctr'] || mean_ctr
# log("Ad pctr: #{ad_pctr}")
advertiser = advertisers[advertiser_id] || {}
advertiser_pctr = advertiser['pctr'] || mean_ctr
# log("Advertiser pctr: #{advertiser_pctr}")
keyword_tokens = keywords[keyword_id] || []
query_tokens = queries[query_id] || []
keyword_matches = (keyword_tokens & query_tokens).length
keyword_match_val = keyword_matches / [keyword_tokens.length, 3].min.to_f
# log("Keyword match val: #{keyword_match_val}")
pctr = constant + (ad_pctr_coef * ad_pctr) + (advertiser_pctr_coef * advertiser_pctr) + (keyword_match_val_coef * keyword_match_val)
# log("Pctr: #{pctr}")
submission_file.puts(pctr)
end
test_file.close
submission_file.close
log("OK")