This project implements a Gradient Boosting Machine (GBM) for regression tasks completely from first principles using Python and NumPy. It avoids using high-level boosting libraries (like XGBoost or LightGBM) to demonstrate a deep understanding of the underlying algorithms.
The model is trained and evaluated on the Boston Housing Dataset, using Decision Trees as weak learners to minimize Mean Squared Error (MSE) via gradient descent in function space.
-
Custom Implementation: Core boosting logic (
fit,predict) implemented manually in theGradientBoostingRegressorScratchclass. -
Hyperparameter Tuning: Supports configuration of:
-
n_estimators(Number of boosting stages) -
learning_rate(Shrinkage parameter to prevent overfitting) -
max_depth(Complexity of individual weak learners)
-
-
Loss Function: Optimization based on Squared Error Loss (
$L = \frac{1}{2}(y - \hat{y})^2$ ). - Robust Data Pipeline: Handles the deprecated Boston Housing dataset by fetching directly from the CMU StatLib repository.
- Language: Python 3.x
- Core Logic: NumPy (Matrix operations), Pandas (Data handling)
- Base Learner: Scikit-learn (
DecisionTreeRegressorused only as the weak learner) - Visualization: Matplotlib (Training curves and residual plots)
Gradient-Boosting-Machine/
├── gbm_model.py # Core class library containing the GBM algorithm
├── train_eval.py # Script to load data, train model, and generate plots
├── README.md # Project documentation
├── 1_Training_Loss_Curve.png # (Generated) Loss minimization visualization
├── 2_Actual_vs_Predicted.png # (Generated) Prediction scatter plot
├── 3_Residuals_Distribution.png # (Generated) Error distribution analysis
└── 4_LR_Comparison.png # (Generated) Hyperparameter impact analysis
pip install numpy pandas matplotlib scikit-learn
Execute the main script to train the model and generate performance reports:
python train_eval.py
You can import the class and use it just like a Scikit-learn estimator:
from gbm_model import GradientBoostingRegressorScratch
# Initialize
model = GradientBoostingRegressorScratch(
n_estimators=200,
learning_rate=0.1,
max_depth=3
)
# Train
model.fit(X_train, y_train)
# Predict
predictions = model.predict(X_test)On the held-out test set (20% split), the model achieves excellent convergence:
- Test RMSE: 2.4525 (Root Mean Squared Error)
- Test R²: 0.9180 (Coefficient of Determination)
- Train RMSE: 0.8274
The script automatically generates the following insights:
- Training Loss Curve: Verifies that the MSE decreases with each boosting iteration.
- Residual Analysis: Confirms errors are normally distributed (validating regression assumptions).
- Learning Rate Comparison: Demonstrates the trade-off between convergence speed and stability.