【Lightgbm】AttributeError: 'LGBMRegressor' object has no attribute 'best_iteration'

0x00 情景复现

使用 lightgbm 进行简单便捷的fit操作,尝试使用early_stopping,
以选择最好的一次迭代进行预测时,调用best_iteration时报错:

import lightgbm as lgb
gbm = lgb.LGBMRegressor(objective='binary',
                        num_leaves=64,
                        learning_rate=0.01,
                        n_estimators=2000)

gbm.fit(X_train, y_train,
        eval_set=[(X_val, y_val)],  # 对于需要best_iteration的,必须存在验证集
        eval_metric='binary_logloss',
        early_stopping_rounds=50)  # 对于需要best_iteration的,可以设置若连续多少轮无优化提前停止

gbm.predict(X_test, num_iteration=gbm.best_iteration)

# 出现报错
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-0e54b2141afb> in <module>()
      1 # gbm.best_iteration_
----> 2 gbm.predict(X_test, num_iteration=gbm.best_iteration)

AttributeError: 'LGBMRegressor' object has no attribute 'best_iteration'

0x01 查阅资料

通过查阅API文档可知:
class lightgbm.LGBMRegressor包括以下Attributes:

  • n_features_
    int – The number of features of fitted model.

  • classes_
    array of shape = [n_classes] – The class label array (only for classification problem).

  • n_classes_
    int – The number of classes (only for classification problem).

  • best_score_
    dict or None – The best score of fitted model.

  • best_iteration_ <<< HERE
    int or None – The best iteration of fitted model if early_stopping_rounds has been specified.

  • objective_
    string or callable – The concrete objective used while fitting this model.

  • booster_
    Booster – The underlying Booster of this model.

  • evals_result_
    dict or None – The evaluation results if early_stopping_rounds has been specified.

  • feature_importances_
    array of shape = [n_features] – The feature importances (the higher, the more important the feature).

0x02 解决方案

# 加个下划线即可
# print(gbm.best_iteration)  # Error
print (gbm.best_iteration_)  # -> 707

# gbm.predict(X_test, num_iteration=gbm.best_iteration)  # Error
gbm.predict(X_test, num_iteration=gbm.best_iteration_)  # Correct
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值