如何将优化的参数作为列表传递给 light gbm?

How to pass optimized params to light gbm as a list?

我使用了另一种优化算法,returns我是 Light GBM 的最佳参数。

hyper_optimized_clf_classifier = Util.hp_opt(lgb.LGBMClassifier(silent=True, random_state=1), X, y, score, verbose=True,
                   n_estimators =(hp.quniform,50,500,50),
                   learning_rate =(hp.qloguniform, np.log(0.05), np.log(0.4),0.001),
                    min_child_weight =(hp.qloguniform,np.log(3),np.log(200),1),
                    reg_lambda = (hp.qloguniform, np.log(2), np.log(100),1),
                    num_leaves = (hp.qloguniform, np.log(5),np.log(64),1),
                    subsample = (hp.quniform, 0.5, 1, 0.05),
                   colsample_bytree = (hp.quniform, 0.4, 1, 0.05),
                    max_depth = (hp.quniform, 2, 15, 1),
                    subsample_freq = (hp.quniform, 1, 10, 1),
                    max_bin = (hp.qloguniform, np.log(15), np.log(1023),1),
                    max_evals=100)

如果我尝试 get_params,我会得到一个最佳优化参数的字典,我想将其传递给再次训练:

hyper_optimized_clf_classifier.get_params()


{'boosting_type': 'gbdt',
 'class_weight': None,
 'colsample_bytree': 0.45,
 'importance_type': 'split',
 'learning_rate': 0.057,
 'max_depth': 14,
 'min_child_samples': 20,
 'min_child_weight': 20.0,
 'min_split_gain': 0.0,
 'n_estimators': 450,
 'n_jobs': -1,
 'num_leaves': 5,
 'objective': None,
 'random_state': 1,
 'reg_alpha': 0.0,
 'reg_lambda': 2.0,
 'silent': True,
 'subsample': 1.0,
 'subsample_for_bin': 200000,
 'subsample_freq': 6}

我尝试将这些参数作为值列表传递给 light gbm 以再次训练:

['gbdt', None, 0.45, 'split', 0.057, 14, 20, 20.0, 0.0, 450, -1, 5, None, 1, 0.0, 2.0, True, 1.0, 200000,6]

    clf = lgb.LGBMClassifier(list(hyper_optimized_clf_classifier.get_params().values()))

但它不识别它。

"LightGBMError: Unknown boosting type gbdt,none,0.45,split,0.057,14,20,20.0,0.0,450,-1,5,none,1,0.0,2.0,true,1.0,200000,6"

这是一个基于问题数据和@Misha's评论的可重现示例,以供将来参考。

from lightgbm import LGBMClassifier

lgbm_clf = LGBMClassifier()

lgbm_params = {
 'colsample_bytree': 0.45,
 'learning_rate': 0.057,
 'max_depth': 14,
 'min_child_weight': 20.0,
 'n_estimators': 450,
 'num_leaves': 5,
 'random_state': 1,
 'reg_lambda': 2.0,
 'subsample': 0.99,
 'subsample_freq': 6}

lgbm_clf.set_params(**lgbm_params) 

lgbm_clf

输出(注意:在默认级别设置的参数已被删除,因为它们无论如何都不会显示在这里):

LGBMClassifier(colsample_bytree=0.45, learning_rate=0.057, max_depth=14,
               min_child_weight=20.0, n_estimators=450, num_leaves=5,
               random_state=1, reg_lambda=2.0, subsample=0.99,
               subsample_freq=6)