In this section, we look into practical examples of hyperparameter tuning for gradient boosting, providing you with the tools and techniques to improve your models. We will work with a real dataset, demonstrate the effects of various hyperparameters, and guide you through the process of systematically finding the optimal configuration.
For our examples, we'll utilize the well-known "California Housing" dataset. This dataset provides information on various housing features in California and is commonly used for regression tasks. The objective is to predict median house values based on features such as median income, average house occupancy, and proximity to the ocean.
Before getting into tuning, let's load our data and split it into training and test sets:
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
# Load the dataset
housing = fetch_california_housing()
X, y = housing.data, housing.target
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
We'll start by training a basic gradient boosting model using scikit-learn
. This will serve as our baseline for comparison after hyperparameter tuning.
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
# Initialize and fit the model
gbm = GradientBoostingRegressor(random_state=42)
gbm.fit(X_train, y_train)
# Evaluate the model
y_pred = gbm.predict(X_test)
baseline_mse = mean_squared_error(y_test, y_pred)
print(f"Baseline MSE: {baseline_mse:.4f}")
Now, let's improve the model's performance by tuning its hyperparameters using grid search. We'll focus on three important hyperparameters: learning_rate
, n_estimators
, and max_depth
.
from sklearn.model_selection import GridSearchCV
# Define the parameter grid
param_grid = {
'learning_rate': [0.01, 0.05, 0.1],
'n_estimators': [100, 200, 300],
'max_depth': [3, 4, 5]
}
# Initialize the grid search
grid_search = GridSearchCV(
estimator=gbm,
param_grid=param_grid,
cv=3,
scoring='neg_mean_squared_error',
n_jobs=-1,
verbose=2
)
# Fit the grid search
grid_search.fit(X_train, y_train)
# Best parameters and score
print("Best parameters found: ", grid_search.best_params_)
print("Best cross-validation score (MSE): ", -grid_search.best_score_)
Impact of learning rate and number of estimators on model performance
Impact of max depth and number of estimators on model performance
The grid search provides the optimal hyperparameters based on the specified grid. Here, we can observe how the choice of learning_rate
, n_estimators
, and max_depth
impacts the model's performance:
n_estimators
) to converge, but it can lead to a more robust and accurate model.Using the best parameters from our grid search, we can now evaluate the improved model on the test set.
# Retrieve the best model
best_gbm = grid_search.best_estimator_
# Predict and evaluate on the test set
y_pred_best = best_gbm.predict(X_test)
tuned_mse = mean_squared_error(y_test, y_pred_best)
print(f"Tuned MSE: {tuned_mse:.4f}")
Hyperparameter tuning can significantly boost the performance of gradient boosting models. By carefully selecting the learning rate, number of estimators, and maximum depth, among other parameters, you can tailor the model to better fit your data and improve its predictive accuracy. This hands-on approach should provide you with a solid understanding of how to apply these techniques in your own projects.
By mastering the techniques of hyperparameter tuning, you can help gradient boosting models handle the complexities of real-world datasets more effectively.
© 2025 ApX Machine Learning