jpmml / r2pmml

R library for converting R models to PMML
GNU Affero General Public License v3.0
73 stars 18 forks source link

getting error while replicating code for decorate.xgb.Booster #64

Closed adijohar closed 3 years ago

adijohar commented 3 years ago

Hi

I am just trying to replicate the example given in the documentation of decorate.xgb.Booster and create a pmml file for xgboost model. I am getting below issue. I may be doing some silly mistake, but i am not able to figure that out. Below are the setup details. R version - 4.0.2 R Studio Version - 1.3.1093 xgboost package version - 1.2.0.1 r2pmml package version - 0.24.0 Windows 10

Code: data(iris) iris_x = iris[, -ncol(iris)] iris_y = iris[, ncol(iris)] iris_y = (as.integer(iris_y) - 1) iris.fmap = genFMap(iris_x) iris.dmatrix = genDMatrix(iris_y, iris_x) iris.xgboost = xgboost(data = iris.dmatrix, objective = "multi:softprob", num_class = 3, nrounds = 11) iris.xgboost = decorate(iris.xgboost, iris.fmap, response_name = "Species", response_levels = c("setosa", "versicolor", "virginica")) pmmlFile = file.path(tempdir(), "Iris-XGBoost.pmml") r2pmml(iris.xgboost, pmmlFile, compact = FALSE)

Error: Oct 28, 2020 6:26:46 PM org.jpmml.rexp.Main run INFO: Parsing RDS.. Oct 28, 2020 6:26:46 PM org.jpmml.rexp.Main run INFO: Parsed RDS in 19 ms. Oct 28, 2020 6:26:47 PM org.jpmml.rexp.Main run INFO: Initializing default Converter Oct 28, 2020 6:26:47 PM org.jpmml.rexp.Main run INFO: Initialized org.jpmml.rexp.XGBoostConverter Oct 28, 2020 6:26:47 PM org.jpmml.rexp.Main run INFO: Converting.. Oct 28, 2020 6:26:47 PM org.jpmml.rexp.Main run SEVERE: Failed to convert java.lang.ClassCastException: org.jpmml.rexp.RStringVector cannot be cast to org.jpmml.rexp.RIntegerVector at org.jpmml.rexp.XGBoostConverter.loadFeatureMap(XGBoostConverter.java:175) at org.jpmml.rexp.XGBoostConverter.loadFeatureMap(XGBoostConverter.java:157) at org.jpmml.rexp.XGBoostConverter.encodeSchema(XGBoostConverter.java:65) at org.jpmml.rexp.ModelConverter.encodePMML(ModelConverter.java:69) at org.jpmml.rexp.Converter.encodePMML(Converter.java:39) at org.jpmml.rexp.Main.run(Main.java:149) at org.jpmml.rexp.Main.main(Main.java:97)

Exception in thread "main" java.lang.ClassCastException: org.jpmml.rexp.RStringVector cannot be cast to org.jpmml.rexp.RIntegerVector at org.jpmml.rexp.XGBoostConverter.loadFeatureMap(XGBoostConverter.java:175) at org.jpmml.rexp.XGBoostConverter.loadFeatureMap(XGBoostConverter.java:157) at org.jpmml.rexp.XGBoostConverter.encodeSchema(XGBoostConverter.java:65) at org.jpmml.rexp.ModelConverter.encodePMML(ModelConverter.java:69) at org.jpmml.rexp.Converter.encodePMML(Converter.java:39) at org.jpmml.rexp.Main.run(Main.java:149) at org.jpmml.rexp.Main.main(Main.java:97) 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

I tried to check the structure of iris.fmap. Is there any issue in this.

str(iris.fmap) 'data.frame': 4 obs. of 3 variables: $ id : int 0 1 2 3 $ name: chr "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" $ type: chr "q" "q" "q" "q"

vruusmann commented 3 years ago

iris.fmap = genFMap(iris_x) iris.dmatrix = genDMatrix(iris_y, iris_x)

Where did you find such an example code?

The genDMatrix() and genFMap() functions were removed a long time ago.

You should be doing something like this: https://github.com/jpmml/r2pmml#package-xgboost

java.lang.ClassCastException: org.jpmml.rexp.RStringVector cannot be cast to org.jpmml.rexp.RIntegerVector

There's some piece of Java code that expects to find a certain R attribute presented as factor. However, it currently finds a character there.

It's probably the target (aka label) column. Try converting it from a character vector to a proper factor using R's as.factor() utility function.

vruusmann commented 3 years ago

str(iris.fmap) $ type: chr "q" "q" "q" "q"

The iris.fmap$type attribute should be a factor.

vruusmann commented 3 years ago

List of XGBoost code examples (regression, binary classification, multi-class classification): https://github.com/jpmml/jpmml-xgboost/blob/master/src/test/resources/xgboost.R

adijohar commented 3 years ago

Thanks for your help.

I was referring the reference manual - r2pmml.pdf on CRAN website for version 0.24.0 https://cran.r-project.org/web/packages/r2pmml/r2pmml.pdf

vruusmann commented 3 years ago

I was referring the reference manual - r2pmml.pdf on CRAN website for version 0.24.0

Releasing to CRAN is a major PITA.

You should install straight from GitHub, as suggested in the README file: https://github.com/jpmml/r2pmml#installation