How to set hyperparameters with a dictionary

Question:

model = lightgbm.LGBMClassifier()  
hyperparameter_dictionary = {'boosting_type': 'goss',   'num_leaves': 25, 'n_estimators': 184, ...}  

How do I set the model’s hyperparameters with the dictionary?

Thanks!

Asked By: Roc

||

Answers:

pass the hyperparam dictionary to the model constructor, adding ** to the dict to pass each dict item like a kwarg param as lgbm expects it per https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMClassifier.html#lightgbm.LGBMClassifier:

hyperparameter_dictionary = {'boosting_type': 'goss', 'num_leaves': 25, 'n_estimators': 184}
model = lightgbm.LGBMClassifier(**hyperparameter_dictionary)

TEST:

print(model)

LGBMClassifier(boosting_type='goss', ... n_estimators=184, n_jobs=-1, num_leaves=25,...)
Answered By: Max Power

The sklearn BaseEstimator interface provides get_params and set_params for getting and setting hyperparameters of an estimator. LightGBM is compliant so you can do as follows:

model = lightgbm.LGBMClassifier()  
hyperparameter_dictionary = {'boosting_type': 'goss',   'num_leaves': 25, 'n_estimators': 184}  
model.set_params(**hyperparameter_dictionary)
Answered By: Micah Smith