Python / Python Mathematical Intuition and Scikit Learn Interview Questions
What is the mathematical intuition behind gradient boosting? How does it differ from random forests?
Gradient boosting builds an ensemble of weak learners (typically shallow decision trees) sequentially, where each new tree is trained to predict the negative gradient of the loss function with respect to the current ensemble's predictions — essentially, each tree learns to correct the errors (residuals, for squared loss) of all previous trees combined. The final prediction is F(x) = F₀(x) + η·Σ hₘ(x), where each hₘ is a tree fit to the current residuals and η is a shrinkage/learning rate.
This is fundamentally different from random forests, which build trees independently and in parallel on bootstrap samples and average them to reduce variance. Gradient boosting builds trees sequentially and dependently — each tree depends on all previous ones — primarily to reduce bias by progressively fitting the parts of the function the ensemble has gotten wrong so far.
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier # Conceptual implementation of gradient boosting for regression (squared loss) import numpy as np from sklearn.tree import DecisionTreeRegressor def simple_gradient_boost(X, y, n_estimators=50, lr=0.1, max_depth=3): F = np.zeros(len(y)) # initial prediction: all zeros trees = [] for m in range(n_estimators): residuals = y - F # negative gradient for squared loss tree = DecisionTreeRegressor(max_depth=max_depth) tree.fit(X, residuals) # fit tree to the residuals F += lr * tree.predict(X) # update ensemble prediction trees.append(tree) return trees, F # scikit-learn's production implementation gb = GradientBoostingClassifier( n_estimators=100, learning_rate=0.1, # shrinkage â smaller values need more trees but generalise better max_depth=3, ) gb.fit(X_train, y_train)
More Related questions...