【模型调参】Lgb调参方法

首先,调参方法有几种:

贪心算法 https://www.jianshu.com/p/ab89df9759c8
网格调参 https://blog.csdn.net/weixin_43172660/article/details/83032029
贝叶斯调参 https://blog.csdn.net/linxid/article/details/81189154

这里以回归任务、 lgb 模型为例子:

参数集合

objective = ['regression', 'regression_l1', 'mape', 'huber', 'fair']
num_leaves = [3,5,10,15,20,40, 55]
max_depth = [3,5,10,15,20,40, 55]
bagging_fraction = []
feature_fraction = []
drop_rate = []

1 贪心调参

best_obj = dict()
for obj in objective:
    model = LGBMRegressor(objective=obj)
    score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
    best_obj[obj] = score
    
best_leaves = dict()
for leaves in num_leaves:
    model = LGBMRegressor(objective=min(best_obj.items(), key=lambda x:x[1])[0], num_leaves=leaves)
    score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
    best_leaves[leaves] = score
    
best_depth = dict()
for depth in max_depth:
    model = LGBMRegressor(objective=min(best_obj.items(), key=lambda x:x[1])[0],
                          num_leaves=min(best_leaves.items(), key=lambda x:x[1])[0],
                          max_depth=depth)
    score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
    best_depth[depth] = score
sns.lineplot(x=['0_initial','1_turning_obj','2_turning_leaves','3_turning_depth'], 
			 y=[0.143 ,min(best_obj.values()), 
			 min(best_leaves.values()), min(best_depth.values())])

2 Grid Search

from sklearn.model_selection import GridSearchCV

parameters = {'objective': objective , 'num_leaves': num_leaves, 'max_depth': max_depth}
model = LGBMRegressor()
clf = GridSearchCV(model, parameters, cv=5)
clf = clf.fit(train_X, train_y)

clf.best_params_
model = LGBMRegressor(objective='regression',
                          num_leaves=55,
                          max_depth=15)

np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))                       

3 贝叶斯调参

from bayes_opt import BayesianOptimization

def rf_cv(num_leaves, max_depth, subsample, min_child_samples):
    val = cross_val_score(
        LGBMRegressor(objective = 'regression_l1',
            num_leaves=int(num_leaves),
            max_depth=int(max_depth),
            subsample = subsample,
            min_child_samples = int(min_child_samples)
        ),
        X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)
    ).mean()
    return 1 - val
    
rf_bo = BayesianOptimization(
    rf_cv,
    {
    'num_leaves': (2, 100),
    'max_depth': (2, 100),
    'subsample': (0.1, 1),
    'min_child_samples' : (2, 100)
    }
)

rf_bo.maximize()

1 - rf_bo.max['target']

参考:https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12586969.1002.24.1cd8593aLNK3uJ&postId=95460

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LGB(LightGBM)是一种基于决策树的梯度提升框架,具有高效、准确的特点,被广泛应用于机器学习和数据科学领域。下面是LGB模型常用的参数及其含义: 1. `num_leaves`:决策树的最大叶子节点数,这是LGB模型最重要的参数之一,通常取值在40-60之间。 2. `learning_rate`:学习率,每次迭代每个树的权重缩减量,通常取值在0.01-0.1之间。 3. `max_depth`:决策树的最大深度,控制模型的复杂度和过拟合情况,通常取值在5-15之间。 4. `min_child_samples`:叶子节点最少样本数,控制过拟合情况,通常取值在20-100之间。 5. `subsample`:每个树的样本采样比例,通常取值在0.5-0.8之间。 6. `colsample_bytree`:每个树的特征采样比例,通常取值在0.5-0.8之间。 7. `reg_alpha`:L1正则化系数,控制模型的复杂度和过拟合情况。 8. `reg_lambda`:L2正则化系数,控制模型的复杂度和过拟合情况。 关于调参方法,通常采用网格搜索或贝叶斯优化等方法,通过交叉验证来确定最佳参数组合。具体步骤如下: 1. 确定模型的初始参数范围。 2. 采用交叉验证方法,在训练集上训练模型,并在验证集上评估模型的性能。 3. 根据模型的性能,调整参数范围,重新训练模型,并重新进行交叉验证。 4. 重复步骤3,直到达到最佳性能,得到最佳的参数组合。 需要注意的是,调参的过程需要耗费大量时间和计算资源,因此需要谨慎选择参数范围和调参方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值