/
grid.py
executable file
·333 lines (260 loc) · 9.67 KB
/
grid.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python
import argparse
from sklearn.datasets import load_svmlight_file
import svmlight as SVC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.naive_bayes import GaussianNB as GNB
from sklearn.naive_bayes import MultinomialNB as MNB
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report as cr
from sklearn.metrics import accuracy_score as accus
from sklearn import cross_validation
#from sklearn.linear_model import LogisticRegression as LR
import numpy as np
import codecs
import oraclelib
import re
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='train', required=True)
parser.add_argument('-t', dest='test', required=True)
parser.add_argument('-f', dest='fold', required=True)
parser.add_argument('-r', dest='report', required=True)
parser.add_argument('-c', dest='cnt', required=True, help='the number of candidates for each sentence')
ret = parser.parse_args()
def convert2svmlight(dat):
output = []
for line in dat:
items = re.split(r'\s+', line)
tmp = []
score = float(items[0])
qid = int(re.split(r':',items[1])[1])
features = items[2:]
formated_feature = []
for f in features:
index, val = re.split(r':', f)
formated_feature.append(
(int(index), float(val))
)
tmp.append(score)
tmp.append(formated_feature)
tmp.append(qid)
output.append(tuple(tmp))
return output
def convert2pairwise(feature, target, syscnt):
if feature.shape[0]%4 != 0:
print "Error"
exit()
start = 0
pw_feature = []
pw_target = []
while start < feature.shape[0]:
end = start + syscnt
section = feature[start:end]
ngram = target[start:end]
for i in range(0, syscnt):
for j in range(0, syscnt):
if i == j:
continue
if ngram[i] == ngram[j]:
continue
tmp = (section[i] - section[j]).todense().tolist()[0]
pw_feature.append(tmp)
if ngram[i] - ngram[j] > 0:
pw_target.append(1)
else:
pw_target.append(-1)
start = end
ret = [np.array(pw_feature), np.array(pw_target)]
return ret
def data_split(target, mfolds):
sp = cross_validation.StratifiedKFold(target, mfolds)
return sp
def collect_data_qid(data_idx, train):
output = []
for item in train:
if item[2] in data_idx:
output.append(item)
return output
def clf_test(model, test):
ranking = []
start = 0
test_size = len(test)
while start < test_size:
end = start + 4
features = test[start:end]
scores = [0, 0, 0, 0, 0]
for i in range(4):
for j in range(i):
clf_feature = features[i] - features[j]
pred = model.predict(clf_feature)
if pred > 0:
scores[i] += 1.0
else:
scores[j] += 1.0
ranking.extend(scores)
start = end
return ranking
def ranking_test(model, test):
ranking = model.predict(test)
return ranking
def output_ranking(dat, output):
for item in dat:
output.write('%0.6f\n'%(item))
return None
def my_accus(feature, pred):
output = {}
for f, p in zip(feature, pred):
qid = f[2]
if qid not in output:
output[qid] = {}
output[qid]['real'] = []
output[qid]['pred'] = []
output[qid]['real'].append(f[0])
output[qid]['pred'].append(p)
input = output
num_disagree = 0
num_agree = 0
num_total = 0
for qid in input:
real = input[qid]['real']
pred = input[qid]['pred']
for i in range(len(real)):
for j in range(0, i):
if ((real[i] < real[j]) and (pred[i] < pred[j])) or ((real[i]>real[j]) and (pred[i]>pred[j])):
num_agree += 1
num_total += 1
elif real[i] == real[j]:
continue
else:
num_disagree += 1
num_total += 1
return float(num_agree)/float(num_total)
def my_cross_val_score(data_fold, train, c_p):
scores = []
for x, y in data_fold:
data_x = collect_data_qid(x, train)
data_y = collect_data_qid(y, train)
model = SVC.learn(data_x, C=c_p, kernel='linear', type='ranking')
pred = SVC.classify(model, data_y)
scores.append(
my_accus(data_y, pred)
)
return scores
def my_GridSearchCV(data_fold, train, param):
best = 0
best_c = None
for c in param['C']:
scores = my_cross_val_score(data_fold, train, c)
if (sum(scores)/len(scores)) > best:
best = (sum(scores)/len(scores))
best_c = c
return best_c
class mySVM(object):
def __init__(self, model_):
self.model = model_
return None
def predict(self, sample_):
pred = SVC.classify(self.model, sample_)
return pred
def SVM_experiment(data_fold, train, test, dumper):
param = { 'C':[] }
for i in range(-15, 15):
param['C'].append(pow(2, i))
c_best = my_GridSearchCV(data_fold, train, param)
dumper.write("Classifier: SVM\n")
dumper.write('Best Parameters: %f'%(c_best))
model = SVC.learn(train, C=c_best, kernel='linear', type='ranking')
ret = mySVM(model)
pred = ranking_test(ret, test)
output_ranking(pred, codecs.open('svm.ranking', 'w', 'utf-8'))
return None
def NB_experiment(data_fold, train, test, dumper):
print "Ready to find the Best Parameters for Naive Bayes"
print 'Gaussian Naive Bayes'
nb = GNB()
print "fitting NaiveBayes Experiment"
dumper.write('Classifier: Naive Bayes\n')
scores = cross_validation.cross_val_score(nb, train[0], train[1],
cv = data_fold, score_func=accus)
reports = "Accuracy on Train: %0.2f (+/- %0.2f)"%(scores.mean(), scores.std()/2)
print reports
dumper.write(reports+'\n')
reports = " ".join(['%0.2f'%(item) for item in scores])
dumper.write(reports+'\n')
nb = GNB()
nb.fit(train[0], train[1])
pred = clf_test(nb, test)
output_ranking(pred, codecs.open('nb.ranking', 'w', 'utf-8'))
return None
def LR_experiment(data_fold, train, test, dumper):
tuned_param = [
{ 'penalty':['l2'],
'dual':[True],
'C':[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5],
'fit_intercept':['False'],
'class_weight':['auto']},
]
model = GridSearchCV(LR(), tuned_param, score_func = accus)
model.fit(train[0], train[1], cv=data_fold)
print "Best Parameters in training set"
print model.best_estimator_
print ""
dumper.write('Classifier: Logistic Regression\n')
reports = 'Best Parameter:%r'%(model.best_estimator_)
dumper.write(reports + '\n')
scores = cross_validation.cross_val_score( model, train[0], train[1],
cv = data_fold, score_func = accus)
reports = 'Accuracy on Train Set:%0.2f (+/- %0.2f)'%(scores.mean(), scores.std()/2)
dumper.write(reports+'\n')
reports = ' '.join(['%0.2f'%item for item in scores])
dumper.write(reports+'\n')
pred = clf_test(model, test)
output_ranking(pred, codecs.open('lr.ranking', 'w', 'utf-8'))
return None
def RFC_experiment(data_fold, train, test, dumper):
tuned_param = [
{'n_estimators':[]}
]
for i in range(1, 20):
tuned_param[0]['n_estimators'].append(i*10)
print "Classifier: Random Forest Classifier"
dumper.write("Classifier: Random Forest Classifier\n")
model = GridSearchCV(RFC(n_estimators=10), tuned_param, score_func = accus)
model.fit(train[0], train[1], cv = data_fold)
print "best parameters found in training set"
print model.best_estimator_
print ""
dumper.write('Best Parameters: %r\n'%(model.best_estimator_))
scores = cross_validation.cross_val_score(model, train[0], train[1],
cv = data_fold, score_func=accus)
reports = "Accuracy on Train: %0.2f (+/- %0.2f)"%(scores.mean(), scores.std()/2)
print reports
dumper.write(reports + '\n')
reports = " ".join(['%f'%(item) for item in scores])
dumper.write(reports + '\n')
pred = clf_test(model, test)
output_ranking(pred, codecs.open('rfc.ranking', 'w', 'utf-8'))
return None
if __name__ == "__main__":
#data preprocessing
dev_f, dev_t = load_svmlight_file(ret.train)
print 'converting training dataset'
sys_cnt = int(ret.cnt)
dev = convert2pairwise(dev_f, dev_t)
test_f, test_t = load_svmlight_file(ret.test)
test_f = test_f.todense()
print 'spliting data into %s folds'%ret.fold
dev_split = data_split(dev[1], int(ret.fold))
#report file
sink = codecs.open(ret.report, 'w', 'utf-8')
NB_experiment(dev_split, dev, test_f, sink)
RFC_experiment(dev_split, dev, test_f, sink)
# LR_experiment(dev_split, dev, test_f, sink)
# dev4svm = convert2svmlight(oraclelib.read_plain(ret.train))
# test4svm = convert2svmlight(oraclelib.read_plain(ret.test))
# devsplit4svm = cross_validation.ShuffleSplit(len(dev4svm),
# n_iter=int(ret.fold),
# test_size = float(1)/float(ret.fold),
# random_state=404)
# SVM_experiment(devsplit4svm, dev4svm, test4svm, sink)
sink.close()