jpmml / jpmml-transpiler

Java Transpiler (Translator + Compiler) API for PMML
GNU Affero General Public License v3.0
28 stars 2 forks source link

NPE when the member models of an ensemble classification model do not specify target categories #6

Open ahmed-shariff opened 4 years ago

ahmed-shariff commented 4 years ago

For some models exported with jpmml-sklearn, when I am running the transpiler I get the following exception:

java.lang.NullPointerException
    at org.jpmml.translator.ModelTranslator.getTargetCategories (ModelTranslator.java:265)
    at org.jpmml.translator.tree.TreeModelTranslator.translateClassifier (TreeModelTranslator.java:156)
    at org.jpmml.translator.ModelTranslator.createEvaluateMethod (ModelTranslator.java:128)
    at org.jpmml.translator.ModelTranslator.translate (ModelTranslator.java:102)
    at org.jpmml.translator.PMMLObjectUtil.createExpression (PMMLObjectUtil.java:345)
    at org.jpmml.translator.PMMLObjectUtil.constructObject (PMMLObjectUtil.java:193)
    at org.jpmml.translator.PMMLObjectUtil.createObject (PMMLObjectUtil.java:157)
    at org.jpmml.translator.PMMLObjectUtil.createExpression (PMMLObjectUtil.java:355)
    at org.jpmml.translator.PMMLObjectUtil.constructObject (PMMLObjectUtil.java:186)
    at org.jpmml.translator.PMMLObjectUtil.createObject (PMMLObjectUtil.java:157)
    at org.jpmml.translator.PMMLObjectUtil.createExpression (PMMLObjectUtil.java:355)
    at org.jpmml.translator.PMMLObjectUtil.initialize (PMMLObjectUtil.java:404)
    at org.jpmml.translator.PMMLObjectUtil.initializeObject (PMMLObjectUtil.java:210)
    at org.jpmml.translator.PMMLObjectUtil.createObject (PMMLObjectUtil.java:159)
    at org.jpmml.translator.PMMLObjectUtil.createBuilderMethod (PMMLObjectUtil.java:148)
    at org.jpmml.translator.PMMLObjectUtil.createExpression (PMMLObjectUtil.java:350)
    at org.jpmml.translator.PMMLObjectUtil.initialize (PMMLObjectUtil.java:391)
    at org.jpmml.translator.PMMLObjectUtil.initializeObject (PMMLObjectUtil.java:210)
    at org.jpmml.translator.PMMLObjectUtil.createDefaultConstructor (PMMLObjectUtil.java:121)
    at org.jpmml.transpiler.TranspilerUtil.translate (TranspilerUtil.java:67)
    at com.umanitoba.hci.washinghands.App.main (App.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:748)

The pmml and pickle file of the model in question: file.zip

vruusmann commented 4 years ago

This is a legit bug now - child model translators (TreeModel element) should be inheriting the list of target categories from the parent model translator (MiningModel element), but they don't.

This bug seems to manifest itself when the data type of the target field is something else than string (in your sample PMML file the data type of the "class" field is double).

As a workaround, you should change the data type of the "class" data column to string, and re-train the model. It should work then.

A proper fix is already implemented in the JPMML-Evaluator library version 1.5.0-SNAPSHOT (current development branch, not a release version). It should be published in a week or two.

vruusmann commented 4 years ago

This bug affects Scikit-Learn's RandomForestClassifier. It shouldn't affect any other model type.

ahmed-shariff commented 4 years ago

Thank you very much

ahmed-shariff commented 4 years ago

The problem still seems to persist with the newer versions: I tried re-running some of the random-forest models with the class datatype set to int/double and also set to strings. And I am still getting the same error.

vruusmann commented 4 years ago

The issue is still in "open" state, which indicates that I haven't had time to work on it yet.

The JPMML-Evaluator library version 1.5(.1) introduced the required groundwork, but there's still a minor JPMML-Transpiler code change needed.