By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
SmartData Collective
  • Analytics
    AnalyticsShow More
    data science anayst
    Growing Demand for Data Science & Data Analyst Roles
    6 Min Read
    predictive analytics in dropshipping
    Predictive Analytics Helps New Dropshipping Businesses Thrive
    12 Min Read
    data-driven approach in healthcare
    The Importance of Data-Driven Approaches to Improving Healthcare in Rural Areas
    6 Min Read
    analytics for tax compliance
    Analytics Changes the Calculus of Business Tax Compliance
    8 Min Read
    big data analytics in gaming
    The Role of Big Data Analytics in Gaming
    10 Min Read
  • Big Data
  • BI
  • Exclusive
  • IT
  • Marketing
  • Software
Search
© 2008-23 SmartData Collective. All Rights Reserved.
Reading: Converting Arbitrary R Expressions to PMML
Share
Notification Show More
Latest News
ai in automotive industry
AI Is Changing the Automotive Industry Forever
Artificial Intelligence
SMEs Use AI-Driven Financial Software for Greater Efficiency
Artificial Intelligence
data security in big data age
6 Reasons to Boost Data Security Plan in the Age of Big Data
Big Data
data science anayst
Growing Demand for Data Science & Data Analyst Roles
Data Science
ai software development
Key Strategies to Develop AI Software Cost-Effectively
Artificial Intelligence
Aa
SmartData Collective
Aa
Search
  • About
  • Help
  • Privacy
Follow US
© 2008-23 SmartData Collective. All Rights Reserved.
SmartData Collective > Uncategorized > Converting Arbitrary R Expressions to PMML
Uncategorized

Converting Arbitrary R Expressions to PMML

MichaelZeller
Last updated: 2015/10/29 at 4:24 AM
MichaelZeller
8 Min Read
SHARE

The pmmlTransformations R package can be used to transform data and add new features to be used in predictive PMML models.

Contents
How it worksSingle numeric fieldMultiple input fieldsPMML for arbitrary functionsConclusionReferences

In this blog post, we will focus on FunctionXform, a function introduced in version 1.3.0 of pmmlTransformations, and present a few examples of using it to create new data features.

The pmmlTransformations R package can be used to transform data and add new features to be used in predictive PMML models.

In this blog post, we will focus on FunctionXform, a function introduced in version 1.3.0 of pmmlTransformations, and present a few examples of using it to create new data features.

More Read

big data improves

3 Ways Big Data Improves Leadership Within Companies

IT Is Not Analytics. Here’s Why.
Romney Invokes Analytics in Rebuke of Trump
WEF Davos 2016: Top 100 CEO bloggers
In Memoriam: Robin Fray Carey

How it works

Transformations in the pmmlTransformations package work in the following manner: given a WrapData object and a transformation name, the code calculates data for a new feature and creates a new WrapData object. This new object is then passed in as the data argument when training an R model with a compatible R package. When PMML is produced with pmml::pmml(), the transformation is inserted into the LocalTransformations node as a DerivedField. Any original fields used by transformations are added to the appropriate nodes in the resulting PMML file.

While other transformations in the package transform only one field, FunctionXform makes it possible to use multiple data fields and functions to produce a new feature.

Note that while FunctionXform is part of the pmmlTransformations package, the code to produce PMML from R is in the pmml package. The following examples require both packages to be installed to work.

To make tables more readable in this blog post, we are using the kable function (part of knitr).

Single numeric field

Using the iris dataset as an example, let’s construct a new numeric feature by transforming one variable.

First, load the required libraries:

library(pmml)
library(pmmlTransformations)
library(knitr)

Then load the data and display the first 3 lines:

data(iris)
kable(head(iris,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa

Create the irisBox wrapper object with WrapData:

irisBox <- WrapData(iris)

irisBox contains the data and transform information that will be used to produce PMML later. The original data is in irisBox$data. Any new features created with a transformation are added as columns to this data frame.

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa

Transform and field information is in irisBox$fieldData. The fieldData data frame contains information on every field in the dataset, as well as every transform used. The functionXform column contains expressions used in the FunctionXform transform. Here we’ll show only a few of the columns:

kable(irisBox$fieldData[,1:5])
 typedataTypeorigFieldNamesampleMinsampleMax
Sepal.LengthoriginalnumericNANANA
Sepal.WidthoriginalnumericNANANA
Petal.LengthoriginalnumericNANANA
Petal.WidthoriginalnumericNANANA
SpeciesoriginalfactorNANANA

Now add a new feature, Sepal.Length.Sqrt, using FunctionXform:

irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length",
newFieldName="Sepal.Length.Sqrt",
formulaText="sqrt(Sepal.Length)")

The new feature is calculated and added as a column to the irisBox$data data frame:

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesSepal.Length.Sqrt
5.13.51.40.2setosa2.258318
4.93.01.40.2setosa2.213594
4.73.21.30.2setosa2.167948

irisBox$fieldData now contains a new row with the transformation expression in the functionXform column:

kable(irisBox$fieldData[6,c(1:3,14)])
 typedataTypeorigFieldNamefunctionXform
Sepal.Length.SqrtderivednumericSepal.Lengthsqrt(Sepal.Length)

Construct a linear model to predict Petal.Width using this new feature, and convert it to PMML:

fit <- lm(Petal.Width ~ Sepal.Length.Sqrt, data=irisBox$data)
fit_pmml <- pmml(fit, transform=irisBox)

Since the model predicts Petal.Width using a variable based on Sepal.Length, Sepal.Length will be added to the DataDictionary and MiningSchema nodes in the resulting PMML. We can take a look at the relevant parts of the output like so:

fit_pmml[[2]] #Data Dictionary node
#>
#>
#>
#>
fit_pmml[[3]][[1]] #Mining Schema node
#>
#>
#>
#>

The LocalTransformations node contains Sepal.Length.Sqrt as a derived field:

fit_pmml[[3]][[3]]
#>
#>
#>
#>
#>
#>
#>

The PMML model can now be deployed and consumed. For any input data, the new Sepal.Length.Sqrt feature will be created when the data is scored against the model.

Multiple input fields

It is also possible to create new features by combining several fields. Using the same iris dataset, let’s create a new field using squares of Sepal.Length and Petal.Length:

irisBox <- WrapData(iris)
irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length,Petal.Length",
newFieldName="Squared.Length.Ratio",
formulaText="(Sepal.Length / Petal.Length)^2")

