Skip to content

Instantly share code, notes, and snippets.

@patrickbrus
Last active January 10, 2021 18:29
Show Gist options
  • Save patrickbrus/3902285cb77bb1662cfa99e8141eb759 to your computer and use it in GitHub Desktop.
Save patrickbrus/3902285cb77bb1662cfa99e8141eb759 to your computer and use it in GitHub Desktop.
Fit a bayesian optimizer for a random forest
# Define a model
forest = RandomForestRegressor(criterion="mse", n_jobs=n_jobs)
# Create the Bayesion optimization object
opt = BayesSearchCV(
forest,
{
"max_depth": (5, 15),
"n_estimators": (10, 50),
"bootstrap": [True, False]
},
n_iter=n_iter,
cv=n_folds
)
# Train the model
opt.fit(train_x, train_y)
model = opt.best_estimator_
# Save the trained model
joblib.dump(model, os.path.join(args.model_dir, "model.joblib"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment