# xgboost Predictions from R and Python don't match

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:

(2) Run this script in R:

library(xgboost)
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

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

• have you read this entry on kaggle? or this one? – phiver Sep 30 '16 at 8:43
• 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. – phiver Oct 1 '16 at 10:30
• @phiver That's a great point. It looks like the CRAN version hasn't been updated in quite a while. – sergeyf Oct 1 '16 at 20:32

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)