R package xgboost 3.2.1.1 not working with r2pmml 0.30.0
In below code I train a GBM model on the IRIS dataset using the xgboost library (version 3.2.1.1) in R. When trying to export the gbm to an pmml file using the package r2pmml (version 0.30.0) I get the error message:
Error in x$fmap <- fmap :
ALTLIST classes must provide a Set_elt method [class: XGBAltrepPointerClass, pkg: xgboost]
Code:
### training xgboost and export to pmml
cat("\014")
rm(list=ls())
#install.packages('renv')
#renv::init(repos = "https://p3m.dev/cran/2025-12-01")
#renv::install(c("xgboost", "caret"))
library(xgboost)
library(r2pmml)
data(iris)
print(iris)
species = iris$Species
label = as.integer(iris$Species)-1# en vektor med 0,1,2
iris$Species = NULL
n = nrow(iris)
train.index = sample(n,floor(0.75*n))
train.data = as.matrix(iris[train.index,])
train.data = as.matrix(data.frame(train.data, check.names = FALSE))#suggested by copilot
train.label = label[train.index]
test.data = as.matrix(iris[-train.index,])
test.label = label[-train.index]
options(xgboost.altrep = FALSE)#suggested by copilot
xgb.train = xgb.DMatrix(data=train.data,label=train.label)
xgb.test = xgb.DMatrix(data=test.data,label=test.label)
### parameters
num_class = length(levels(species))
params = list(
booster="gbtree",
eta=0.001,
max_depth=5,
gamma=3,
subsample=0.75,
colsample_bytree=1,
objective="multi:softprob",
eval_metric="mlogloss",
num_class=num_class
)
# Train the XGBoost classifer
xgb.fit=xgb.train(
params=params,
data=xgb.train,
nrounds=10000,
nthreads=1,
early_stopping_rounds=10,
watchlist=list(val1=xgb.train,val2=xgb.test),
verbose=0
)
# Review the final model and results
xgb.fit
# Predict outcomes with the test data
xgb.pred = predict(xgb.fit,test.data,reshape=T)
xgb.pred = as.data.frame(xgb.pred)
colnames(xgb.pred) = levels(species)
# Use the predicted label with the highest probability
xgb.pred$prediction = apply(xgb.pred,1,function(x) colnames(xgb.pred)[which.max(x)])
xgb.pred$label = levels(species)[test.label+1]
# Calculate the final accuracy
result = sum(xgb.pred$prediction==xgb.pred$label)/nrow(xgb.pred)
print(paste("Final Accuracy =",sprintf("%1.2f%%", 100*result)))
packageVersion("xgboost")#3.2.1.1
packageVersion("r2pmml")#3.2.1.1
# Feature map from training data
xgb.fmap <- r2pmml::as.fmap(train.data)
r2pmml(
xgb.fit,
"D:\\mypmml.pmml",
fmap = xgb.fmap,
response_name = "label",
response_levels = c("0", "1","2"),
compact = FALSE
)
I am working with Java 11 installed.
> system("java --version")
java 11.0.25 2024-10-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.25+9-LTS-256)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.25+9-LTS-256, mixed mode)
I did not get it to work with renv package. Perhaps my IT-department blocks this site.
renv::init(repos = "https://p3m.dev/cran/2025-12-01")