Skip to content

Instantly share code, notes, and snippets.

@groverpr
Last active September 11, 2023 02:48
Show Gist options
  • Save groverpr/97026f225e7b61712e17ce24f20cf201 to your computer and use it in GitHub Desktop.
Save groverpr/97026f225e7b61712e17ce24f20cf201 to your computer and use it in GitHub Desktop.
How to use objective and evaluation in lightgbm
import lightgbm
********* Sklearn API **********
# default lightgbm model with sklearn api
gbm = lightgbm.LGBMRegressor()
# updating objective function to custom
# default is "regression"
# also adding metrics to check different scores
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics = ["mse", 'mae'])
# fitting model
gbm.fit(
X_train,
y_train,
eval_set=[(X_valid, y_valid)],
eval_metric=custom_asymmetric_valid,
verbose=False,
)
y_pred = gbm.predict(X_valid)
********* Python API **********
# create dataset for lightgbm
# if you want to re-use data, remember to set free_raw_data=False
lgb_train = lgb.Dataset(X_train, y_train, free_raw_data=False)
lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train, free_raw_data=False)
# specify your configurations as a dict
params = {
'objective': 'regression',
'verbose': 0
}
gbm = lgb.train(params,
lgb_train,
num_boost_round=10,
init_model=gbm,
fobj=custom_asymmetric_train,
feval=custom_asymmetric_valid,
valid_sets=lgb_eval)
y_pred = gbm.predict(X_valid)
@louis925
Copy link

Hi, Thanks for sharing but your code for Python API doesn't work. According to the LightGBM documentation, The customized objective and evaluation functions (fobj and feval) have to accept two variables (in order): prediction and training_dataset. So you need to modify the head of those function to

def custom_asymmetric_valid(y_pred, dataset_true):
    y_true = dataset_true.get_label()
    ...

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