Search code examples
rxgboostpmml

Can't export xgboost model to pmml format in R


I am trying to export XGboost model to pmml format using library(pmml). This is the simple code that I use and the error that keeps showing.

dev_samp = data.matrix(mtcars[,c(4,6,9)])

set.seed(123)
bst <- xgboost(data=as.matrix(dev_samp[,1:2]), 
                 label=dev_samp[,3],
                 max_depth=2,
                 eta=0.2,  
                 nrounds=2,
                 colsample_bytree = 0.5,
                 lambda = 0.3,
                 objective = "binary:logistic",
                 eval_metric = "error")

# Export to PMML:
pmod <- pmml(bst,input_feature_names = colnames(as.matrix(dev_samp[,1:2])),
             output_label_name = colnames(dev_samp)[3])
Error in pmml.xgb.Booster(bst, input_feature_names = colnames(as.matrix(dev_samp[,  : 
  Input feature names required at present version. Try using colnames function on Matrix, matrix or xgb.DMatrix$data

Outside of this function the code colnames(as.matrix(dev_samp[,1:2])) and colnames(dev_samp)[3] works.

EDIT:

Thank you for your answer. Unfortunately I am restricted from direct install from GitHub.

So what I did is this:

library(xgboost)
library(r2pmml)

#mtcars$wt=as.integer(mtcars$wt*1000)
#mtcars$hp=as.integer(mtcars$hp*2)
dev_samp = data.matrix(mtcars[,c(4,6,9)])

set.seed(123)
bst <- xgboost(data=as.matrix(dev_samp[,1:2]), 
               label=as.integer(dev_samp[,3]),
               max_depth=2,
               eta=0.2,  
               nrounds=2,
               colsample_bytree = 0.5,
               lambda = 0.3,
               objective = "binary:logistic",
               eval_metric = "error")

fmap=data.frame(seq_along(bst$feature_names)-1, bst$feature_names, "q")
fmap[,2] = as.factor(fmap[,2])
fmap[,3] = as.factor(fmap[,3])
fmap[,1] = as.integer(fmap[,1])

pmod <- decorate.xgb.Booster(bst, "mtcars.pmml",
       fmap = fmap,
       response_name = "prediction",
       response_levels = c("0", "1"),
       missing = "")

r2pmml(pmod, "XGBoostModel2.pmml")

But the last line r2pmml(pmod, "XGBoostModel2.pmml") returns error:

Error in .convert(tempfile, file, converter, converter_classpath, verbose) : 
  The JPMML-R conversion application has failed (error code 1). The Java executable should have printed more information about the failure into its standard output and/or standard error streams

Solution

  • Alternatively, if correctness and conversion speed matter to you, then you can use the R2PMML package for converting R language XGBoost models to PMML.

    library("r2pmml")
    
    r2pmml(bst, "xgboost.pmml", fmap = as.fmap(dev_samp[,1:2]), response_name = colnames(dev_samp)[3])
    

    Be sure to use the latest R2PMML package version (direct install from GitHub repository). The CRAN version is currently a bit outdated (does not provide the as.fmap(..) utility function).