【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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章