无法在R中将xgboost模型导出为pmml格式

问题描述

我正在尝试使用pmml将XGboost模型导出为library(pmml)格式。 这是我使用的简单代码,并且不断显示错误

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 required at present version. Try using colnames function on Matrix,matrix or xgb.DMatrix$data

在此功能之外,代码colnames(as.matrix(dev_samp[,1:2]))colnames(dev_samp)[3]有效。

编辑:

谢谢您的回答。不幸的是,我不能从GitHub直接安装。

所以我要做的是

library(xgboost)
library(r2pmml)

#mtcars$wt=as.integer(mtcars$wt*1000)
#mtcars$hp=as.integer(mtcars$hP*2)
dev_samp = data.matrix(mtcars[,label=as.integer(dev_samp[,3]),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")

但是最后一行r2pmml(pmod,"XGBoostModel2.pmml")返回错误

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

解决方法

或者,如果您对准确性和转换速度很重要,则可以使用R2PMML包将R语言XGBoost模型转换为PMML。

library("r2pmml")

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

确保使用最新的R2PMML软件包版本(从GitHub存储库直接安装)。目前,CRAN版本有些过时(不提供as.fmap(..)实用程序功能)。