我试图将两个变量的函数添加到R中的PMML中。
我要执行的模型是
y = a + b * exp(Sepal.Width-Sepal.Length)^ 2
我希望PMML的输入为Sepal.Width和Sepal.Length。
我有以下代码来使字段derived_Sepal.Length成为字段,但是我不知道如何使用自定义转换函数,例如exp(Sepal.Width-Sepal.Length)^ 2。
library(pmml)
library(XML)
library(pmmlTransformations)
irisBox <- WrapData(iris)
irisBox <- ZScoreXform(irisBox,"Sepal.Length")
model <- lm(Petal.Width ~ derived_Sepal.Length - Sepal.Width, data=irisBox$data)
pmmlModel <- pmml(model,transforms=irisBox)
pmmlModelEnhanced <- addLT(pmmlModel,namespace="4_2")
saveXML(pmmlModelEnhanced, file=outputPMMLFilename)
任何使用R在PMML中进行数据转换的一般建议或技巧也将不胜感激。
谢谢!
当前,尚没有可用于将任意R表达式转换为PMML的工具。您将必须使用通用R XML API手动编写PMML代码段,然后将其附加到PMML文档,然后再写入文件。
假设您要使用派生字段my_field
:
my_field = (Sepal.Length - Sepal.Width)^2
# Use the my_field in your formula
lm = lm(Species ~ my_field, data = iris)
# Convert the lm() object to an in-memory XML DOM object
lm.pmml = pmml(lm)
# Fix the contents of the PMML/DataDictionary:
# 1) Remove the 'my_field' field definition
# 2) Add `Sepal.Length` and `Sepal.Width` field definitions - you will be referencing them in your custom expression, so they need to be available
lm.pmml = remove_datafield(lm.pmml, "my_field")
lm.pmml = add_datafield(lm.pmml, "Sepal.Width", "double", "continuous")
lm.pmml = add_datafield(lm.pmml, "Sepal.Length", "double", "continuous")
# Fix the contents of the PMML/TransformationDictionary:
# 1) Add 'my_field' field definition
lm.pmml = add_derivedfield(lm.pmml, ..)
# The PMML manipulation is done now, save it to a local filesystem file
saveXML(lm.pmml, outputPMMLFilename)
展望未来,您可能需要关注JPMML-Converter项目,因为自动R到PMML转换是该计划的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句