简体   繁体   中英

How can I export a gbm model in R?

Is there a standard (or available) way to export a gbm model in R? PMML would work, but when II try to use the pmml library, perhaps incorrectly, I get an error:

For example, my code looks similar to this:

  library("gbm")
  library("pmml")

  model <- gbm(
      formula,
      data = my.data,
      distribution = "adaboost",
      n.trees = 450,
      n.minobsinnode = 10,
      interaction.depth = 4, shrinkage=0.05, verbose=TRUE)
  export <- pmml(model)
  # and then export to xml

And the error I get is:

Error in UseMethod("pmml") : no applicable method for 'pmml' applied to an object of class "gbm"

I've also tried passing in the dataset. In any case, I could live with another format I can parse programmatically (I'll be scoring on the JVM) but PMML would be great if there is a way to make that work.

You can do the job using the r2pmml package . Currently, it supports regression (ie. distribution = "gaussian" ) and binary classification (ie. distribution = "adaboost" or distribution = "bernoulli" ) model types.

Below is a sample code for the Auto MPG dataset :

library("gbm")
library("r2pmml")

auto = read.csv(file = "AutoNA.csv", header = TRUE)

auto.formula = gbm(mpg ~ ., data = auto, interaction.depth = 3, shrinkage = 0.1, n.trees = 100, response.name = "mpg")
print(auto.formula)

r2pmml(auto.formula, "/tmp/gbm.pmml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM