3
$\begingroup$

Thought maybe somebody here could help us solve a mystery (https://github.com/dmlc/xgboost/issues/1623):

We are trying to build a xgboost prediction function in R for a model that was trained in Python and the results don't match. See below for an example of how to reproduce.

Fairly new to xgboost, particularly using it across languages, so may be missing something obvious.

Steps to reproduce:

(1) Download this model file: http://ml.stat.purdue.edu/hafen/WTKG.model

(2) Run this script in R:

library(xgboost)
mod <- xgb.load("WTKG.model")
x <- c(91, 9, 9, NA, NA, 273, 20, 170, NA, NA, 14, 14, 0,
  2, 0.94289404091, 0.94289404091, 0.93087973569, 0.0120143052199997, 0.95490834613,
  0.95490834613, 1, 90, 0.95490834613, 1, 90,
  0.93087973569, 357, -266, 0.93087973569, 357, -266,
  0.95490834613, NA, 0.93087973569, NA, NA, NA)
d <- xgb.DMatrix(matrix(x, nrow = 1), missing = NA)
predict(mod, d)
# [1] 0.6483372

(3) Run this script in Python:

import numpy as np
import xgboost as xgb

bst = xgb.Booster({'nthread': 4})
bst.load_model('WTKG.model')
x = [91, 9, 9, np.nan, np.nan, 273, 20, 170, np.nan, np.nan, 14, 14, 0,
  2, 0.94289404091, 0.94289404091, 0.93087973569, 0.0120143052199997, 0.95490834613,
  0.95490834613, 1, 90, 0.95490834613, 1, 90,
  0.93087973569, 357, -266, 0.93087973569, 357, -266,
  0.95490834613, np.nan, 0.93087973569, np.nan, np.nan, np.nan]
d = xgb.DMatrix(data=[x], missing=np.nan)
bst.predict(d)[0]
# 1.3775804
$\endgroup$
3
  • $\begingroup$ have you read this entry on kaggle? or this one? $\endgroup$
    – phiver
    Commented Sep 30, 2016 at 8:43
  • $\begingroup$ I checked the github issue you are referring to. Could it be because of difference in xgboost version? The model was built under xgboost 0.6, while the R version used is version 0.4-4. $\endgroup$
    – phiver
    Commented Oct 1, 2016 at 10:30
  • $\begingroup$ @phiver That's a great point. It looks like the CRAN version hasn't been updated in quite a while. $\endgroup$
    – sergeyf
    Commented Oct 1, 2016 at 20:32

2 Answers 2

3
$\begingroup$

Solution for posterity: In Python, xgboost requires np.array as input. So this line:

d = xgb.DMatrix(data=[x], missing=np.nan)

should be:

d = xgb.DMatrix(data=np.array([x]), missing=np.nan)
$\endgroup$
-1
$\begingroup$

I think you can get your answer from here

$\endgroup$
1
  • $\begingroup$ This post shows that if you FIT separately in Python and R you may get different results. But I have one model already trained/fit. The predictions should be the same regardless of how different the fitting procedures are, no? $\endgroup$
    – sergeyf
    Commented Sep 30, 2016 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.