gridsearch for gradientboostingclassifier sklearn in python

Here's an example of how to perform a grid search for GradientBoostingClassifier in sklearn:

main.py
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate some random data for demonstration
X, y = make_classification()

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Define the parameter grid to search over
param_grid = {
    'n_estimators': [50, 100, 200],
    'learning_rate': [0.05, 0.1, 0.2],
    'max_depth': [3, 4, 5]
}

# Initialize the GradientBoostingClassifier model
clf = GradientBoostingClassifier()

# Initialize the grid search object
grid_search = GridSearchCV(clf, param_grid=param_grid, cv=5, n_jobs=-1)

# Fit the grid search object to the data
grid_search.fit(X_train, y_train)

# Print the best parameters and accuracy score found during the grid search
print("Best parameters: ", grid_search.best_params_)
print("Best accuracy score: {:.3f}".format(grid_search.best_score_))
1040 chars
31 lines

In this example, we first generate some random classification data for demonstration purposes and split it into training and testing sets. Then we define a parameter grid to search over using param_grid. We initialize a GradientBoostingClassifier model and a GridSearchCV object with the appropriate parameters, and fit the grid search object to the data using fit(). Finally, we print the best parameters and accuracy score found during the grid search using best_params_ and best_score_. Note that n_jobs is set to -1 to use all available cores on the machine.

gistlibby LogSnag