From 55c37a046bc0db90eafa63deef2c06d173c0f5c0 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Tue, 25 Dec 2018 16:20:49 +0300 Subject: [PATCH 1/3] Add iris.pmml loading to LogReg model --- examples/pom.xml | 8 ++ .../LogRegFromSparkThroughPMMLExample.java | 108 ++++++++++++++++++ .../src/main/resources/models/spark/iris.pmml | 30 +++++ 3 files changed, 146 insertions(+) create mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/inference/LogRegFromSparkThroughPMMLExample.java create mode 100644 examples/src/main/resources/models/spark/iris.pmml diff --git a/examples/pom.xml b/examples/pom.xml index 429ec794f486d..ecf44e964fcf2 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -122,6 +122,14 @@ ${javassist.version} test + + + org.jpmml + pmml-evaluator + 1.4.4 + + + diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/LogRegFromSparkThroughPMMLExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/LogRegFromSparkThroughPMMLExample.java new file mode 100644 index 0000000000000..30a4498ac8a58 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/LogRegFromSparkThroughPMMLExample.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.examples.ml.inference; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import javax.xml.bind.JAXBException; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.ml.math.primitives.vector.Vector; +import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector; +import org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel; +import org.apache.ignite.ml.selection.scoring.evaluator.BinaryClassificationEvaluator; +import org.apache.ignite.ml.selection.scoring.metric.Accuracy; +import org.apache.ignite.ml.util.MLSandboxDatasets; +import org.apache.ignite.ml.util.SandboxMLCache; +import org.dmg.pmml.PMML; +import org.dmg.pmml.regression.RegressionModel; +import org.dmg.pmml.regression.RegressionTable; +import org.jpmml.model.PMMLUtil; +import org.xml.sax.SAXException; + +/** + * Run logistic regression model loaded from PMML file. The PMML file was generated by Spark MLLib toPMML operator. + *

+ * Code in this example launches Ignite grid and fills the cache with test data points (based on the + * Iris dataset).

+ *

+ * You can change the test data used in this example and re-run it to explore this algorithm further.

+ */ +public class LogRegFromSparkThroughPMMLExample { + /** Run example. */ + public static void main(String[] args) throws FileNotFoundException { + System.out.println(); + System.out.println(">>> Logistic regression model loaded from PMML over partitioned dataset usage example started."); + // Start ignite grid. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(">>> Ignite grid started."); + + IgniteCache dataCache = new SandboxMLCache(ignite) + .fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS); + + LogisticRegressionModel mdl = PMMLParser.load("examples/src/main/resources/models/spark/iris.pmml"); + + System.out.println(">>> Logistic regression model: " + mdl); + + double accuracy = BinaryClassificationEvaluator.evaluate( + dataCache, + mdl, + (k, v) -> v.copyOfRange(1, v.size()), + (k, v) -> v.get(0), + new Accuracy<>() + ); + + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); + } + } + + /** Util class to build the LogReg model. */ + private static class PMMLParser { + /** + * @param path Path. + */ + public static LogisticRegressionModel load(String path) { + try (InputStream is = new FileInputStream(new File(path))) { + PMML pmml = PMMLUtil.unmarshal(is); + + RegressionModel logRegMdl = (RegressionModel)pmml.getModels().get(0); + + RegressionTable regTbl = logRegMdl.getRegressionTables().get(0); + + Vector coefficients = new DenseVector(regTbl.getNumericPredictors().size()); + + for (int i = 0; i < regTbl.getNumericPredictors().size(); i++) + coefficients.set(i, regTbl.getNumericPredictors().get(i).getCoefficient()); + + double interceptor = regTbl.getIntercept(); + + return new LogisticRegressionModel(coefficients, interceptor); + } + catch (IOException | JAXBException | SAXException e) { + e.printStackTrace(); + } + + return null; + } + } +} diff --git a/examples/src/main/resources/models/spark/iris.pmml b/examples/src/main/resources/models/spark/iris.pmml new file mode 100644 index 0000000000000..78f310d8c2aea --- /dev/null +++ b/examples/src/main/resources/models/spark/iris.pmml @@ -0,0 +1,30 @@ + + +
+ + 2018-12-25T15:09:09 +
+ + + + + + + + + + + + + + + + + + + + + + + +
From c8cfec6b9fb59aec67252e8617bd3a6db380a79c Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Tue, 25 Dec 2018 16:56:40 +0300 Subject: [PATCH 2/3] Fixed broken JavaDoc --- examples/pom.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/pom.xml b/examples/pom.xml index ecf44e964fcf2..5ee989db4b7df 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -128,7 +128,23 @@ pmml-evaluator 1.4.4 + + com.fasterxml.jackson.core + jackson-core + 2.7.3 + + + com.fasterxml.jackson.core + jackson-databind + 2.7.3 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.7.3 + From ad9ae4e565ed083484228653bbd377b770688b68 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Wed, 26 Dec 2018 11:36:58 +0300 Subject: [PATCH 3/3] Fixed POM according PMML author comment --- examples/pom.xml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/pom.xml b/examples/pom.xml index 5ee989db4b7df..6320a0fe6e305 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -122,12 +122,13 @@ ${javassist.version} test - + org.jpmml - pmml-evaluator - 1.4.4 + pmml-model + 1.4.7 + com.fasterxml.jackson.core jackson-core