Incompatibility between R and python model objects


#1

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.


#2

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

import xgboost

bst = xgboost.Booster(model_file=filename)

#3

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’


#4

Use Booster, not XGBClassifier


#5

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.


#6

Just to make sure, I typed

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


#7

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


#8

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’


#9

Create DMatrix object:

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


#10

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

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


#11

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.


#12

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