-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
218 lines (174 loc) · 7.4 KB
/
utils.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
import math
import numpy as np
import pandas as pd
pd.options.display.float_format = '{:.2f}'.format
from tqdm import tqdm
import cudf
# Global variables
K = 4
BASE = 10
SCALE = 400
def initialize_vh_vo(
model_name_sorted, competition_list, SCALE=400, BASE=10, INIT_RATING=1000, ptbl_win=None, classifier=False
):
from sklearn.linear_model import LogisticRegression
pd_new = []
for _ in range(len(model_name_sorted)):
pd_new.append([0] * len(model_name_sorted))
if ptbl_win is None:
ptbl_win = pd.DataFrame(pd_new, index=model_name_sorted, columns=model_name_sorted)
for item in tqdm(competition_list):
# print(key)
model_a = item['model_a']
model_b = item['model_b']
winner = item['winner']
# winner = df.loc[key, 'winner']
if winner == 'model_a':
ptbl_win.loc[model_a, model_b] = ptbl_win.loc[model_a, model_b] + 2
elif winner == 'model_b':
ptbl_win.loc[model_b, model_a] = ptbl_win.loc[model_b, model_a] + 2
elif 'tie' in winner:
ptbl_win.loc[model_a, model_b] = ptbl_win.loc[model_a, model_b] + 1
ptbl_win.loc[model_b, model_a] = ptbl_win.loc[model_b, model_a] + 1
models = pd.Series(np.arange(len(ptbl_win.index)), index=ptbl_win.index)
p = len(models)
X = np.zeros([p * (p - 1) * 2, p])
Y = np.zeros(p * (p - 1) * 2)
cur_row = 0
sample_weights = []
for m_a in ptbl_win.index:
for m_b in ptbl_win.columns:
if m_a == m_b:
continue
# if nan skip
if math.isnan(ptbl_win.loc[m_a, m_b]) or math.isnan(ptbl_win.loc[m_b, m_a]):
continue
X[cur_row, models[m_a]] = +math.log(BASE)
X[cur_row, models[m_b]] = -math.log(BASE)
Y[cur_row] = 1.0
sample_weights.append(ptbl_win.loc[m_a, m_b])
X[cur_row + 1, models[m_a]] = math.log(BASE)
X[cur_row + 1, models[m_b]] = -math.log(BASE)
Y[cur_row + 1] = 0.0
sample_weights.append(ptbl_win.loc[m_b, m_a])
cur_row += 2
X = X[:cur_row]
Y = Y[:cur_row]
if classifier:
np.save('data/data_x_classifier.npy', X)
np.save('data/data_y_classifier.npy', Y)
ptbl_win.to_csv('data/vh_win_matrix_classifier.csv')
else:
np.save('data/data_x.npy', X)
np.save('data/data_y.npy', Y)
ptbl_win.to_csv('data/vh_win_matrix.csv')
lr = LogisticRegression(fit_intercept=False, penalty=None, tol=1e-6, warm_start=True)
lr.fit(X, Y, sample_weight=sample_weights)
elo_scores = SCALE * lr.coef_[0] + INIT_RATING
return pd.Series(elo_scores, index=models.index).sort_values(ascending=False), ptbl_win
def preety_print_model_ratings(ratings):
df = pd.DataFrame([
[n, ratings[n]] for n in ratings.keys()
], columns=["Model", "Elo rating"]).sort_values("Elo rating", ascending=False).reset_index(drop=True)
df.index = df.index + 1
return df
def preprocess_data(X_initial_path, Y_initial_path, win_matrix_initial_path):
X_initial = np.load(X_initial_path)
Y_initial = np.load(Y_initial_path)
win_matrix_initial = pd.read_csv(win_matrix_initial_path, index_col=0)
sample_weights = []
for m_a in win_matrix_initial.index:
for m_b in win_matrix_initial.columns:
if m_a == m_b:
continue
if math.isnan(win_matrix_initial.loc[m_a, m_b]) or math.isnan(win_matrix_initial.loc[m_b, m_a]):
continue
sample_weights.append(win_matrix_initial.loc[m_a, m_b])
sample_weights.append(win_matrix_initial.loc[m_b, m_a])
sample_weights = cudf.Series(sample_weights)
X_initial = cudf.DataFrame(X_initial)
Y_initial = cudf.Series(Y_initial)
return X_initial, Y_initial, win_matrix_initial, sample_weights
def compute_mle_elo_dict(
competition_list, X, Y, SCALE=400, INIT_RATING=1000, ptbl_win=None, sample_weights=None
):
from cuml.linear_model import LogisticRegression
for item in competition_list:
model_a = item['model_a']
model_b = item['model_b']
winner = item['winner']
count = 0
for m_a in ptbl_win.index:
for m_b in ptbl_win.columns:
if m_a == m_b:
continue
if winner == 'model_a':
if model_a == m_a and model_b == m_b:
sample_weights[count] += 2
elif model_b == m_a and model_a == m_b:
sample_weights[count+1] += 2
elif winner == 'model_b':
if model_a == m_a and model_b == m_b:
sample_weights[count+1] += 2
elif model_b == m_a and model_a == m_b:
sample_weights[count] += 2
elif 'tie' in winner:
if model_b == m_a and model_a == m_b:
sample_weights[count] += 1
sample_weights[count+1] += 1
if model_a == m_a and model_b == m_b:
sample_weights[count] += 1
sample_weights[count+1] += 1
count += 2
models = pd.Series(np.arange(len(ptbl_win.index)), index=ptbl_win.index)
lr = LogisticRegression(fit_intercept=False, penalty=None, tol=1e-6)
lr.fit(X, Y, sample_weight=sample_weights)
elo_scores = SCALE * lr.coef_.to_numpy()[0] + INIT_RATING
return pd.Series(elo_scores, index=models.index).sort_values(ascending=False), sample_weights
def get_rank(ranking, target_model):
for idx, key in enumerate(ranking['Model']):
if key == target_model:
return idx + 1
def get_sample_weight(model, sampling_weights):
weight = sampling_weights.get(model, 0)
return weight
def get_battle_pair(models, sampling_weights):
if len(models) == 1:
return models[0], models[0]
model_weights = []
for model in models:
weight = get_sample_weight(
model, sampling_weights
)
model_weights.append(weight)
total_weight = np.sum(model_weights)
model_weights = model_weights / total_weight
chosen_idx = np.random.choice(len(models), p=model_weights)
chosen_model = models[chosen_idx]
rival_models = []
rival_weights = []
for model in models:
if model == chosen_model:
continue
weight = get_sample_weight(model, sampling_weights)
rival_models.append(model)
rival_weights.append(weight)
rival_weights = rival_weights / np.sum(rival_weights)
rival_idx = np.random.choice(len(rival_models), p=rival_weights)
rival_model = rival_models[rival_idx]
return chosen_model, rival_model
def get_battle_prob(model_a, model_b, models, sampling_weights):
model_weights = []
for model in models:
weight = get_sample_weight(
model, sampling_weights
)
if model == model_a:
target_weight_a = weight
if model == model_b:
target_weight_b = weight
model_weights.append(weight)
total_weight = np.sum(model_weights)
prob_a = target_weight_a / total_weight * target_weight_b / (total_weight-target_weight_a)
prob_b = target_weight_b / total_weight * target_weight_a / (total_weight-target_weight_b)
return prob_a + prob_b