JPMML Example Random Forest

The Predictive Model Markup Language (PMML) developed by the Data Mining Group is a standardized XML-based representation of mining models to be used and shared across languages or tools. The standardized definition allows a classification model trained with R to be used with Storm for example. Many projects related to Big Data have some support for PMML, which is often implemented by JPMML.

Train and Export from R

For this example we train a random forest model based on the iris data set in R. The data set is divided into two samples for training and testing.

Now that we have our model we can use the PMML implementation of R to store it a file. Since PMML is XML based we also need the XML library of R to write the model into a file.

Run Model in Java with JPMML

Now that we have our trained model we take JPMML to exercise it on the same input. Random Forest, although a kind of tree model, does not count as such in a strict manner. It is a MiningModel for classification in the PMML definition.

Here we create an explicit  ModelEvaluator for a MiningModel. For a more general implementation the  ModelEvalutorFactory could be used. The  PMML  class also has a  getModels() function to extract the given model from the definition found in the XML.

The model evaluator can now be used to classify the input data. We use the same iris data we used to train the model for demo purposes. Each model specifies the required input fields and their value types. Here the readArgumentsFromLine function is being used to set the parameters necessary prior to the evaluation. Not much more is required. After we have set the parameters the model can successfully be evaluated. The result again can be read from the model itself.

The output would look like this:

Further Readings

7 thoughts on “JPMML Example Random Forest”

  1. Hi Henning,

    I was testing your code but got stuck on using JPMML. Two files you were trying to import in your java code,

    > — import org.dmg.pmml.MiningModel;

    > — import org.dmg.pmml.PMML;

    doesn’t exist in the package of the version you had in pom.xml. Could you please give me some info on this.

    1. Hi,
      sorry for the late reply, but I checked and they are included as part of org.jpmml:pmml-model:1.1.12 dependency specified in the pom. What is the error you are seeing?

  2. Hi, thank you for the awesome tutorial! It says prepare is undefined in ModelEvaluator.
    Do you have link of github for this project?

  3. Hey, I need JPMML Example for Support Vectore Machine Model. Please provide a working sample example which can consume PMML with Support Vectore Machine Model

Leave a Reply

Your email address will not be published. Required fields are marked *