-
Notifications
You must be signed in to change notification settings - Fork 38
/
XGBRegressor.py
192 lines (167 loc) · 6.43 KB
/
XGBRegressor.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
import numpy as np
import abc
from sklearn import datasets,tree
from sklearn.metrics import mean_squared_error
import xgboost as xgb
np.random.seed(1)
class LossBase(object):
def __init__(self,y_target,y_pred):
self.y_target=y_target
self.y_pred=y_pred
pass
@abc.abstractmethod
def forward(self):
raise NotImplementedError
@abc.abstractmethod
def g(self):
raise NotImplementedError
@abc.abstractmethod
def h(self):
raise NotImplementedError
class MSELoss(LossBase):
def __init__(self,y_target,y_pred):
super(MSELoss,self).__init__(y_target,y_pred)
def forward(self):
return (self.y_target-self.y_pred)**2
def g(self):
return 2*(self.y_pred-self.y_target)
def h(self):
return 2*np.ones_like(self.y_target)
class CART:
def __init__(self, reg_lambda=1, gamma=0., max_depth=3,col_sample_ratio=0.5,row_sample_ratio=1.):
self.reg_lambda=reg_lambda
self.gamma=gamma
self.max_depth=max_depth
self.tree = None
self.leaf_nodes=0
self.obj_val=0.
self.col_sample_ratio=col_sample_ratio
self.row_sample_ratio=row_sample_ratio
def fit(self, X, y,g,h):
D = {}
D['X'] = X
D['y'] = y
A = np.arange(X.shape[1])
m=len(y)
self.tree = self.TreeGenerate(D,A,g,h,np.array(range(m)),0)
self.obj_val=-0.5*self.obj_val+self.gamma*self.leaf_nodes
def predict(self, X):
if self.tree is None:
raise RuntimeError('cant predict before fit')
y_pred = []
for i in range(X.shape[0]):
tree = self.tree
x = X[i]
while True:
if not isinstance(tree, dict):
y_pred.append(tree)
break
a = list(tree.keys())[0]
tree = tree[a]
if isinstance(tree, dict):
val = x[a]
split_val=float(list(tree.keys())[0][1:])
if val<=split_val:
tree=tree[list(tree.keys())[0]]
else:
tree=tree[list(tree.keys())[1]]
else:
y_pred.append(tree)
break
return np.array(y_pred)
def TreeGenerate(self, D, A,g,h,indices,depth):
X = D['X']
if depth>self.max_depth:
G=np.sum(g[indices])
H=np.sum(h[indices])
w=-(G/(H+self.reg_lambda))
self.obj_val+=(G**2/(H+self.reg_lambda))
self.leaf_nodes+=1
return w
split_j=None
split_s=None
max_gain=0.
col_sample_indices=np.random.choice(A,size=int(len(A)*self.col_sample_ratio))
indices=np.random.choice(indices,size=int(len(indices)*self.row_sample_ratio))
for j in A:
if j not in col_sample_indices:
continue
for s in np.unique(X[:,j]):
tmp_left=np.where(X[indices,j]<=s)[0]
tmp_right=np.where(X[indices,j]>s)[0]
if len(tmp_left)<1 or len(tmp_right)<1:
continue
left_indices=indices[tmp_left]
right_indices=indices[tmp_right]
G_L=np.sum(g[left_indices])
G_R=np.sum(g[right_indices])
H_L=np.sum(h[left_indices])
H_R=np.sum(h[right_indices])
gain= (G_L ** 2 / (H_L + self.reg_lambda) + G_R ** 2 / (H_R + self.reg_lambda) - (G_L + G_R) ** 2 / (H_L + H_R + self.reg_lambda)) - self.gamma
if gain>max_gain:
split_j=j
split_s=s
max_gain=gain
if split_j is None:
G = np.sum(g[indices])
H = np.sum(h[indices])
w = -(G / (H + self.reg_lambda))
self.obj_val += (G ** 2 / (H + self.reg_lambda))
self.leaf_nodes += 1
return w
tree = {split_j: {}}
left_indices=indices[np.where(X[indices,split_j]<=split_s)[0]]
right_indices=indices[np.where(X[indices,split_j]>split_s)[0]]
tree[split_j]['l'+str(split_s)]=self.TreeGenerate(D,A,g,h,left_indices,depth+1)
tree[split_j]['r'+str(split_s)]=self.TreeGenerate(D,A,g,h,right_indices,depth+1)
# 当前节点值
tree[split_j]['val']= -(np.sum(g[indices]) / (np.sum(h[indices]) + self.reg_lambda))
return tree
"""
使用MSELoss
按照陈天奇的xgboost PPT实现
"""
class XGBRegressor:
def __init__(self, reg_lambda=1, gamma=0., max_depth=5, n_estimators=250, eta=.1):
self.reg_lambda=reg_lambda
self.gamma=gamma
self.max_depth=max_depth
self.n_estimators=n_estimators
self.eta=eta
self.mean=None
self.estimators_=[]
def fit(self,X,y):
self.mean=np.mean(y)
y_pred = np.ones_like(y)*self.mean
loss = MSELoss(y, y_pred)
g, h = loss.g(), loss.h()
for t in range(self.n_estimators):
estimator_t=CART(self.reg_lambda, self.gamma, self.max_depth)
y_target=y-y_pred
estimator_t.fit(X,y_target,g,h)
self.estimators_.append(estimator_t)
y_pred+=(self.eta*estimator_t.predict(X))
loss=MSELoss(y,y_pred)
g,h=loss.g(),loss.h()
def predict(self,X):
y_pred=np.ones((X.shape[0],))*self.mean
for t in range(self.n_estimators):
y_pred+=(self.eta*self.estimators_[t].predict(X))
return y_pred
if __name__=='__main__':
breast_data = datasets.load_boston()
X, y = breast_data.data, breast_data.target
X_train, y_train = X[:400], y[:400]
X_test, y_test = X[400:], y[400:]
sklearn_decisiontree_reg=tree.DecisionTreeRegressor(min_samples_split=15, min_samples_leaf=5,random_state=False)
sklearn_decisiontree_reg.fit(X_train, y_train)
decisiontree_pred=sklearn_decisiontree_reg.predict(X_test)
print('base estimator:',mean_squared_error(y_test,decisiontree_pred))
tinyml_gbdt_reg=XGBRegressor(n_estimators=100,max_depth=3,gamma=0.)
tinyml_gbdt_reg.fit(X_train, y_train)
y_pred=tinyml_gbdt_reg.predict(X_test)
print('tinyml mse:',mean_squared_error(y_test,y_pred))
xgb_reg=xgb.sklearn.XGBRegressor(max_depth=3,learning_rate=0.1,n_estimators=100,gamma=0,reg_lambda=1)
xgb_reg.fit(X_train,y_train)
xgb_pred=xgb_reg.predict(X_test)
print('xgb mse:',mean_squared_error(y_test,xgb_pred))