-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlars-traps-split-half.py
More file actions
378 lines (294 loc) · 15.1 KB
/
Copy pathlars-traps-split-half.py
File metadata and controls
378 lines (294 loc) · 15.1 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Note: Mainly created for Lars-Traps with 50:50 split strategy
for Table 3 and Table 4.
"""
import pandas as pd
import numpy as np
import sys
#from ranx import Run, fuse
import argparse, itertools
import scipy.stats as stats
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import root_mean_squared_error
from pandas import ExcelWriter
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import ElasticNet, ElasticNetCV, Lars, LarsCV, Lasso, LassoCV
from sklearn.linear_model import Ridge, RidgeCV, LassoLars, LassoLarsCV, GammaRegressor, PoissonRegressor
from sklearn.model_selection import KFold, RepeatedKFold
from scipy.stats import ttest_ind
from sklearn.svm import SVR, NuSVR
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.kernel_ridge import KernelRidge
from sklearn.utils import resample
from collections import Counter
from smare import smare
def min_max_normalize(lst):
"""Min-max normalization of a list of numbers."""
min_val = min(lst)
max_val = max(lst)
if min_val == max_val:
return [0] * len(lst)
normalized_lst = [(x - min_val) / (max_val - min_val) for x in lst]
return normalized_lst
def squared_error(predictions, targets):
predictions = np.array(predictions)
targets = np.array(targets)
#return np.sqrt(((predictions - targets) ** 2))
return ((predictions - targets) ** 2)
def plot_scatter_and_line(qpp_scores, y_train):
"""
Input (qpp_scores) : a column (Series) containing QPP scores
"""
# QPP scores = inputs/independent variables
# AP scores = outputs/responses/dependent variables
x = qpp_scores.to_numpy().reshape(-1,1) # any no. of rows x 1 column
model = LinearRegression().fit(x, y_train)
return model
def weight_vector_with_traps(coeff_path, intercept, no_of_predictors):
#print(f"length of the coeff path: {len(coeff_path[0])-1}")
#print(f"coeff path: {coeff_path}")
column_num = 0
for col in range(coeff_path.shape[1]):
#print(f"vector {coeff_path[no_of_predictors:, col]}")
if np.any(coeff_path[no_of_predictors:, col] != 0) :
column_num = col
break
if column_num > 0:
column_num -= 1
else:
column_num = 0
#print(f"column no. {column_num}")
weight_vector = coeff_path[:no_of_predictors, column_num]
#print(f"weight vector {weight_vector}")
return weight_vector, intercept
def predict_y_values(weight_vector, x_test, intercept):
# print(f"x_test {x_test.to_string()}")
# print(f"new shape: {np.array(x_test).size}")
# print(f"intercept: {intercept}")
# print(f"weight_vector: {weight_vector}")
# print(f"shape of weight vector {np.array(weight_vector).reshape(1, -1).transpose().size}")
y_predict = np.array(x_test) @ np.array(weight_vector) + intercept
# print(f"y_predict {y_predict}")
return y_predict
def compute_regression():
parser = argparse.ArgumentParser()
parser.add_argument("--input", type = str, help = "./PATH/TO/CSV/INPUT")
parser.add_argument("--k", type = str, choices= ["100", "1000"], help = "retrieval depth")
parser.add_argument("--qpp_type", type = str, choices= ["pre", "post"], help= "pre / post")
parser.add_argument("--dataset", type = str, choices = ["trec678rb", "trecdl", "trecdl19",
"trecdl20", "clueweb09b"], help = "trec678rb / trecdl19 / trecdl20 / clueweb09b", required=True)
args = parser.parse_args()
input_dir = str(args.input)
dataset_type = str(args.dataset)
qpp_type = str(args.qpp_type)
k = str(args.k)
input_path = input_dir + "/" + dataset_type + "-" + qpp_type + "-ret.csv"
df = pd.read_csv(input_path, skiprows = 3)
if qpp_type == "post":
qpp_approaches = ["nqc", "wig", "clarity", "uef_nqc", "uef_wig",
"uef_clarity", "neuralqpp", "qppbertpl", "deepqpp", "bertqpp"]
best_qpp = ["qppbertpl"]
else:
#qpp_approaches = ["MaxIDF", "AvgIDF", "AvQC", "AVQCG", "SumSCQ", "MaxSCQ",
# "AvgSCQ", "SumVAR", "AvgVAR", "MaxVAR", "AvP", "AvNP"]
qpp_approaches = ["MaxIDF", "AvgIDF", "SumSCQ", "MaxSCQ",
"AvgSCQ", "SumVAR", "AvgVAR", "MaxVAR", "AvP", "AvNP"]
best_qpp = ["MaxIDF"]
# unsupervised only
#qpp_approaches = ["nqc", "wig", "clarity", "uef_nqc", "uef_wig",
# "uef_clarity", "neuralqpp"]
#best_qpp = ["deepqpp"]
#best_qpp = ["qppbertpl"]
#best_qpp = [best_qpp_approach]
#best_qpp.append(best_qpp_approach)
#best_qpp = ["MaxIDF"]
#best_qpp = ["bertqpp"]
#qpp_approaches = ["deepqpp"]
# Discarding the row containing the avg. map
df = df[df["QID"] != "MAP"]
#ap_real_pred = pd.read_csv("querywise-ap-qppscores.csv", header=3, index_col=0)
#ap_real_pred = pd.read_csv("data/qpp-fusion-trecdl-dl19.csv", header=3, index_col=0)
ap_real_pred = pd.read_csv(input_path, header=3, index_col=0)
scaler = MinMaxScaler()
#for qpp_approach in qpp_approaches:
ap_qpp_pred = pd.DataFrame()
# min-max normalization of all qpp predictors
ap_real_pred[qpp_approaches] = scaler.fit_transform(ap_real_pred[qpp_approaches])
ap_qpp_pred[best_qpp] = scaler.fit_transform(ap_real_pred[best_qpp])
# In case you don't want to normalize
#ap_real_pred[qpp_approaches] = ap_real_pred[qpp_approaches]
#ap_qpp_pred[best_qpp] = ap_real_pred[best_qpp]
#print(f"k : {k}")
if k == "100":
y = ap_real_pred['ap@100']
else:
y = ap_real_pred['ap@1000']
# Split <tredl (train), trecdl (test)>
ap_real_pred_train = ap_real_pred.iloc[:len(ap_qpp_pred)]
ap_real_pred_test = ap_real_pred.iloc[:len(ap_real_pred)]
if k == "100":
y_train = ap_real_pred_train['ap@100']
y_test = ap_real_pred_test['ap@100']
else:
y_train = ap_real_pred_train['ap@1000']
y_test = ap_real_pred_test['ap@1000']
x_train = ap_real_pred_train[qpp_approaches]
#print(x_train.shape)
fold = 0
avg_rmse_best_qpp = 0
avg_rmse_combined_qpp = 0
avg_p_value = 0
avg_kendall = 0
avg_pearson = 0
avg_smare = 0
#print("Splitting into two halves")
for seed in range(15):
kf = KFold(n_splits = 2, shuffle = True, random_state = seed + 2652124)
#kf = RepeatedKFold(n_repeats = 15, n_splits = 2, random_state = 2652124)
combined_qpp_sq_error_list = []
indv_qpp_sq_error_list = []
y_test_list = []
combined_qpp_predicted_y_list = []
indv_qpp_predicted_y_list = []
for train_index, test_index in kf.split(ap_real_pred):
fold += 1
ap_real_pred_train, ap_real_pred_test = ap_real_pred.iloc[train_index], \
ap_real_pred.iloc[test_index]
#print(ap_real_pred_train.shape)
#print(ap_real_pred_test.shape)
#print(f"Fold : {fold}")
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
x_train = ap_real_pred_train[qpp_approaches]
best_qpp_predictor_train = ap_real_pred_train[best_qpp]
best_qpp_predictor_test = ap_real_pred_test[best_qpp]
#print(f"shape of x_train {x_train.shape}")
#print(f"Shape of x_train : {x_train.shape}")
#print(f"Shape of y_train : {y_train.shape}")
# regr_model = regr_model.fit(x_train, y_train)
regr_model = Lars(fit_intercept=False)
row, col = x_train.shape
# TODO: change this
random_predictors = np.random.rand(row, 6) # as mentioned in Hauff et al.
bias_vector = np.ones((row, 1))
x_train = np.hstack((x_train, bias_vector))
x_train = np.hstack((x_train, random_predictors))
#random_predictors = np.random.rand(*x_train.shape)
x_train = np.hstack((x_train, random_predictors))
regr_model = regr_model.fit(x_train, y_train)
#best_qpp_regr_model = ElasticNetCV(cv = 5, random_state = 0)
#best_qpp_regr_model = ElasticNet(alpha=0.001, l1_ratio=0.6, random_state = 0)
#best_qpp_regr_model = LassoCV(cv = 5, random_state = 0)
#best_qpp_regr_model = Lasso(alpha= 0.001, random_state = 0)
#best_qpp_regr_model = Lars(n_nonzero_coefs=1)
# Trying Support Vector Regression
#best_qpp_regr_model = make_pipeline(StandardScaler(), SVR(epsilon=0.2))
#best_qpp_regr_model = make_pipeline(StandardScaler(), NuSVR(C=1, nu=0.1))
#best_qpp_regr_model = KernelRidge(alpha=0.2)
#best_qpp_regr_model = LarsCV(cv=5)
#best_qpp_regr_model = Ridge(alpha=0.8)
#best_qpp_regr_model = RidgeCV(cv = 5)
#best_qpp_regr_model = LassoLars(alpha=1.0)
#best_qpp_regr_model = LassoLarsCV(cv=5)
#best_qpp_regr_model = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])
#best_qpp_regr_model = GammaRegressor()
#best_qpp_regr_model = PoissonRegressor()
best_qpp_regr_model = LinearRegression()
best_qpp_regr_model = best_qpp_regr_model.fit(best_qpp_predictor_train, y_train)
x_test = ap_real_pred_test[qpp_approaches]
#print(f"Shape of best qpp predictor: {best_qpp_predictor_train.shape}")
#exit(1)
#print(x_test.shape)
#weight_vector, intercept = bolasso(x_train, y_train, qpp_approaches, n_bootstraps = 128)
#y_predicted = predict_y_values(weight_vector, x_test, intercept)
weight_vector, intercept = weight_vector_with_traps(regr_model.coef_path_, regr_model.intercept_, len(qpp_approaches)+1)
row, _ = x_test.shape
bias_vector = np.ones((row, 1))
new_x_test = np.hstack((x_test, bias_vector))
y_predicted = predict_y_values(weight_vector, new_x_test, intercept)
#y_predicted = regr_model.predict(x_test)
y_best_qpp_predicted = best_qpp_regr_model.predict(best_qpp_predictor_test)
y_predicted = np.clip(y_predicted, 0, 1)
y_best_qpp_predicted = np.clip(y_best_qpp_predicted, 0, 1)
rmse = root_mean_squared_error(y_test, y_predicted)
#print(rmse, regr_model.score(x_test, y_test))
combined_qpp_predicted_y_list.extend(y_predicted)
combined_qpp_sq_error = squared_error(y_predicted, y_test)
#print(f"combined qpp sq error : {combined_qpp_sq_error}")
#print(f"y_predicted {y_predicted}")
#print(f"y_test values: {y_test}")
# Commenting below as we don't want to print all these for all different folds
#print(f"Predicted y vector: {y_predicted}")
#print(f"Input x_test: {x_test}")
#print(f"Actual y_test value: {y_test}")
#print(f"No. features seen during training : {regr_model.n_features_in_}")
#print(f"Features seen during training : {regr_model.feature_names_in_}")
#print(f"Parameter vector : {regr_model.coef_}")
#print(f"Intercept : {regr_model.intercept_}")
#print(f"Alphas: {regr_model.alphas_}")
#print(f"active : {regr_model.active_}")
#print(f"Alpha: {regr_model.alpha_}")
#print(f"Coeff path: {regr_model.coef_path_}")
combined_qpp_sq_error_list.extend(combined_qpp_sq_error)
#print(f"best qpp approach: {str(best_qpp)}")
#print(root_mean_squared_error(y_test, y_best_qpp_predicted), best_qpp_regr_model.score(best_qpp_predictor_test, y_test))
indv_qpp_sq_error = squared_error(y_test, y_best_qpp_predicted)
#print(f"Shape of indv error : {indv_qpp_sq_error.shape}")
indv_qpp_sq_error_list.extend(indv_qpp_sq_error)
indv_qpp_predicted_y_list.extend(y_best_qpp_predicted)
y_test_list.extend(y_test)
#print("indv QPP squared error: ")
# Perform the t-test
#t_statistic, p_value = ttest_ind(indv_qpp_sq_error, combined_qpp_sq_error)
t_statistic, p_value = ttest_ind(indv_qpp_sq_error, combined_qpp_sq_error, alternative='greater')
#print("t-statistic:", t_statistic)
#print("p-value:", p_value)
#print("t-test and pvalue of the entire list")
#print("Shape of the two lists")
#print(f"Shape of entire list : {len(combined_qpp_sq_error_list)}")
#print(f"Shape of {best_qpp} : {len(indv_qpp_sq_error_list)}")
#t_statistic, p_value = ttest_ind(combined_qpp_sq_error_list, indv_qpp_sq_error_list)
t_statistic, p_value = ttest_ind(indv_qpp_sq_error_list, combined_qpp_sq_error_list, alternative='greater')
#print("In how many cases combind QPP predictor winning : ")
predictor_status = np.array(np.array(indv_qpp_sq_error_list) > np.array(combined_qpp_sq_error_list)).sum()
#print(predictor_status)
#print("t-statistic:", t_statistic)
#print("p-value:", p_value)
avg_rmse_best_qpp += root_mean_squared_error(indv_qpp_predicted_y_list, y_test_list)
avg_rmse_combined_qpp += root_mean_squared_error(combined_qpp_predicted_y_list, y_test_list)
avg_p_value += p_value
corr,_ = stats.kendalltau(combined_qpp_predicted_y_list, y_test_list)
avg_kendall += corr
corr, _ = stats.pearsonr(combined_qpp_predicted_y_list, y_test_list)
avg_pearson += corr
# DONE: add for SMARE too
temp = pd.DataFrame()
temp['ap'] = y_test_list
temp['predictor'] = combined_qpp_predicted_y_list
avg_smare += smare(temp['ap'], temp['predictor'])
#print(f"RMSE of best QPP {best_qpp} : {root_mean_squared_error(indv_qpp_predicted_y_list, y_test_list)}")
#print(f"RMSE of the combined predictor : {root_mean_squared_error(combined_qpp_predicted_y_list, y_test_list)}")
entire_set = fold /2
avg_rmse_best_qpp = avg_rmse_best_qpp / entire_set
avg_rmse_combined_qpp = avg_rmse_combined_qpp / entire_set
avg_p_value = avg_p_value / entire_set
avg_pearson = avg_pearson / entire_set
avg_kendall = avg_kendall / entire_set
# DONE: add for SMARE too
avg_smare = avg_smare / entire_set
print(f"avg. rmse of {best_qpp} qpp : {avg_rmse_best_qpp:>.4f}")
print(f"avg. rmse of combined qpp : {avg_rmse_combined_qpp:>.4f}")
print(f"avg. p-value : {avg_p_value:>.4f}")
print(f"Total number of folds generated {fold}")
print(f"avg. kendall : {avg_kendall:>.4f}")
print(f"avg. pearson : {avg_pearson:>.4f}")
print(f"avg. smare : {avg_smare:>.4f}")
if __name__ == '__main__':
#qpp_approaches = ["MaxIDF", "AvgIDF", "AvQC", "AVQCG", "SumSCQ", "MaxSCQ",
# "AvgSCQ", "SumVAR", "AvgVAR", "MaxVAR", "AvP", "AvNP"]
#qpp_approaches = ["MaxIDF", "AvgIDF", "SumSCQ", "MaxSCQ",
# "AvgSCQ", "SumVAR", "AvgVAR", "MaxVAR", "AvP", "AvNP"]
#qpp_approaches = ["nqc", "wig", "clarity", "uef_nqc", "uef_wig",
# "uef_clarity", "neuralqpp", "qppbertpl", "deepqpp", "bertqpp"]
#for qpp_approach in qpp_approaches:
compute_regression()