Generate code for sklearn's GradientBoostingClassifier
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
GradientBoostingClassifier from scikit-learn builds an ensemble of decision trees sequentially, where each tree corrects the errors of the previous ones. It is effective for tabular classification tasks and offers built-in feature importance. Key hyperparameters are n_estimators (number of trees), learning_rate (contribution of each tree), and max_depth (tree complexity). This article provides complete, runnable code for training, evaluating, and tuning a GradientBoostingClassifier.
Basic Training and Prediction
The default parameters work well for many datasets. n_estimators=100 builds 100 sequential trees, each correcting residual errors from the previous ensemble.
Key Hyperparameters
Lower learning_rate with higher n_estimators gives better generalization but slower training. subsample < 1.0 introduces randomness (stochastic gradient boosting), reducing overfitting.
Hyperparameter Tuning with GridSearchCV
GridSearchCV tries all parameter combinations with cross-validation. For large grids, use RandomizedSearchCV to sample a subset.
Feature Importance
feature_importances_ ranks features by their contribution to reducing the loss function across all trees.
Training with Early Stopping
Early stopping monitors validation loss and stops training when it plateaus, preventing overfitting and saving computation time.
Staged Prediction (Learning Curve)
staged_predict_proba yields predictions at each boosting stage, letting you visualize when the model starts overfitting.
Pipeline with Preprocessing
Common Pitfalls
- Training too many estimators without early stopping: GradientBoosting can overfit with too many trees. Use
n_iter_no_changeandvalidation_fractionto stop training automatically when validation loss stops improving. - Learning rate too high: A high
learning_rate(e.g., 0.5+) makes each tree contribute too much, causing overfitting. Lower values (0.01-0.1) with more estimators give better generalization. - Slow training on large datasets: GradientBoosting trains trees sequentially and does not parallelize (
n_jobshas no effect). For large datasets, considerHistGradientBoostingClassifier(sklearn) or XGBoost/LightGBM, which are much faster. - Not scaling features: GradientBoosting uses decision trees, which are scale-invariant. Scaling features is not required (unlike logistic regression or SVM). Adding unnecessary scaling wastes preprocessing time.
- Ignoring class imbalance: GradientBoosting does not handle imbalanced classes by default. Use
sample_weightinfit()or adjust class weights manually for imbalanced datasets.
Summary
GradientBoostingClassifierbuilds sequential trees that correct each other's errors- Key parameters:
n_estimators,learning_rate,max_depth,subsample - Use lower
learning_ratewith highern_estimatorsfor better generalization - Enable early stopping with
n_iter_no_changeto prevent overfitting - Use
staged_predict_probato visualize the learning curve and detect overfitting - For large datasets, prefer
HistGradientBoostingClassifieror XGBoost for faster training
Related reading
- Generating confidence interval for precision recall curve
- Generating random integers in TensorFlow
- Generative adversarial networks tanh?
- generative models with tensorflow's tpu_estimator?
- Generate random permutation of huge list in Python
- Generate UUID for Cassandra in Python
- Genetic algorithm and Tetris
- Genetic algorithm resource
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.