Skip to content

Instantly share code, notes, and snippets.

@WillKoehrsen
Created July 17, 2018 23:58
Show Gist options
  • Save WillKoehrsen/960d01f133bd7eca2649e8fd727bbd74 to your computer and use it in GitHub Desktop.
Save WillKoehrsen/960d01f133bd7eca2649e8fd727bbd74 to your computer and use it in GitHub Desktop.
import lightgbm as lgb
def objective(hyperparameters, iteration):
"""Objective function for grid and random search. Returns
the cross validation score from a set of hyperparameters."""
# Number of estimators will be found using early stopping
if 'n_estimators' in hyperparameters.keys():
del hyperparameters['n_estimators']
# Perform n_folds cross validation
cv_results = lgb.cv(hyperparameters, train_set, num_boost_round = 10000, nfold = N_FOLDS,
early_stopping_rounds = 100, metrics = 'auc', seed = 42)
# results to retun
score = cv_results['auc-mean'][-1]
estimators = len(cv_results['auc-mean'])
hyperparameters['n_estimators'] = estimators
return [score, hyperparameters, iteration]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment