jpmml / jpmml-evaluator

Java Evaluator API for PMML
GNU Affero General Public License v3.0
892 stars 255 forks source link

Java Import PMML #62

Closed sbourzai closed 7 years ago

sbourzai commented 7 years ago

Hi Sir, I've trained a treeClassfication model in Pyspark and i have my pmml file corresponding to my model; I want to import it into a java project, i use Maven and these versions : pmml-model : 1.3.7 pmml-evaluator : 1.1.7 pmml-schema : 1.3.7 pmml-manager : 1.1.20

but i get these errors :

org.dmg.pmml.PMML@78b66d36
java.lang.NullPointerException
    at org.jpmml.manager.XPathUtil.formatXPath(XPathUtil.java:39)
    at org.jpmml.manager.UnsupportedFeatureException.<init>(UnsupportedFeatureException.java:50)
    at org.jpmml.evaluator.ModelEvaluatorFactory.getModelManager(ModelEvaluatorFactory.java:103)
    at org.jpmml.evaluator.ModelEvaluatorFactory.getModelManager(ModelEvaluatorFactory.java:47)
    at org.jpmml.manager.PMMLManager.getModelManager(PMMLManager.java:151)
    at ImportPipeLineModel.main(ImportPipeLineModel.java:78)

Have you any idea ? Thanks a lot

This is my java code :

public class ImportPipeLineModel {

static final String modelName = "MyPMMLPredictions";

static final String testCSVFile = "C:/Users//Documents/Iris.csv";
static final String cvsSplitBy = ",";

static String csvFilePath;

public void resolveFilePaths() throws URISyntaxException {

    URL url = ClassLoader.getSystemResource(testCSVFile);
    csvFilePath = url.getPath();
}

// **** Load my PMML file ****

public final static PMML loadModel(final String file) throws Exception {

    org.dmg.pmml.PMML pmml = null;

    File inputFilePath = new File(file);

    try (InputStream in = new FileInputStream(inputFilePath)) {

        Source source = ImportFilter.apply(new InputSource(in));
        pmml = JAXBUtil.unmarshalPMML(source);
        System.out.println(pmml);
    } catch (Exception e) {
        // logger.error( e.toString() );
        throw e;
    }
    return pmml;
}

/*
 * Now I have loaded my Iris Classifier model as a PMML object so I can
 * start the prediction processing. .......
 */

public static void main(String[] args) {

    try {

        PMML pmml = ImportPipeLineModel.loadModel("C:/Users/sbourzai/Documents/IrisClassifierPMML.xml");

        // Récupérer les differents types des champs définis dans le modèle
        // PMML :
        PMMLManager manager = new PMMLManager(pmml);

        ModelEvaluator<?> modelEvaluator = (ModelEvaluator<?>) manager.getModelManager(modelName,
                ModelEvaluatorFactory.getInstance());
        Evaluator evaluator = modelEvaluator;

        // Get my list of required feature set model needs to predict :

        List<FieldName> requiredModelFeatures = evaluator.getActiveFields();

        try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {

            Map<FieldName, FieldValue> features = new LinkedHashMap<>();
            String line = null;

            long startTime = System.currentTimeMillis();
            int predications = 0;

            while ((line = br.readLine()) != null) {

                predications++;

                String[] tokens = line.split(cvsSplitBy);

                Double[] pfeatures = { Double.valueOf(tokens[0]), Double.valueOf(tokens[1]),
                        Double.valueOf(tokens[2]), Double.valueOf(tokens[3]) };

                // Extraire le Label Target :
                String expectedSpecies = tokens[4];

                // Build the feature set :
                features = JPMMLUtils.buildFeatureSet(evaluator, requiredModelFeatures, pfeatures);

                // Executer la prédiction :
                Map<FieldName, ?> results = evaluator.evaluate(features);

                // Récupérer l'ensemble de réponses prédictives :
                ProbabilityClassificationMap<String> predicatedLabel = (ProbabilityClassificationMap<String>) results
                        .get(evaluator.getTargetField());

                System.out.println("\nPredication");

                for (String key : predicatedLabel.keySet()) {
                    System.out.println(String.format("Predicated species [ %s --> %f ]  - Expected species [ %s ]",
                            key, predicatedLabel.getProbability(key), expectedSpecies));
                }

            }

            System.out.println(String.format("Predicted %d items in %dms", predications,
                    System.currentTimeMillis() - startTime));

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

vruusmann commented 7 years ago

I want to import it into a java project, i use Maven and these versions : pmml-model : 1.3.7 pmml-evaluator : 1.1.7 pmml-schema : 1.3.7 pmml-manager : 1.1.20

These versions DO NOT belong together - you're mixing JPMML-Model 1.3.X with JPMML-Evaluator 1.1.X.

In your project, you only need to declare the org.jpmml:pmml-evaluator dependency. Apache Maven will pull in the correct versions of all other dependencies.

For starters, please see "Installation" and "Usage" sections of the README file: https://github.com/jpmml/jpmml-evaluator#installation https://github.com/jpmml/jpmml-evaluator#usage