Skip to content

Instantly share code, notes, and snippets.

@mitallast
Created April 16, 2016 12:26
Show Gist options
  • Select an option

  • Save mitallast/fcb11c63bb0233219b62eda397ad6f02 to your computer and use it in GitHub Desktop.

Select an option

Save mitallast/fcb11c63bb0233219b62eda397ad6f02 to your computer and use it in GitHub Desktop.
# init
# L - error function
def gbm_L(y, z):
return (y-z)**2
# derivative of L function without coefficient `2`
def dbm_L_der(y, z):
# ignore coefficient 2
return y - z
# number of trees
J = 50
# tree coefficient
coef = 0.9
base_algorithms_list = []
coefficients_list = []
# predict function
def gbm_predict(X):
return [np.sum([coeff * algo.predict([x])[0] for algo, coeff in zip(base_algorithms_list, coefficients_list)]) for x in X]
# train $$a_0$$ as simple DecisionTreeRegressor
b = DecisionTreeRegressor(max_depth=5 ,random_state=42)
b.fit(X_train,y_train)
base_algorithms_list.append(b)
coefficients_list.append(coef)
for j in xrange(1, J):
# compute shift $$s = a_(N-1)(x_i) - a_j(x_i)$$
b = DecisionTreeRegressor(max_depth=5 ,random_state=42)
b.fit(X_train,y_train - gbm_predict(X_train))
base_algorithms_list.append(b)
coefficients_list.append(coef / (1.0 + j))
# test trained GBT with RMSE metric
print np.sqrt(mean_squared_error(y_test, gbm_predict(X_test)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment