如何在LightGBM优化后传递多个超参数?

3

我使用了另一种优化算法,为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)

如果我尝试获取参数,我会得到一个最佳优化参数的字典,我想再次传递给训练:
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"

请查看 https://github.com/microsoft/LightGBM/blob/master/examples/python-guide/simple_example.py 并将 params 字典替换为您自己的。 - Florian Mutel
2
你尝试过 lgb.LGBMClassifier().set_params(**hyper_optimized_clf_classifier.get_params()) 吗? - Mischa Lisovyi
1个回答

3
这是一个基于问题数据和@Misha评论的可重现示例,供将来参考。
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)

我发现我们内部的机器学习库中添加了两个繁琐的手动包装器来实现与set_params()相同的结果,以避免类似的痛苦。因此,我在这里执行任务,以便拯救未来的读者。 - mirekphd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接