James Lee
James Lee

Reputation: 51

AttributeError: 'XGBModel' object has no attribute 'callbacks'

Traceback (most recent call last): File "D:\Miniconda3\envs\ppy39\lib\site-packages\flask\app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "D:\Miniconda3\envs\ppy39\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\Miniconda3\envs\ppy39\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "D:\Miniconda3\envs\ppy39\lib\site-packages\flask\app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "C:\Users\admin\Desktop\VScode\WorkProjects\2022\Product_Classification\retention_ml.py", line 169, in output_result
result_28 = xgboost_reg_281.predict(data[col_reg_28]) File "D:\Miniconda3\envs\ppy39\lib\site-packages\xgboost\sklearn.py", line 1047, in predict if self._can_use_inplace_predict(): File "D:\Miniconda3\envs\ppy39\lib\site-packages\xgboost\sklearn.py", line 983, in _can_use_inplace_predict predictor = self.get_params().get("predictor", None) File "D:\Miniconda3\envs\ppy39\lib\site-packages\xgboost\sklearn.py", line 636, in get_params params.update(cp.class.get_params(cp, deep)) File "D:\Miniconda3\envs\ppy39\lib\site-packages\xgboost\sklearn.py", line 633, in get_params params = super().get_params(deep) File "D:\Miniconda3\envs\ppy39\lib\site-packages\sklearn\base.py", line 205, in get_params value = getattr(self, key) AttributeError: 'XGBModel' object has no attribute 'callbacks'

Upvotes: 4

Views: 15548

Answers (3)

Gnoevoet
Gnoevoet

Reputation: 11

If you want to use xgboost version 1.6 you can simply do the following:

new_attrs = ['grow_policy', 'max_bin', 'eval_metric', 'callbacks', 'early_stopping_rounds', 'max_cat_to_onehot', 'max_leaves', 'sampling_method']

for attr in new_attrs:
    setattr(xgb_model, attr, None)

This sets all the parameters that are in 1.6 but not in 1.5. All of them are typed as Optional so as far as I can tell None is fine. Results are consistent.

If you get complaints about the model being an old version (and you used pickle), you can create a virtual environment with the old xgboost version and an environment with the new version. Then, in the old version, do:

import pickle

with open("xgb_model_1_5.p", "rb") as rf:
    xgb = pickle.load(rf)
    bst = xgb.get_booster()
    bst.save_model("booster")

And then in the 1.6 virtual environment:

import pickle

with open("xgb_model_1_5.p", "rb") as rf:
    xgb = pickle.load(rf)
    bst = xgb.get_booster()
    bst.load_model("booster")

with open("xgb_model_1_6.p", "wb") as wf:
    pickle.dump(xgb, wf)

Upvotes: 1

Roberto Suárez
Roberto Suárez

Reputation: 51

We made:

!pip install xgboost==1.5.0

And it ran perfectly

Upvotes: 4

Dian Yu
Dian Yu

Reputation: 91

Check your xgboost library version. I loaded a model saved from xgboost==1.5.0 env to a xgboost==1.6.0 env and got the same error when operating on the model. I downgraded xgboost to 1.5.0 and everything worked fine. I suspect the model saving format is changing since 1.6.0 as it gives warning about me loading a binary model file using pickle dump.

Upvotes: 9

Related Questions