-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmart.py
274 lines (202 loc) · 7.6 KB
/
mart.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
import math
import numpy as np
import math
import pandas
from optparse import OptionParser
from sklearn.tree import DecisionTreeRegressor
from collections import defaultdict
from copy import deepcopy
from multiprocessing import Pool
from itertools import chain
import time
class Ensemble:
def __init__(self, rate):
self.trees = []
self.rate = rate
def __len__(self):
return len(self.trees)
def add(self, tree):
self.trees.append(tree)
def eval_one(self, object):
return self.eval([object])[0]
def eval(self, objects):
results = np.zeros(len(objects))
for tree in self.trees:
results += tree.predict(objects) * self.rate
return results
def remove(self, number):
self.trees = self.trees[:-number]
def groupby(score, query):
result = []
this_query = None
for s, q in zip(score, query):
if q != this_query:
result.append([])
this_query = q
result[-1].append(s)
result = map(np.array, result)
return result
def point_dcg(arg):
i, label = arg
return (2 ** label - 1) / math.log(i + 2, 2)
def dcg(scores):
return sum(map(point_dcg, enumerate(scores)))
def ndcg(page, k=10):
model_top = page[:k]
true_top = np.array([])
if len(page) > 10:
true_top = np.partition(page, -10)[-k:]
true_top.sort()
else:
true_top = np.sort(page)
true_top = true_top[::-1]
max_dcg = dcg(true_top)
model_dcg = dcg(model_top)
if max_dcg == 0:
return 1
return model_dcg / max_dcg
def score(prediction, true_score, query, k=10):
true_pages = groupby(true_score, query)
model_pages = groupby(prediction, query)
total_ndcg = []
for true_page, model_page in zip(true_pages, model_pages):
page = true_page[np.argsort(model_page)[::-1]]
total_ndcg.append(ndcg(page, k))
return sum(total_ndcg) / len(total_ndcg)
def query_lambdas(page, k=10):
true_page, model_page = page
worst_order = np.argsort(true_page)
true_page = true_page[worst_order]
model_page = model_page[worst_order]
model_order = np.argsort(model_page)
idcg = dcg(np.sort(true_page)[-10:][::-1])
size = len(true_page)
position_score = np.zeros((size, size))
for i in range(size):
for j in range(size):
position_score[model_order[i], model_order[j]] = \
point_dcg((model_order[j], true_page[model_order[i]]))
lambdas = np.zeros(size)
for i in range(size):
for j in range(size):
if true_page[i] > true_page[j]:
delta_dcg = position_score[i][j] - position_score[i][i]
delta_dcg += position_score[j][i] - position_score[j][j]
delta_ndcg = abs(delta_dcg / idcg)
rho = 1 / (1 + math.exp(model_page[i] - model_page[j]))
lam = rho * delta_ndcg
lambdas[j] -= lam
lambdas[i] += lam
return lambdas
def compute_lambdas(prediction, true_score, query, k=10):
true_pages = groupby(true_score, query)
model_pages = groupby(prediction, query)
#print (len(true_pages), "pages")
pool = Pool()
lambdas = pool.map(query_lambdas, zip(true_pages, model_pages))
return list(chain(*lambdas))
def mart_responces(prediction, true_score):
return true_score - prediction
def learn(train_file, n_trees=10, learning_rate=0.1, k=10, validate=False):
print("Loading train file")
print("train_file")
print(train_file)
train = np.loadtxt(train_file, delimiter=",", skiprows=1)
print("train file array")
print(train)
scores = train[:, 0]
# val_scores = train[:, 0]
print("scores")
print(scores)
queries = train[:, 1]
# val_queries = validation[:, 1]
print("queries")
print(queries)
features = train[:, 3:]
# val_features = validation[:, 3:]
print("features")
print(features)
ensemble = Ensemble(learning_rate)
print("Training starts...")
model_output = np.zeros(len(features))
# val_output = np.array([float(0)] * len(validation))
# best_validation_score = 0
time.clock()
for i in range(n_trees):
print(" Iteration: " + str(i + 1))
# Compute psedo responces (lambdas)
# witch act as training label for document
start = time.clock()
print(" --generating labels")
lambdas = compute_lambdas(model_output, scores, queries, k)
print(zip(lambdas, scores))
#lambdas = mart_responces(model_output, scores)
print(" --done", str(time.clock() - start) + " sec")
# create tree and append it to the model
print(" --fitting tree")
start = time.clock()
tree = DecisionTreeRegressor(max_depth=6)
# print "Distinct lambdas", set(lambdas)
tree.fit(features, lambdas)
print(" ---done", str(time.clock() - start) + " sec")
print(" --adding tree to ensemble")
ensemble.add(tree)
# update model score
print(" --generating step prediction")
prediction = tree.predict(features)
# print "Distinct answers", set(prediction)
print(" --updating full model output")
model_output += learning_rate * prediction
# print set(model_output)
# train_score
start = time.clock()
print(" --scoring on train")
train_score = score(model_output, scores, queries, 10)
print(" --iteration train score " + str(train_score) + ", took " + str(time.clock() - start) + "sec to calculate")
# # validation score
# print " --scoring on validation"
# val_output += learning_rate * tree.predict(val_features)
# val_score = ndcg(val_output, val_scores, val_queries, 10)
# print " --iteration validation score " + str(val_score)
# if(validation_score > best_validation_score):
# best_validation_score = validation_score
# best_model_len = len(ensemble)
# # have we assidently break the celling?
# if (best_validation_score > 0.9):
# break
# rollback to best
# if len(ensemble) > best_model_len:
# ensemble.remove(len(ensemble) - best_model_len)
# finishing up
# print "final quality evaluation"
#train_score = compute_ndcg(ensemble.eval(features), scores)
# test_score = compute_ndcg(ensemble.eval(validation), validation_score)
# print "train %s, test %s" % (train_score, test_score)
print ("Finished sucessfully.")
print ("------------------------------------------------")
return ensemble
def predict(model, fn):
predict = np.loadtxt(fn, delimiter=",", skiprows=1)
queries = predict[:, 1]
doc_id = predict[:, 2]
features = predict[:, 3:]
results = model.eval(features)
writer = csv.writer(open("result.csv"))
for line in zip(queries, results, doc_id):
writer.writerow(line)
return "OK"
import csv
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-t", "--train", action="store", type="string", dest="train_file")
parser.add_option("-v", "--validation", action="store_true", dest="validate")
parser.add_option("-p", "--predict", action="store", type="string", dest="predict_file")
options, args = parser.parse_args()
options.train_file = args[0]
options.validate = args[1]
options.predict_file = args[2]
iterations = 30
learning_rate = 0.001
model = learn(options.train_file,validate = options.validate,n_trees = 200)
if options.predict_file:
predict(model, options.predict_file)