Incompatibility between R and python model objects

I have saved an xgb model in R using all the recommended methods (mainly xgb save and R’s pickle) which are meant to be platform independent. I have as yet not been able to deploy this model in python.

Has anyone successfully created an xgb model in R, saved it, and then deployed it in python?

I have seen many bugs and issues relating to this, and have tried all recommended fixes without success.

I would use xgb.save() and then load the model in Python as follows:

import xgboost

bst = xgboost.Booster(model_file=filename)

Thank you for getting back to me.
Have you actually tried this? I know it is ‘supposed’ to work. I tried it and got the following error, which
others have reported:

Traceback (most recent call last):
File “”, line 1, in
File “/Users/davide/miniconda3/lib/python3.6/site-packages/xgboost/sklearn.py”, line 801, in predict
return self._le.inverse_transform(column_indexes)
AttributeError: ‘XGBClassifier’ object has no attribute ‘_le’

Use Booster, not XGBClassifier

In Python, I merely typed:

clf=pickle.load(open(“pickle_model.pkl”, “rb”))
y=clf.predict(xtmp)

I didn’t mention XGBClassifier

When I had saved it in R, I never mentioned XGBClassifier. I had fit the model via

bst <- xgb.train(param,…

After that, how should I have saved it?

Thanks again for your help.

Just to make sure, I typed

class(bst)
[1] “xgb.Booster”

My example didn’t mention pickle. Can you try my example (without pickle)?

bst = xgboost.Booster(model_file=‘xgbsave_test.model’)
[22:44:07] WARNING: src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.

y=bst.predict(xtmp)
Traceback (most recent call last):
File “”, line 1, in
File “/Users/davide/miniconda3/lib/python3.6/site-packages/xgboost/core.py”, line 1284, in predict
self._validate_features(data)
File “/Users/davide/miniconda3/lib/python3.6/site-packages/xgboost/core.py”, line 1671, in _validate_features
self.feature_names = data.feature_names
AttributeError: ‘numpy.ndarray’ object has no attribute ‘feature_names’

Create DMatrix object:

dtest = xgboost.DMatrix(xtmp)
y = bst.predict(dtest)

:grinning::grinning::grinning::grinning::grinning:

Thank you so much! I have been tearing my hair out!!

Glad it helped. Thanks always for using XGBoost package.

In general, I’d avoid using pickle for loading anything that was created outside of Python.

Thanks for the tip. This is contrary to a number of blogs.