Skip to content

Instantly share code, notes, and snippets.

@tomonori-masui
Last active February 5, 2022 06:10
Show Gist options
  • Select an option

  • Save tomonori-masui/136b16ef296977385b5bc5b244870aef to your computer and use it in GitHub Desktop.

Select an option

Save tomonori-masui/136b16ef296977385b5bc5b244870aef to your computer and use it in GitHub Desktop.
CustomGradientBoostingClassifier
class CustomGradientBoostingClassifier:
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):
F0 = np.log(y.mean()/(1-y.mean())) # log-odds values
self.F0 = np.full(len(y), F0) # converting to array with the input length
Fm = self.F0.copy()
for _ in range(self.n_estimators):
p = np.exp(Fm) / (1 + np.exp(Fm)) # converting back to probabilities
r = y - p # residuals
tree = DecisionTreeRegressor(max_depth=self.max_depth, random_state=0)
tree.fit(X, r)
ids = tree.apply(x) # getting the terminal node IDs
# looping through the terminal nodes
for j in np.unique(ids):
fltr = ids == j
# getting gamma using the formula (Σresiduals/Σp(1-p))
num = r[fltr].sum()
den = (p[fltr]*(1-p[fltr])).sum()
gamma = num / den
# updating the prediction
Fm[fltr] += self.learning_rate * gamma
# replacing the prediction value in the tree
tree.tree_.value[j, 0, 0] = gamma
self.trees.append(tree)
def predict_proba(self, X):
Fm = self.F0
for i in range(self.n_estimators):
Fm += self.learning_rate * self.trees[i].predict(X)
return np.exp(Fm) / (1 + np.exp(Fm)) # converting back into probabilities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment