-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgbm_model.py
More file actions
43 lines (33 loc) · 1.36 KB
/
Copy pathgbm_model.py
File metadata and controls
43 lines (33 loc) · 1.36 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
import numpy as np
from sklearn.tree import DecisionTreeRegressor
class GradientBoostingRegressorScratch:
"""
Gradient Boosting Regressor implemented from scratch.
Loss Function: Mean Squared Error (MSE)
Gradient: Negative Residuals (y - y_pred)
"""
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.max_depth = max_depth
self.trees = []
self.loss_history = []
self.initial_prediction = None
def fit(self, X, y):
self.initial_prediction = np.mean(y)
y_pred = np.full(y.shape, self.initial_prediction)
for i in range(self.n_estimators):
residuals = y - y_pred
tree = DecisionTreeRegressor(max_depth=self.max_depth, random_state=42)
tree.fit(X, residuals)
self.trees.append(tree)
update = tree.predict(X)
y_pred += self.learning_rate * update
mse = np.mean((y - y_pred) ** 2)
self.loss_history.append(mse)
return self
def predict(self, X):
y_pred = np.full(X.shape[0], self.initial_prediction)
for tree in self.trees:
y_pred += self.learning_rate * tree.predict(X)
return y_pred