Skip to content

Instantly share code, notes, and snippets.

@tomonori-masui
Last active June 6, 2024 16:54
Show Gist options
  • Save tomonori-masui/c756edea4bfeee754935eec71cc3c662 to your computer and use it in GitHub Desktop.
Save tomonori-masui/c756edea4bfeee754935eec71cc3c662 to your computer and use it in GitHub Desktop.
CustomGradientBoostingRegressor
class CustomGradientBoostingRegressor:
def __init__(self, learning_rate, n_estimators, max_depth=1):
self.learning_rate = learning_rate
self.n_estimators = n_estimators
self.max_depth = max_depth
self.trees = []
def fit(self, X, y):
self.F0 = y.mean()
Fm = self.F0
for _ in range(self.n_estimators):
r = y - Fm
tree = DecisionTreeRegressor(max_depth=self.max_depth, random_state=0)
tree.fit(X, r)
gamma = tree.predict(X)
Fm += self.learning_rate * gamma
self.trees.append(tree)
def predict(self, X):
Fm = self.F0
for i in range(self.n_estimators):
Fm += self.learning_rate * self.trees[i].predict(X)
return Fm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment