Skip to content

Instantly share code, notes, and snippets.

@kyleziegler
Created March 18, 2022 17:40
Show Gist options
  • Save kyleziegler/90bcff7d77d64e49dfb408b9cf058fc9 to your computer and use it in GitHub Desktop.
Save kyleziegler/90bcff7d77d64e49dfb408b9cf058fc9 to your computer and use it in GitHub Desktop.
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
from skopt.plots import plot_convergence
from skopt.utils import use_named_args
from skopt.space import Real, Integer
from skopt import gp_minimize
import matplotlib.pyplot as plt
plt.rcParams["figure.dpi"] = 100
plt.rcParams["figure.figsize"] = [10,4]
def bayes_loop_example():
model = GradientBoostingClassifier(random_state=0, n_estimators=50)
space = [
Real(10**-5, 10**0, "log-uniform", name='learning_rate'),
Integer(20, 50, name='n_estimators'),
Integer(2, 8, name='min_samples_split')
]
@use_named_args(space)
def objective(**params):
model.set_params(**params)
return -np.mean(cross_val_score(model, X_train, y_train, cv=3, n_jobs=-1,
scoring="neg_mean_absolute_error"))
res_gp = gp_minimize(objective, space, n_calls=20, random_state=0)
plot_convergence(res_gp)
print("Best Parameters", res_gp.x)
print("Best Score", res_gp.fun)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment