XGBoost model trained in Python cannot predict in R

I trained a model in Python using xgboost.train() and saved using booster_obj.save_model(‘model.model’). I can load this model obj in R using xgb.load method successfully but when calling predict(model, dmatrix) it will always give empty prediction.

Can anyone confirm the functionality to load Python trained model in R actually works?

Python xgboost version 1.0.2, R xgboost 0.82 or 1.2.0.1 (neither would fail)

Can you try installing the R package version 1.0.2?

Since there is no 1.0.2 version in R, I tried 1.0.0.2 and still the same empty results.

That’s quite odd. Can you put up a script and the model file so that we can investigate?

This combo won’t work

Python script:
version: 1.2.0

import xgboost
import numpy as np
from sklearn.datasets import load_iris

data = load_iris()
dtrain = xgboost.DMatrix(data['data'], 
                        label=data['target'] == 1)

params = {
    'learning_rate': 0.1,
    'max_depth': 3,
    'objective': 'binary:logistic',
}

model = xgboost.train(
    params,
    dtrain
)

model.predict(dtrain)
model.save_model('model.model')
np.savetxt('data.csv', data['data'], delimiter=',')

R script:
version: 0.90.0.1

library(xgboost)

model <- xgb.load('./iris_test/model.model')
dtrain <- xgb.DMatrix(as.matrix(read.csv('./iris_test/data.csv')))
predict(model, dtrain)

The R version is much older than the Python package. In general, the version that reads the model should be as recent as the version that produced it. Try upgrading R package to 1.2.0.1.

I confirm it works under R 1.2.0.1. Thank you!