loft-br / xgboost-survival-embeddings

Improving XGBoost survival analysis with embeddings and debiased estimators
https://loft-br.github.io/xgboost-survival-embeddings/
Apache License 2.0
313 stars 51 forks source link

AttributeError: `best_iteration` is only defined when early stopping is used - when xgboost>=2 #71

Open DrShushen opened 9 months ago

DrShushen commented 9 months ago

Code sample

Run the below code snippets.

Requirements:

# I used Python 3.8.
pip install xgbse
pip install "xgboost>=2"
# xgbse: 0.2.3
# xgboost: 2.0.0

import xgbse
import numpy as np

model = xgbse.XGBSEDebiasedBCE(lr_params={"max_iter": 10})

np.random.seed(0)
X = np.random.normal(size=(10, 5))
e = np.random.randint(low=0, high=1 + 1, size=(10, 1), dtype=bool)
t = np.random.rand(10, 1)
y = np.array(list(zip(e, t)), dtype={"names": ("e", "t"), "formats": ("bool", "f8")})

model.fit(X, y)

Problem description

An AttributeError is thrown unexpectedly. Since early_stopping_rounds is set as None in fit (which is the default), the user wouldn't expect to see an error related to early stopping.

Traceback (most recent call last):
  File "xgbse_20230926.py", line 15, in <module>
    model.fit(X, y)
  File "<...>/python3.8/site-packages/xgbse/_debiased_bce.py", line 232, in fit
    dtrain, pred_leaf=True, iteration_range=(0, self.bst.best_iteration + 1)
  File "<...>/python3.8/site-packages/xgboost/core.py", line 2602, in best_iteration
    raise AttributeError(
AttributeError: `best_iteration` is only defined when early stopping is used.

Expected behavior

Code executes without throwing the AttributeError.

Possible solutions

For example, add a check whether early stopping was enabled before accessing self.bst.best_iteration (this appears to be used in self.bst.predict(..., iteration_range=(0, self.bst.best_iteration + 1)) in _debiased_bce.py), otherwise use an appropriate default value.