Skip to content

Instantly share code, notes, and snippets.

@3catz
Created September 28, 2020 17:44
Show Gist options
  • Save 3catz/09024f18fef3361afc38742700efa30a to your computer and use it in GitHub Desktop.
Save 3catz/09024f18fef3361afc38742700efa30a to your computer and use it in GitHub Desktop.
Cross Val and Bayesian hyperparameter tuning for Random Forest
from bayes_opt import BayesianOptimization
def RF_opt(n_estimators, max_depth):
global rskf
reg = RandomForestClassifier(verbose = 0,
n_estimators = int(n_estimators),
#min_samples_split = int(min_samples_split),
#min_samples_leaf = int(min_samples_leaf),
max_depth = int(max_depth),
warm_start = True,
#max_features = int(max_features),
random_state = rs)
reg.fit(X1,Y1)
scores = cross_val_score(reg, X1, Y1, scoring = mcc, cv = rskf)
return np.mean(scores)
pbounds = {"n_estimators": (40, 500),
"max_depth": (2,14),
#"min_samples_split": (10, 20),
#"min_samples_leaf":(10, 20),
#"max_features": None,
}
optimizer = BayesianOptimization(
f = RF_opt,
pbounds = pbounds,
verbose = 2,
)
optimizer.maximize(init_points = 2, n_iter = 20)
print(optimizer.max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment