Last active
June 6, 2024 16:54
-
-
Save tomonori-masui/c756edea4bfeee754935eec71cc3c662 to your computer and use it in GitHub Desktop.
CustomGradientBoostingRegressor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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