无法将xgboost模型导出为R中的pmml格式
Can't export xgboost model to pmml format in R
我正在尝试使用 library(pmml)
将 XGboost 模型导出为 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 = colnames(as.matrix(dev_samp[, :
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[,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")
但是最后一行r2pmml(pmod, "XGBoostModel2.pmml")
returns错误:
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(..)
实用功能)。
我正在尝试使用 library(pmml)
将 XGboost 模型导出为 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 = colnames(as.matrix(dev_samp[, :
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[,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")
但是最后一行r2pmml(pmod, "XGBoostModel2.pmml")
returns错误:
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(..)
实用功能)。