As before, the new field is added as a column to the irisBox$data data frame:

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesSquared.Length.Ratio
5.13.51.40.2setosa13.27041
4.93.01.40.2setosa12.25000
4.73.21.30.2setosa13.07101

Fit a linear model for Petal.Length using this new feature, and convert it to PMML:

fit <- lm(Petal.Width ~ Squared.Length.Ratio, data=irisBox$data)
fit_pmml <- pmml(fit, transform=irisBox)

The PMML will contain Sepal.Length and Petal.Length in the DataDictionary and MiningSchema, since these were used in FormulaXform:

fit_pmml[[2]] #Data Dictionary node
#>
#>
#>
#>
#>
fit_pmml[[3]][[1]] #Mining Schema node
#>
#>
#>
#>
#>

The Local.Transformations node contains Squared.Length.Ratio as a derived field:

fit_pmml[[3]][[3]]
#>
#>
#>
#>
#>
#>
#>
#> 2
#>
#>
#>

PMML for arbitrary functions

The function functionToPMML (part of the pmml package) makes it possible to convert an R expression into PMML directly, without creating a model or calculating values. This can be useful for debugging.

As long as the expression passed to the function is a valid R expression (e.g., no unbalanced parentheses), it can contain arbitrary function names not defined in R. Constants in the expression passed to FunctionXform are always assumed to be of type double. Variables in the expression are always assumed to be field names, and not substituted. That is, even if x has a value in the R environment, the resulting expression will still use x.

functionToPMML("1 + 2")
#>
#> 1
#> 2
#>

x <- 3
functionToPMML("foo(bar(x * y))")
#>
#>
#>
#>
#>
#>
#>
#>

functionToPMML("if(a<2) else if (a>3) {'four'} else ")
#>
#>
#>
#> 2
#>
#>
#>
#> 3
#>
#>
#>
#>
#> 3
#>
#> four
#> 5
#>
#>

Conclusion

functionXform makes it possible to easily create new features for PMML models with R.

The pmmlTransformations functionXform vignette contains additional examples, including transforming categorical data, using transformed features in another transform, unsupported functions, and notes on limitations of the function.

References

  • pmmlTransformations on CRAN
  • PMML 4.2.1 official specification
  • In-depth discussion of pmmlTransformations

MichaelZeller October 29, 2015
Share this Article
Facebook Twitter Pinterest LinkedIn
Share

Follow us on Facebook

Latest News

ai in automotive industry
AI Is Changing the Automotive Industry Forever
Artificial Intelligence
SMEs Use AI-Driven Financial Software for Greater Efficiency
Artificial Intelligence
data security in big data age
6 Reasons to Boost Data Security Plan in the Age of Big Data
Big Data
data science anayst
Growing Demand for Data Science & Data Analyst Roles
Data Science

Stay Connected

1.2k Followers Like
33.7k Followers Follow
222 Followers Pin

You Might also Like

big data improves
Big DataJobsKnowledge ManagementUncategorized

3 Ways Big Data Improves Leadership Within Companies

6 Min Read
Image
Uncategorized

IT Is Not Analytics. Here’s Why.

7 Min Read

Romney Invokes Analytics in Rebuke of Trump

4 Min Read

WEF Davos 2016: Top 100 CEO bloggers

14 Min Read

SmartData Collective is one of the largest & trusted community covering technical content about Big Data, BI, Cloud, Analytics, Artificial Intelligence, IoT & more.

AI chatbots
AI Chatbots Can Help Retailers Convert Live Broadcast Viewers into Sales!
Chatbots
ai in ecommerce
Artificial Intelligence for eCommerce: A Closer Look
Artificial Intelligence

Quick Link

  • About
  • Contact
  • Privacy
Follow US

© 2008-23 SmartData Collective. All Rights Reserved.

Removed from reading list

Undo
Go to mobile version
Welcome Back!

Sign in to your account

Lost your password?