Hyperparameter tuning is a crucial step in optimizing machine learning models for best performance. In this example, we’ll demonstrate how to use scikit-learn’s GridSearchCV
to perform hyperparameter tuning for HistGradientBoostingClassifier
, a powerful algorithm for classification tasks.
Grid search is a method for evaluating different combinations of model hyperparameters to find the best performing configuration. It exhaustively searches through a specified parameter grid, trains and evaluates the model for each combination using cross-validation, and selects the hyperparameters that yield the best performance metric.
HistGradientBoostingClassifier
is a gradient boosting algorithm that uses histograms to speed up training on large datasets. It builds an ensemble of decision trees, where each tree attempts to correct the errors of the previous ones, leading to improved model accuracy.
The key hyperparameters for HistGradientBoostingClassifier
include the learning_rate
, which controls the contribution of each tree to the overall model; max_iter
, the number of boosting iterations; and max_leaf_nodes
, the maximum number of leaf nodes in each tree, which affects the complexity of the model.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import HistGradientBoostingClassifier
# Generate synthetic classification dataset
X, y = make_classification(n_samples=100, n_features=20, n_informative=10, n_redundant=10, random_state=42)
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define parameter grid
param_grid = {
'learning_rate': [0.01, 0.1, 1],
'max_iter': [50, 100],
'max_leaf_nodes': [31, 63, 127]
}
# Perform grid search
grid_search = GridSearchCV(estimator=HistGradientBoostingClassifier(random_state=42),
param_grid=param_grid,
cv=5,
scoring='accuracy')
grid_search.fit(X_train, y_train)
# Report best score and parameters
print(f"Best score: {grid_search.best_score_:.3f}")
print(f"Best parameters: {grid_search.best_params_}")
# Evaluate on test set
best_model = grid_search.best_estimator_
accuracy = best_model.score(X_test, y_test)
print(f"Test set accuracy: {accuracy:.3f}")
Running the example gives an output like:
Best score: 0.800
Best parameters: {'learning_rate': 0.1, 'max_iter': 100, 'max_leaf_nodes': 31}
Test set accuracy: 0.850
The steps are as follows:
- Generate a synthetic classification dataset using
make_classification
. - Split the dataset into training and test sets using
train_test_split
. - Define the parameter grid with values for
learning_rate
,max_iter
, andmax_leaf_nodes
. - Perform grid search using
GridSearchCV
withHistGradientBoostingClassifier
, 5-fold cross-validation, and accuracy scoring. - Report the best cross-validation score and hyperparameters.
- Evaluate the best model on the test set and report accuracy.
By using GridSearchCV
, we efficiently find the optimal hyperparameters for HistGradientBoostingClassifier
, enhancing model performance while saving time compared to manual tuning.