Skip to content

Instantly share code, notes, and snippets.

@mohit-sinha
Created June 27, 2018 11:51
Show Gist options
  • Save mohit-sinha/be3f2999eb21d1992d03b7590fe2d88b to your computer and use it in GitHub Desktop.
Save mohit-sinha/be3f2999eb21d1992d03b7590fe2d88b to your computer and use it in GitHub Desktop.
Use this for LightGBM parameter optimisation by Bayesian optimisation.
import pandas as pd;
import numpy as np;
import lightgbm as lgb
from bayes_opt import BayesianOptimization
from sklearn.model_selection import cross_val_score
def lgb_evaluate(
numLeaves,
maxDepth,
scaleWeight,
minChildWeight,
subsample,
colSam
):
clf = lgb.LGBMClassifier(
objective = 'binary',
metric= 'auc',
eval_metric= 'auc',
reg_alpha= 0,
reg_lambda= 2,
bagging_fraction= 0.999,
min_split_gain= 0,
min_child_samples= 10,
subsample_freq= 3,
subsample_for_bin= 50000,
n_estimators= 9999999,
num_leaves= int(numLeaves),
max_depth= int(maxDepth),
scale_pos_weight= scaleWeight,
min_child_weight= minChildWeight,
subsample= subsample,
colsample_bytree= colSam,
verbose =-1
)
scores = cross_val_score(clf, train_x, train_y, cv=5, scoring='roc_auc')
print(np.mean(scores))
return np.mean(scores)
def bayesOpt(train_x, train_y):
lgbBO = BayesianOptimization(lgb_evaluate, {
'numLeaves': (5, 50),
'maxDepth': (2, 63),
'scaleWeight': (1, 10000),
'minChildWeight': (0.01, 70),
'subsample': (0.4, 1),
'colSam': (0.4, 1)
})
lgbBO.maximize(init_points=5, n_iter=5)
print(lgbBO.res['max'])
bayesOpt(train_x, train_y)
#Run this in one of your Ipynb cells
#Kindly comment if you wish to share anything or improvise
@tbonfi-rd
Copy link

thank you! It's old but this has aged well 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment