# Examples of wrapping open source machine learning software in PMML?

Does anyone know of examples of wrapping an existing piece of supervised learning software to output models in PMML format? Of particular interest are learners that just take in labeled vectors of numbers as training data and put out models that are pretty much just coefficient vectors (liblinear, SVMlight, BXRtrain, BOW, etc.). That is, they don't have any smarts about data types, ranges of legal values of features, etc.: something else is assumed to deal with that and present the learner with appropriate numeric vectors.

For such software, all the interesting data dictionary stuff would need to be supplied alongside the input data if it's going to show up in PMML model that's output. There's nothing conceptually difficult about this: what I'm curious to see is if there's any conventions, design patterns, etc. that have grown up in the PMML community for doing this.

The PMML website list of software that either consumes or produces models in PMML, but this is mostly commercial closed source software. The programs with open source versions listed there (Rapidminer and WEKA that I can spot) are rather complex data mining suites. What I'd like to see an example of is a a minimalist wrapping of a simple one-trick pony kind of learner.

• A very good question. I've given up trying to convince people to utilize PMML. It's often not considered very difficult to just code one's own model description syntax, especially for easily parametrized models such as decision trees, SVMs, and GLMs. – Iterator Aug 25 '11 at 4:06

I believe Rattle and Weka are both open source and support PMML.

• Rattle (really just a front end for R) and Weka indeed both support PMML, but both are large and complex systems. I'm wondering someone wrapping a very simple learner that produces one type of model. – DavidDLewis Aug 11 '11 at 4:07
• But you can have a very simple learner in R or Weka and wrap PMML on it. Or do you mean that you want an example of a small code that parses PMML and performs the algorithm? – highBandWidth Aug 11 '11 at 16:29
• I'm interested in what a developer with good design instincts would do if they are given a piece of existing machine learning code and told to wrap it so that it outputs trained models in PMML format. It's not rocket science, but if someone has already worked through some of the issues it would be interesting to see what they did. – DavidDLewis Aug 15 '11 at 14:19

The pmml package for R (used by Rattle, which is mentioned in highBandWidth's answer), provides a fairly transparent look at how to turn a model into PMML output.

In the pmml package reference manual, the example of building a linear model for the iris data set and then producing PMML is given:

> library("pmml")
> (iris.lm <- lm(Sepal.Length ~ ., data=iris))
> pmml(iris.lm)


This will produce the following PMML:

<PMML version="3.2" xmlns="http://www.dmg.org/PMML-3_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dmg.org/PMML-3_2 http://www.dmg.org/v3-2/pmml-3-2.xsd">
<Extension name="user" value="user" extender="Rattle/PMML"/>
<Application name="Rattle/PMML" version="1.2.27"/>
<Timestamp>2011-08-27 23:17:42</Timestamp>
<DataField name="Sepal.Length" optype="continuous" dataType="double"/>
<DataField name="Sepal.Width" optype="continuous" dataType="double"/>
<DataField name="Petal.Length" optype="continuous" dataType="double"/>
<DataField name="Petal.Width" optype="continuous" dataType="double"/>
<DataField name="Species" optype="categorical" dataType="string">
<Value value="setosa"/>
<Value value="versicolor"/>
<Value value="virginica"/>
</DataField>
<RegressionModel modelName="Linear_Regression_Model" functionName="regression" algorithmName="least squares" targetFieldName="Sepal.Length">
<MiningSchema>
<MiningField name="Sepal.Length" usageType="predicted"/>
<MiningField name="Sepal.Width" usageType="active"/>
<MiningField name="Petal.Length" usageType="active"/>
<MiningField name="Petal.Width" usageType="active"/>
<MiningField name="Species" usageType="active"/>
</MiningSchema>
<RegressionTable intercept="2.17126629215507">
<NumericPredictor name="Sepal.Width" exponent="1" coefficient="0.495888938388551"/>
<NumericPredictor name="Petal.Length" exponent="1" coefficient="0.829243912234806"/>
<NumericPredictor name="Petal.Width" exponent="1" coefficient="-0.315155173326474"/>
<CategoricalPredictor name="Species" value="setosa" coefficient="0"/>
<CategoricalPredictor name="Species" value="versicolor" coefficient="-0.72356195778073"/>
<CategoricalPredictor name="Species" value="virginica" coefficient="-1.02349781449083"/>
</RegressionTable>
</RegressionModel>
</PMML>


## Source Code

The relevant source code for this linear model is in the pmml package pmml.R and pmml.lm.R files. As will be the case for any PMML producer, it basically reads model parameters (here the model is in iris.lm), and then builds up the XML nodes from the model data.

The code in pmml.lm.R is pretty straightforward, and basically node-by-node builds up the PMML.

Below are some of the queries on the data model that are used (indirectly) in pmml.lm.R:

> terms <- attributes(iris.lm$terms) > terms$dataClasses
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
"numeric"    "numeric"    "numeric"    "numeric"     "factor"
> iris.lm$xlevels$Species
[1] "setosa"     "versicolor" "virginica"

> iris.lm\$coefficients
(Intercept)       Sepal.Width      Petal.Length       Petal.Width Speciesversicolor  Speciesvirginica
2.1712663         0.4958889         0.8292439        -0.3151552        -0.7235620        -1.0234978