-
Notifications
You must be signed in to change notification settings - Fork 0
/
boosting.py
57 lines (47 loc) · 1.89 KB
/
boosting.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
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
class SimplifiedBoostingRegressor:
def __init__(self):
pass
@staticmethod
def loss(targets, predictions):
loss = np.mean((targets - predictions)**2)
return loss
@staticmethod
def loss_gradients(targets, predictions):
gradients = 2 * (predictions - targets)
assert gradients.shape == targets.shape
return gradients
def fit(self, model_constructor, data, targets, num_steps=10, lr=0.1, max_depth=5, verbose=False):
'''
Fit sequence of models on the provided data.
Model constructor with no parameters (and with no ()) is passed to this function.
If
example:
boosting_regressor = SimplifiedBoostingRegressor()
boosting_regressor.fit(DecisionTreeRegressor, X, y, 100, 0.5, 10)
'''
new_targets = targets
self.models_list = []
self.lr = lr
self.loss_log = []
for step in range(num_steps):
try:
model = model_constructor(max_depth=max_depth)
except TypeError:
print('max_depth keyword is not found. Ignoring')
model = model_constructor()
self.models_list.append(model.fit(data, new_targets))
predictions = self.predict(data)
self.loss_log.append(self.loss(targets, predictions))
gradients = self.loss_gradients(targets, predictions)
new_targets = - gradients
if verbose:
print('Finished! Loss=', self.loss_log[-1])
return self
def predict(self, data):
predictions = np.zeros(len(data))
for model in self.models_list:
predictions += model.predict(data) * self.lr
return predictions