A comprehensive Python implementation of the Gradient Boosting algorithm with extensive visualization tools.
Gradient Boosting/
├── gradient_boosting/ # Core algorithm package
│ ├── __init__.py
│ ├── gradient_boosting.py # Main GB implementation
│ ├── decision_tree.py # Decision tree for weak learners
│ └── loss_functions.py # Loss functions (MSE, LogLoss)
│
├── visualization/ # Visualization package
│ ├── __init__.py
│ ├── visualizer.py # Main GB visualizations
│ ├── tree_visualizer.py # Tree structure visualization
│ └── performance_visualizer.py # Performance metrics
│
├── example_regression.py # Regression example
├── example_classification.py # Classification example
├── requirements.txt # Dependencies
└── README.md # This file
-
GradientBoostingRegressor
- Implements gradient boosting for regression tasks
- Configurable hyperparameters (n_estimators, learning_rate, max_depth, etc.)
- Tracks training loss across iterations
- Supports subsampling for stochastic gradient boosting
-
GradientBoostingClassifier
- Binary classification with log loss
- Probability predictions
- Staged predictions for visualization
-
DecisionTreeRegressor
- Custom decision tree implementation
- Used as weak learners in the ensemble
- Configurable depth and split criteria
-
Loss Functions
- SquaredLoss for regression
- LogLoss for binary classification
- Gradient computation for optimization
-
GradientBoostingVisualizer
- Training loss curves
- Staged predictions (1D and 2D)
- Residuals evolution
- Feature importance plots
- Animated boosting process
-
TreeVisualizer
- Visual tree structure diagrams
- Text-based tree printing
- Node and edge annotations
-
PerformanceVisualizer
- Predictions vs actual values
- Residual analysis
- Confusion matrices
- ROC curves
- Learning curves
pip install -r requirements.txtRequired packages:
- numpy
- matplotlib
- scikit-learn (for metrics and example data)
from gradient_boosting import GradientBoostingRegressor
from visualization import GradientBoostingVisualizer
# Create and train model
model = GradientBoostingRegressor(
n_estimators=50,
learning_rate=0.1,
max_depth=3
)
model.fit(X_train, y_train)
# Visualize
viz = GradientBoostingVisualizer(model)
viz.plot_training_loss()
viz.plot_staged_predictions_1d(X_train, y_train)from gradient_boosting import GradientBoostingClassifier
from visualization import PerformanceVisualizer
# Create and train model
model = GradientBoostingClassifier(
n_estimators=50,
learning_rate=0.1,
max_depth=3
)
model.fit(X_train, y_train)
# Visualize performance
perf_viz = PerformanceVisualizer(model, X_train, y_train, X_test, y_test)
perf_viz.plot_confusion_matrix()
perf_viz.plot_roc_curve()# Run regression example
python example_regression.py
# Run classification example
python example_classification.pyGradient Boosting builds an ensemble of weak learners (decision trees) sequentially:
- Initialize with a constant prediction (mean for regression, log-odds for classification)
- For each iteration:
- Calculate the negative gradient (residuals)
- Fit a decision tree to the residuals
- Update predictions by adding the scaled tree prediction
- Final prediction is the sum of all tree predictions
Key equation:
F_m(x) = F_{m-1}(x) + η * h_m(x)
Where:
- F_m(x): prediction at stage m
- η: learning rate
- h_m(x): new tree fitted to residuals
Shows how the loss decreases with each boosting iteration.
- 1D: Shows prediction curve evolution
- 2D: Shows decision boundary evolution with contour plots
Histograms showing how residuals shrink over iterations.
Bar chart showing which features are most frequently used for splits.
- Predictions vs Actual scatter plots
- Residual plots
- Confusion matrices
- ROC curves
- Learning curves (train vs test)
Visual diagram of individual decision trees with:
- Decision nodes (feature + threshold)
- Leaf nodes (prediction values)
- Split paths
n_estimators: Number of boosting stages (trees)learning_rate: Shrinkage parameter (0 < η ≤ 1)max_depth: Maximum depth of individual treesmin_samples_split: Minimum samples to split a nodemin_samples_leaf: Minimum samples at leaf nodessubsample: Fraction of samples for stochastic boosting
Extend the loss function base class:
class CustomLoss:
def __call__(self, y_true, y_pred):
# Calculate loss
pass
def gradient(self, y_true, y_pred):
# Calculate negative gradient
pass
def init_estimate(self, y):
# Initial prediction
pass- This is an educational implementation focusing on clarity
- For production use, consider scikit-learn's GradientBoostingRegressor/Classifier
- Visualizations work best with small to medium datasets
- 1D visualizations require single-feature data
- 2D visualizations require two-feature data
This project is open source and available for educational purposes.
Author: Maria Hadi
Date: November 2025
Version: 1.0.0