【转】LightGBM 如何调参

看到一篇写得很好的文章,原文地址https://www.jianshu.com/p/b4ac0596e5ef

 

本文结构:

  1. 什么是 LightGBM
  2. 怎么调参
  3. 和 xgboost 的代码比较

1. 什么是 LightGBM

Light GBM is a gradient boosting framework that uses tree based learning algorithm.

LightGBM 垂直地生长树,即 leaf-wise,它会选择最大 delta loss 的叶子来增长。

而以往其它基于树的算法是水平地生长,即 level-wise,

当生长相同的叶子时,Leaf-wise 比 level-wise 减少更多的损失。

高速,高效处理大数据,运行时需要更低的内存,支持 GPU

不要在少量数据上使用,会过拟合,建议 10,000+ 行记录时使用。


2. 怎么调参

下面几张表为重要参数的含义和如何应用

Control Parameters含义用法
max_depth树的最大深度当模型过拟合时,可以考虑首先降低 max_depth
min_data_in_leaf叶子可能具有的最小记录数默认20,过拟合时用
feature_fraction例如 为0.8时,意味着在每次迭代中随机选择80%的参数来建树boosting 为 random forest 时用
bagging_fraction每次迭代时用的数据比例用于加快训练速度和减小过拟合
early_stopping_round如果一次验证数据的一个度量在最近的early_stopping_round 回合中没有提高,模型将停止训练加速分析,减少过多迭代
lambda指定正则化0~1
min_gain_to_split描述分裂的最小 gain控制树的有用的分裂
max_cat_group在 group 边界上找到分割点当类别数量很多时,找分割点很容易过拟合时
Core Parameters含义用法
Task数据的用途选择 train 或者 predict
application模型的用途选择 regression: 回归时,binary: 二分类时,multiclass: 多分类时
boosting要用的算法gbdt, rf: random forest, dart: Dropouts meet Multiple Additive Regression Trees, goss: Gradient-based One-Side Sampling
num_boost_round迭代次数通常 100+
learning_rate如果一次验证数据的一个度量在最近的 early_stopping_round 回合中没有提高,模型将停止训练常用 0.1, 0.001, 0.003…
num_leaves 默认 31
device cpu 或者 gpu
metric mae: mean absolute error , mse: mean squared error , binary_logloss: loss for binary classification , multi_logloss: loss for multi classification
IO parameter含义
max_bin表示 feature 将存入的 bin 的最大数量
categorical_feature如果 categorical_features = 0,1,2, 则列 0,1,2是 categorical 变量
ignore_columncategorical_features 类似,只不过不是将特定的列视为categorical,而是完全忽略
save_binary这个参数为 true 时,则数据集被保存为二进制文件,下次读数据时速度会变快

调参

IO parameter含义
num_leaves取值应 <= 2 ^(max_depth), 超过此值会导致过拟合
min_data_in_leaf将它设置为较大的值可以避免生长太深的树,但可能会导致 underfitting,在大型数据集时就设置为数百或数千
max_depth这个也是可以限制树的深度

下表对应了 Faster Speed ,better accuracy ,over-fitting 三种目的时,可以调的参数

Faster Speedbetter accuracyover-fitting
max_bin 设置小一些用较大的 max_binmax_bin 小一些
 num_leaves 大一些num_leaves 小一些
feature_fraction 来做 sub-sampling feature_fraction
bagging_fraction 和 bagging_freq 设定 bagging_fraction 和 bagging_freq
 training data 多一些training data 多一些
save_binary 来加速数据加载直接用 categorical featuregmin_data_in_leaf 和 min_sum_hessian_in_leaf
用 parallel learning用 dartlambda_l1, lambda_l2 ,min_gain_to_split 做正则化
 num_iterations 大一些,learning_rate 小一些max_depth 控制树的深度

3. lightGBM 和 xgboost 的代码比较

#xgboost
dtrain = xgb.DMatrix(x_train,label=y_train)
dtest = xgb.DMatrix(x_test)


# lightgbm
train_data = lgb.Dataset(x_train,label=y_train)

setting parameters:

#xgboost
parameters = {
    'max_depth':7, 
    'eta':1, 
    'silent':1,
    'objective':'binary:logistic',
    'eval_metric':'auc',
    'learning_rate':.05}

# lightgbm
param = {
    'num_leaves':150, 
    'objective':'binary',
    'max_depth':7,
    'learning_rate':.05,
    'max_bin':200}
param['metric'] = ['auc', 'binary_logloss']

training model :

#xgboost
num_round = 50
from datetime import datetime 
start = datetime.now() 
xg = xgb.train(parameters,dtrain,num_round) 
stop = datetime.now()

# lightgbm
num_round = 50
start = datetime.now()
lgbm = lgb.train(param,train_data,num_round)
stop = datetime.now()

Execution time of the model:

#xgboost
execution_time_xgb = stop - start 
execution_time_xgb

# lightgbm
execution_time_lgbm = stop - start
execution_time_lgbm

predicting model on test set:

#xgboost
ypred = xg.predict(dtest) 
ypred

# lightgbm
ypred2 = lgbm.predict(x_test)
ypred2[0:5]   

Converting probabilities into 1 or 0:

#xgboost
for i in range(0,9769): 
    if ypred[i] >= .5:       # setting threshold to .5 
       ypred[i] = 1 
    else: 
       ypred[i] = 0

# lightgbm
for i in range(0,9769):
    if ypred2[i] >= .5:       # setting threshold to .5
       ypred2[i] = 1
    else:  
       ypred2[i] = 0

calculating accuracy of our model :

#xgboost
from sklearn.metrics import accuracy_score 
accuracy_xgb = accuracy_score(y_test,ypred) 
accuracy_xgb

# lightgbm
accuracy_lgbm = accuracy_score(ypred2,y_test)
accuracy_lgbm
y_test.value_counts()
from sklearn.metrics import roc_auc_score

calculating roc_auc_score:

#xgboost
auc_xgb =  roc_auc_score(y_test,ypred)

# lightgbm
auc_lgbm = roc_auc_score(y_test,ypred2)

最后可以建立一个 dataframe 来比较 Lightgbm 和 xgb:

auc_lgbm comparison_dict = {
    'accuracy score':(accuracy_lgbm,accuracy_xgb),
    'auc score':(auc_lgbm,auc_xgb),
    'execution time':(execution_time_lgbm,execution_time_xgb)}

comparison_df = DataFrame(comparison_dict) 
comparison_df.index= ['LightGBM','xgboost'] 
comparison_df


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中进行LightGBM调参可以通过设置一系列参数来实现。首先,可以调整学习率和估计器的数目。学习率(learning_rate)控制每个估计器对于前一个估计器的权重。较小的学习率可以使模型更加稳定,但可能需要更多的估计器来达到最佳性能。估计器的数目(num_estimators)表示要使用的决策树的数量,较大的数目可能会增加模型的复杂度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LightGBM调参](https://blog.csdn.net/weixin_41917143/article/details/110421742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [提升机器算法LightGBM(图解+理论+增量训练python代码+lightGBM调参方法)](https://blog.csdn.net/lamusique/article/details/95631638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [LightGBM 如何调参](https://blog.csdn.net/weixin_44116269/article/details/103269604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值