Caret Model random forest into PMML error

前端 未结 2 1769
予麋鹿
予麋鹿 2021-01-20 15:36

I would like to export a Caret random forest model using the pmml library so I can use it for predictions in Java. Here is a reproduction of the error I am getting.

相关标签:
2条回答
  • 2021-01-20 16:06

    You cannot invoke the pmml method with train or train.formula types (ie. this is the type of your model.Test object).

    Caret documentation for the train method says that you can access the best model as the finalModel field. You can invoke the pmml method on that object then.

    rf = model.Test$finalModel
    pmml(rf)
    

    Unfortunately, it turns out that Caret specifies the RF model using the "matrix interface" (ie. by setting the x and y fields), not using the more common "formula interface" (ie. by setting the formula field). AFAIK, the "pmml" package does not support the export of such RF models.

    So, looks like your best option is to use a two-level approach. First, use the Caret package to find the most appropriate RF parametrization for your dataset. Second, train the final RF model manually using the "formula interface" with this parametrization.

    0 讨论(0)
  • 2021-01-20 16:09

    You can use the r2pmml package to do the job:

    library("caret")
    library("r2pmml")
    
    data(iris)
    
    train.rf = train(Species ~ ., data = iris, method = "rf")
    print(train.rf)
    r2pmml(train.rf, "/tmp/train-rf.pmml")
    
    0 讨论(0)
提交回复
热议问题