【lgb去除警告,设置早停】[LightGBM] [Warning] No further splits with positive gain, best gain: -inf

文章讲述了新版LightGBM中关于训练时警告的处理方法,以及如何通过调整verbosity参数和使用callbacks功能来优化训练过程,包括早停策略的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新版lightGBM在训练时会显示以下警告信息:
在这里插入图片描述
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
中文解释:
表示LightGBM在尝试进一步划分决策树时,没有找到可以带来正增益的划分,也就是说,现有的划分已经是最优的。

这个警告信息是LightGBM的日志输出的一部分,对模型的训练没有实际影响。如果你想隐藏这些警告信息,可以调整LightGBM的日志级别(verbosity)。

在LightGBM中有一个参数叫做verbosity,你可以设置这个参数来控制日志信息的输出。verbosity参数的值可以是-1(静默)、0(致命)、1(警告)、2(信息)、3(调试)。默认值是1,这意味着会打印警告和错误信息。你可以设置为-1或0,以隐藏警告信息。

解决方法:
在参数里面加入verbosity= -1

import lightgbm as lgb

# 创建数据集
train_data = lgb.Dataset(X, label=y)

# 设置参数
params = {
    'boosting_type': 'gbdt',
    'objective': 'regression',
    'metric': 'rmse',
    'verbosity': -1,  # 设置verbosity为-1,以隐藏警告信息
}

# 训练模型
gbm = lgb.train(params, train_data)

另外,新版本的 LightGBM(版本’4.0.0’及更高版本)引入了 callbacks 功能,用于在训练过程中执行自定义操作,例如监控指标、提前停止训练等。通过使用 callbacks,您可以更灵活地控制 LightGBM 训练过程的行为。

def lgb_train(X_train, y_train, X_valid, y_valid):
    model_lgb = lgb.LGBMClassifier(
        max_depth=10,
        n_estimators=5000,
        min_child_weight=100,
        colsample_bytree=0.7,
        subsample=0.9,
        learning_rate=0.1,
        verbosity= -1)
    
    # 设置回调函数
    callbacks = [lgb.log_evaluation(period=1), lgb.early_stopping(stopping_rounds=10)]
    
    model_lgb.fit(
        X_train,
        y_train,
        eval_metric='auc',
        eval_set=[(X_train, y_train), (X_valid, y_valid)],
        callbacks=callbacks)
    
    print(model_lgb.best_score_['valid_1']['auc'])
    return model_lgb

回调函数说明:
callbacks = [lgb.log_evaluation(period=1), lgb.early_stopping(stopping_rounds=10)]
这段代码是用于LightGBM(Light Gradient Boosting Machine)模型训练的回调函数设置。回调函数是在模型训练过程中的不同阶段执行的特定操作,用于监控训练进度和控制训练行为。这里有两个回调函数:

  1. lgb.log_evaluation(period=1)

    • 这个回调函数的作用是在每一轮训练结束后记录评估指标的值,并且指定了记录的频率。
    • period=1 表示每一轮都会记录一次评估指标的值,这有助于实时地了解模型在训练中的性能。
  2. lgb.early_stopping(stopping_rounds=10)

    • 这个回调函数的作用是实现早停止(early stopping)策略,用于自动停止训练,以防止模型过拟合。
    • stopping_rounds=10 表示如果在连续的10轮训练中,模型的性能在验证集上没有明显的改善(例如,损失函数值没有下降),则训练会提前停止。

综合起来,这两个回调函数一起用于监控训练过程中的性能并防止过拟合。每一轮训练结束后,会记录评估指标的值,如果连续的10轮中性能没有提升,训练就会提前结束,以节省时间和资源,并避免过拟合问题。这是一个常见的训练策略,有助于得到更好的模型。

新版本变化:移除了fit方法中的
verbose=Falseearly_stopping_rounds=10的参数设置,但可以通过verbosity= -1来屏蔽警告,另外可以在callbacks中设置早停止,以达到之前的效果。

# 设置 LightGBM 参数(正则化和复杂度控制) > param <- list( + objective = "regression", + metric = "rmse", + learning_rate = 0.01, + num_leaves = 31, + feature_fraction = 0.8, + bagging_fraction = 0.8, + lambda_l1 = 0.1, + lambda_l2 = 0.2 + ) > > # 使用机制训练 LightGBM > lgb_model <- lgb.train( + params = params, + data = lgb_train, + nrounds = params$nrounds, + valids = list(validation = lgb.Dataset(test_matrix, label = test_labels)), + early_stopping_rounds = 10, + verbose = 1 + ) [LightGBM] [Info] Number of positive: 231, number of negative: 154 [LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000443 seconds. You can set `force_row_wise=true` to remove the overhead. And if memory is not enough, you can set `force_col_wise=true`. [LightGBM] [Info] Total Bins 792 [LightGBM] [Info] Number of data points in the train set: 385, number of used features: 13 [LightGBM] [Info] [binary:BoostFromScore]: pavg=0.600000 -> initscore=0.405465 [LightGBM] [Info] Start training from score 0.405465 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] Stopped training because there are no more leaves that meet the split requirements [1]: validation's binary_error:0.395833 Will train until there is no improvement in 10 rounds. [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] Stopped training because there are no more leaves that meet the split requirements [2]: validation's binary_error:0.395833 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] Stopped training because there are no more leaves that meet the split requirements [3]: validation's binary_error:0.395833 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] Stopped training because there are no more leaves that meet the split requirements [4]: validation's binary_
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值