3

i have build a python file based on randomforestclassifier and made a PMML model, now we need to use this PMML in Java to classify the data into 2 catagories..but this is new to me and i don't know how to handle the java part..

mathilde
  • 49
  • 1
  • 1
  • 2

4 Answers4

3

Google: pmml java

Second Link is https://github.com/jpmml/jpmml-evaluator

There you have a library with examples. So try it and come back if you have problems.

Florian H
  • 3,052
  • 2
  • 14
  • 25
  • 1
    I personally also found it useful to look at the following link for additional example code, in particular the part that runs if the optimize condition is set to true, if you want to speed things up: https://github.com/jpmml/jpmml-evaluator/blob/master/pmml-evaluator-example/src/main/java/org/jpmml/evaluator/EvaluationExample.java – Dennis Soemers Apr 06 '17 at 14:04
  • 3
    Please consider that the suggested library is AGPL licensed. In some environments using AGPL libraries is not an option. A commercial license is also available. – Erik van Oosten Jul 23 '19 at 13:49
0

Another choice is PMML4S that is implemented in Scala, but you are free to use it by Scala or Java API. It's very easy to use, for example:

import org.pmml4s.model.Model;

Model model = Model.fromFile("/the/pmml/file/path");
Object result = model.predict(data)

The data could be in Map, Array, A String in JSON, Series, the result type is same as the input. For details how to use PMML4S in Java, you could see the example: https://github.com/autodeployai/pmml4s/blob/master/src/test/java/org/pmml4s/model/JModelTest.java

PredictFuture
  • 216
  • 2
  • 6
0

There is a demo base on high performance and light framework Vert.x: vertx-pmml

Just config router via Json like this:

"route": {
        "/predict/iris": {
            "access": "public",
            "method": "POST",
            "actor": "evaluter.predict",
            "ext": {
                "name": "IRIS_SVC",
                "pmml": ".\\config\\model_svc.pmml",
                "version": "1.0.0"
            }
        }
...

Then prefect your feature handler(not nescessary if feed a json map match your pmml inputfields):

//src/main/java/com/hirisklab/evaluate/evaluator/actor/EvaluateImpl.java
private Future<Map<String, Object>> Featurelize(JsonObject data) {
    Promise<Map<String, Object>> promise = Promise.promise();
    try {
        Map<String, Object> feature = data.getMap();
        // TODO: featurelize with pmml input fields...
        promise.complete(feature);
    } catch (Exception e) {
        promise.fail(e);
    }
    return promise.future();
}

The action assigned in router(actor field) will handle at here:

//src/main/java/com/hirisklab/evaluate/evaluator/actor/EvaluateImpl.java
public void predict(JsonObject data, Handler<AsyncResult<EvaluateResponse<JsonObject>>> handler) {
    Promise<EvaluateResponse<JsonObject>> promise = Promise.promise();
    try {
        JsonObject profile = Optional.ofNullable(data.getJsonObject("_EXT")).orElseThrow(() -> EvaluateException.InvalidConfigException);
        EvaluaterFactory.getEvaluater(profile).onSuccess(evaluater -> {
            Featurelize(data.getJsonObject("data")).onSuccess(feature -> {
                evaluater.predict(feature).onSuccess(result -> {
                    promise.complete(new EvaluateResponse<JsonObject>(new JsonObject().put("raw", result)));
                }).onFailure(f -> promise.fail(EvaluateException.FailedToPredict));
            }).onFailure(f -> promise.fail(EvaluateException.FailedToFeaturelize));
        }).onFailure(f -> promise.fail(EvaluateException.FailedToLoadEvaluator));
    } catch (Exception e) {
        e.printStackTrace();
        promise.fail(e);
    }
    handler.handle(promise.future());
}
Musen Li
  • 1
  • 1
0

Here is the simple code that might help you to get the direction:

// This will load the PMML file
Evaluator evaluator = new LoadingModelEvaluatorBuilder()
                .load(new File("path\\file.pmml"))
                .build();


// The internal self check
evaluator.verify();
System.out.println("PMML Loaded");


// This will create the `actual` pipeline from the PMML and load in java
TransformerBuilder pmmlTransformerBuilder = new TransformerBuilder(evaluator)
                .withTargetCols()
                .withOutputCols()
                .exploded(true);


System.out.println("Building in...");
Transformer pmmlTransformer = pmmlTransformerBuilder.build();


// Now we are loading the file and converting that into the data frame so that
// we can use it to transform into the prediction into the PMML model
Dataset<Row> DF = sparkSession.read()
                .option("header", true)
                .option("inferSchema", true)
                .csv("path\\file.csv");
DF.printSchema();


// This will predict the new data points from the pipeline
Dataset<Row> result = pmmlTransformer.transform(DF);

NOTE: Make sure, you are loading the correct csv file there. There should not be any change in the column headers otherwise it will show an error.

Here is the link: JPMML-Evaluator (the same which is provided by Florian) to explore more about this.

Aayush Shah
  • 381
  • 2
  • 11