-
Notifications
You must be signed in to change notification settings - Fork 0
/
xgb_utils.py
161 lines (119 loc) · 4.7 KB
/
xgb_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
import numpy as np
from sklearn import ensemble
import warnings
import numpy as np
from sklearn.datasets import load_diabetes
from scipy.special import softmax
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import torch
def eval_rmse_score(pred, ground):
rmse = np.sqrt(np.mean(np.square(pred-ground)))
return rmse
def eval_accuracy_score(pred, ground):
pred_labels = np.argmax(pred, axis=1)
correct = np.sum(pred_labels==ground.squeeze())
total = pred_labels.size
return correct/total
def eval_auc_score(pred, ground):
probs = softmax(pred, axis=1)
label = ground.squeeze()
auc = roc_auc_score(label, probs[:,1])
return auc
def xgboost_regressor_binary_decoder_v2(X):
X_np = np.array(X)
if X_np.ndim == 2:
X_np = np.squeeze(X_np)
log_alpha = X_np[0]
log_ccpa = X_np[1]
log_subsample = X_np[2]
log_max_features = X_np[3]
log_learning_rate = X_np[4]
alpha = np.power(10, log_alpha)
if alpha >= 1.0:
warnings.warn("subsample larger than 1.0, val = "+str(alpha))
alpha=1.0-1e-3
ccpa = np.power(10, log_ccpa)
subsample = np.power(10, log_subsample)
if subsample > 1.0:
warnings.warn("subsample larger than 1.0, val = "+str(subsample))
subsample=1.0
max_features = np.power(10, log_max_features)
if max_features > 1.0:
warnings.warn("max_features larger than 1.0, val = "+str(max_features))
max_features=1.0
learning_rate = np.power(10, log_learning_rate)
if learning_rate > 1.0:
warnings.warn("max_features larger than 1.0, val = "+str(learning_rate))
learning_rate=1.0
xgb_config = {}
xgb_config['alpha'] = alpha
xgb_config['ccpa'] = ccpa
xgb_config['subsample'] = subsample
xgb_config['max_features'] = max_features
xgb_config['learning_rate'] = learning_rate
return xgb_config
def wrap_xgb_regressor_params(xgb_config, n_boosters):
params = {'n_estimators': n_boosters,
'loss': 'huber',
'alpha': xgb_config['alpha'],
'ccp_alpha': xgb_config['ccpa'],
'subsample': xgb_config['subsample'],
'max_features': xgb_config['max_features'],
'criterion': 'friedman_mse',
'learning_rate': xgb_config['learning_rate'],
'n_iter_no_change':10}
return params
def eval_xgb_regressor_performance(domain, designs, max_boosters):
#if X.ndim == 2:
# X = X.squeeze()
Xtr, ytr = domain.get_data(train=True, normalize=True)
Xte, _ = domain.get_data(train=False, normalize=True)
score = torch.zeros(len(designs))
for i, x in enumerate(designs):
xgb_config = xgboost_regressor_binary_decoder_v2(x)
params = wrap_xgb_regressor_params(xgb_config, max_boosters)
xgb_regressor = ensemble.GradientBoostingRegressor(**params)
xgb_regressor.fit(Xtr, ytr.squeeze())
pred = xgb_regressor.predict(Xte)
score[i] = domain.metric(pred)
return score
class DiabetesDomain:
def __init__(self, partition_ratio, partition_seed):
diabetes = load_diabetes()
X, y = diabetes.data, diabetes.target
self.Xtr, self.Xte, self.ytr, self.yte = train_test_split(
X, y, test_size=partition_ratio, random_state=partition_seed)
self.ytr = self.ytr.reshape([-1,1])
self.yte = self.yte.reshape([-1,1])
def get_data(self,train=True, normalize=False):
if train:
X = self.Xtr
y = self.ytr
else:
X = self.Xte
y = self.yte
if normalize:
scaler_X = preprocessing.StandardScaler().fit(self.Xtr)
scaler_y = preprocessing.StandardScaler().fit(self.ytr)
X = scaler_X.transform(X)
y = scaler_y.transform(y)
return X, y
def metric(self, pred, normalize=True, torch_tensor=False):
if pred.ndim == 1:
pred = pred.reshape([-1,1])
if torch_tensor:
pred = pred.data.cpu().numpy()
scaler_y = preprocessing.StandardScaler().fit(self.ytr)
if normalize:
pred = scaler_y.inverse_transform(pred)
score = eval_rmse_score(pred, self.yte)
score = score / scaler_y.scale_
return - float(score)
def DiabetesFunctional(max_iters):
domain = DiabetesDomain(partition_ratio=0.33, partition_seed=27)
def func(binary_code):
score = eval_xgb_regressor_performance(domain, binary_code, max_iters)
return score
return func