Last active
September 11, 2023 02:48
-
-
Save groverpr/97026f225e7b61712e17ce24f20cf201 to your computer and use it in GitHub Desktop.
How to use objective and evaluation in lightgbm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andfeval
) have to accept two variables (in order): prediction and training_dataset. So you need to modify the head of those function to