From commits-return-120685-archive-asf-public=cust-asf.ponee.io@ignite.apache.org Mon Oct 1 07:55:16 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id A1642180789 for ; Mon, 1 Oct 2018 07:55:13 +0200 (CEST) Received: (qmail 51327 invoked by uid 500); 1 Oct 2018 05:55:12 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 51239 invoked by uid 99); 1 Oct 2018 05:55:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Oct 2018 05:55:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7D65EE0057; Mon, 1 Oct 2018 05:55:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Mon, 01 Oct 2018 05:55:15 -0000 Message-Id: <748d4108ff0f46f5b40392bb515bec42@git.apache.org> In-Reply-To: <0bb45735132e44128394cd157d3a09e1@git.apache.org> References: <0bb45735132e44128394cd157d3a09e1@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [05/21] ignite git commit: IGNITE-9711: [ML] Remove IgniteThread wrapper from ml examples http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/svm/multiclass/SVMMultiClassClassificationExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/svm/multiclass/SVMMultiClassClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/svm/multiclass/SVMMultiClassClassificationExample.java index b7ca448..520b8cc 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/svm/multiclass/SVMMultiClassClassificationExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/svm/multiclass/SVMMultiClassClassificationExample.java @@ -32,7 +32,6 @@ import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector; import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer; import org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel; import org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationTrainer; -import org.apache.ignite.thread.IgniteThread; /** * Run SVM multi-class classification trainer ({@link SVMLinearMultiClassClassificationModel}) over distributed dataset @@ -59,106 +58,100 @@ public class SVMMultiClassClassificationExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - SVMMultiClassClassificationExample.class.getSimpleName(), () -> { - IgniteCache dataCache = new TestCache(ignite).getVectors(data); + IgniteCache dataCache = new TestCache(ignite).getVectors(data); - SVMLinearMultiClassClassificationTrainer trainer = new SVMLinearMultiClassClassificationTrainer(); + SVMLinearMultiClassClassificationTrainer trainer = new SVMLinearMultiClassClassificationTrainer(); - SVMLinearMultiClassClassificationModel mdl = trainer.fit( - ignite, - dataCache, - (k, v) -> { - double[] arr = v.asArray(); - return VectorUtils.of(Arrays.copyOfRange(arr, 1, arr.length)); - }, - (k, v) -> v.get(0) - ); + SVMLinearMultiClassClassificationModel mdl = trainer.fit( + ignite, + dataCache, + (k, v) -> { + double[] arr = v.asArray(); + return VectorUtils.of(Arrays.copyOfRange(arr, 1, arr.length)); + }, + (k, v) -> v.get(0) + ); - System.out.println(">>> SVM Multi-class model"); - System.out.println(mdl.toString()); + System.out.println(">>> SVM Multi-class model"); + System.out.println(mdl.toString()); - MinMaxScalerTrainer normalizationTrainer = new MinMaxScalerTrainer<>(); + MinMaxScalerTrainer normalizationTrainer = new MinMaxScalerTrainer<>(); - IgniteBiFunction preprocessor = normalizationTrainer.fit( - ignite, - dataCache, - (k, v) -> { - double[] arr = v.asArray(); - return VectorUtils.of(Arrays.copyOfRange(arr, 1, arr.length)); - } - ); - - SVMLinearMultiClassClassificationModel mdlWithNormalization = trainer.fit( - ignite, - dataCache, - preprocessor, - (k, v) -> v.get(0) - ); + IgniteBiFunction preprocessor = normalizationTrainer.fit( + ignite, + dataCache, + (k, v) -> { + double[] arr = v.asArray(); + return VectorUtils.of(Arrays.copyOfRange(arr, 1, arr.length)); + } + ); - System.out.println(">>> SVM Multi-class model with minmaxscaling"); - System.out.println(mdlWithNormalization.toString()); + SVMLinearMultiClassClassificationModel mdlWithNormalization = trainer.fit( + ignite, + dataCache, + preprocessor, + (k, v) -> v.get(0) + ); - System.out.println(">>> ----------------------------------------------------------------"); - System.out.println(">>> | Prediction\t| Prediction with Normalization\t| Ground Truth\t|"); - System.out.println(">>> ----------------------------------------------------------------"); + System.out.println(">>> SVM Multi-class model with minmaxscaling"); + System.out.println(mdlWithNormalization.toString()); - int amountOfErrors = 0; - int amountOfErrorsWithNormalization = 0; - int totalAmount = 0; + System.out.println(">>> ----------------------------------------------------------------"); + System.out.println(">>> | Prediction\t| Prediction with Normalization\t| Ground Truth\t|"); + System.out.println(">>> ----------------------------------------------------------------"); - // Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix - int[][] confusionMtx = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; - int[][] confusionMtxWithNormalization = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; + int amountOfErrors = 0; + int amountOfErrorsWithNormalization = 0; + int totalAmount = 0; - try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { - for (Cache.Entry observation : observations) { - double[] val = observation.getValue().asArray(); - double[] inputs = Arrays.copyOfRange(val, 1, val.length); - double groundTruth = val[0]; + // Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix + int[][] confusionMtx = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; + int[][] confusionMtxWithNormalization = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; - double prediction = mdl.apply(new DenseVector(inputs)); - double predictionWithNormalization = mdlWithNormalization.apply(new DenseVector(inputs)); + try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { + for (Cache.Entry observation : observations) { + double[] val = observation.getValue().asArray(); + double[] inputs = Arrays.copyOfRange(val, 1, val.length); + double groundTruth = val[0]; - totalAmount++; + double prediction = mdl.apply(new DenseVector(inputs)); + double predictionWithNormalization = mdlWithNormalization.apply(new DenseVector(inputs)); - // Collect data for model - if(groundTruth != prediction) - amountOfErrors++; + totalAmount++; - int idx1 = (int)prediction == 1 ? 0 : ((int)prediction == 3 ? 1 : 2); - int idx2 = (int)groundTruth == 1 ? 0 : ((int)groundTruth == 3 ? 1 : 2); + // Collect data for model + if(groundTruth != prediction) + amountOfErrors++; - confusionMtx[idx1][idx2]++; + int idx1 = (int)prediction == 1 ? 0 : ((int)prediction == 3 ? 1 : 2); + int idx2 = (int)groundTruth == 1 ? 0 : ((int)groundTruth == 3 ? 1 : 2); - // Collect data for model with minmaxscaling - if(groundTruth != predictionWithNormalization) - amountOfErrorsWithNormalization++; + confusionMtx[idx1][idx2]++; - idx1 = (int)predictionWithNormalization == 1 ? 0 : ((int)predictionWithNormalization == 3 ? 1 : 2); - idx2 = (int)groundTruth == 1 ? 0 : ((int)groundTruth == 3 ? 1 : 2); + // Collect data for model with minmaxscaling + if(groundTruth != predictionWithNormalization) + amountOfErrorsWithNormalization++; - confusionMtxWithNormalization[idx1][idx2]++; + idx1 = (int)predictionWithNormalization == 1 ? 0 : ((int)predictionWithNormalization == 3 ? 1 : 2); + idx2 = (int)groundTruth == 1 ? 0 : ((int)groundTruth == 3 ? 1 : 2); - System.out.printf(">>> | %.4f\t\t| %.4f\t\t\t\t\t\t| %.4f\t\t|\n", prediction, predictionWithNormalization, groundTruth); - } - System.out.println(">>> ----------------------------------------------------------------"); - System.out.println("\n>>> -----------------SVM model-------------"); - System.out.println("\n>>> Absolute amount of errors " + amountOfErrors); - System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double)totalAmount)); - System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx)); + confusionMtxWithNormalization[idx1][idx2]++; - System.out.println("\n>>> -----------------SVM model with Normalization-------------"); - System.out.println("\n>>> Absolute amount of errors " + amountOfErrorsWithNormalization); - System.out.println("\n>>> Accuracy " + (1 - amountOfErrorsWithNormalization / (double)totalAmount)); - System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtxWithNormalization)); - - System.out.println(">>> Linear regression model over cache based dataset usage example completed."); + System.out.printf(">>> | %.4f\t\t| %.4f\t\t\t\t\t\t| %.4f\t\t|\n", prediction, predictionWithNormalization, groundTruth); } - }); + System.out.println(">>> ----------------------------------------------------------------"); + System.out.println("\n>>> -----------------SVM model-------------"); + System.out.println("\n>>> Absolute amount of errors " + amountOfErrors); + System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double)totalAmount)); + System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx)); + + System.out.println("\n>>> -----------------SVM model with Normalization-------------"); + System.out.println("\n>>> Absolute amount of errors " + amountOfErrorsWithNormalization); + System.out.println("\n>>> Accuracy " + (1 - amountOfErrorsWithNormalization / (double)totalAmount)); + System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtxWithNormalization)); - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> Linear regression model over cache based dataset usage example completed."); + } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeClassificationTrainerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeClassificationTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeClassificationTrainerExample.java index 28a5fbc..652b293 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeClassificationTrainerExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeClassificationTrainerExample.java @@ -26,7 +26,6 @@ import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Example of using distributed {@link DecisionTreeClassificationTrainer}. @@ -53,58 +52,49 @@ public class DecisionTreeClassificationTrainerExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - DecisionTreeClassificationTrainerExample.class.getSimpleName(), () -> { + // Create cache with training data. + CacheConfiguration trainingSetCfg = new CacheConfiguration<>(); + trainingSetCfg.setName("TRAINING_SET"); + trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10)); - // Create cache with training data. - CacheConfiguration trainingSetCfg = new CacheConfiguration<>(); - trainingSetCfg.setName("TRAINING_SET"); - trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10)); + IgniteCache trainingSet = ignite.createCache(trainingSetCfg); - IgniteCache trainingSet = ignite.createCache(trainingSetCfg); + Random rnd = new Random(0); - Random rnd = new Random(0); + // Fill training data. + for (int i = 0; i < 1000; i++) + trainingSet.put(i, generatePoint(rnd)); - // Fill training data. - for (int i = 0; i < 1000; i++) - trainingSet.put(i, generatePoint(rnd)); + // Create classification trainer. + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0); - // Create classification trainer. - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0); + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + trainingSet, + (k, v) -> VectorUtils.of(v.x, v.y), + (k, v) -> v.lb + ); - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, - trainingSet, - (k, v) -> VectorUtils.of(v.x, v.y), - (k, v) -> v.lb - ); + System.out.println(">>> Decision tree classification model: " + mdl); - System.out.println(">>> Decision tree classification model: " + mdl); + // Calculate score. + int correctPredictions = 0; + for (int i = 0; i < 1000; i++) { + LabeledPoint pnt = generatePoint(rnd); - // Calculate score. - int correctPredictions = 0; - for (int i = 0; i < 1000; i++) { - LabeledPoint pnt = generatePoint(rnd); + double prediction = mdl.apply(VectorUtils.of(pnt.x, pnt.y)); + double lbl = pnt.lb; - double prediction = mdl.apply(VectorUtils.of(pnt.x, pnt.y)); - double lbl = pnt.lb; + if (i %50 == 1) + System.out.printf(">>> test #: %d\t\t predicted: %.4f\t\tlabel: %.4f\n", i, prediction, lbl); - if (i %50 == 1) - System.out.printf(">>> test #: %d\t\t predicted: %.4f\t\tlabel: %.4f\n", i, prediction, lbl); + if (prediction == lbl) + correctPredictions++; + } - if (prediction == lbl) - correctPredictions++; - } - - System.out.println(">>> Accuracy: " + correctPredictions / 10.0 + "%"); - - System.out.println(">>> Decision tree classification trainer example completed."); - }); - - igniteThread.start(); - - igniteThread.join(); + System.out.println(">>> Accuracy: " + correctPredictions / 10.0 + "%"); + System.out.println(">>> Decision tree classification trainer example completed."); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeRegressionTrainerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeRegressionTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeRegressionTrainerExample.java index 301df10..2a89c7e 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeRegressionTrainerExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/DecisionTreeRegressionTrainerExample.java @@ -25,7 +25,6 @@ import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.tree.DecisionTreeNode; import org.apache.ignite.ml.tree.DecisionTreeRegressionTrainer; -import org.apache.ignite.thread.IgniteThread; /** * Example of using distributed {@link DecisionTreeRegressionTrainer}. @@ -53,51 +52,43 @@ public class DecisionTreeRegressionTrainerExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - DecisionTreeRegressionTrainerExample.class.getSimpleName(), () -> { + // Create cache with training data. + CacheConfiguration trainingSetCfg = new CacheConfiguration<>(); + trainingSetCfg.setName("TRAINING_SET"); + trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10)); - // Create cache with training data. - CacheConfiguration trainingSetCfg = new CacheConfiguration<>(); - trainingSetCfg.setName("TRAINING_SET"); - trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10)); + IgniteCache trainingSet = ignite.createCache(trainingSetCfg); - IgniteCache trainingSet = ignite.createCache(trainingSetCfg); + // Fill training data. + generatePoints(trainingSet); - // Fill training data. - generatePoints(trainingSet); + // Create regression trainer. + DecisionTreeRegressionTrainer trainer = new DecisionTreeRegressionTrainer(10, 0); - // Create regression trainer. - DecisionTreeRegressionTrainer trainer = new DecisionTreeRegressionTrainer(10, 0); + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + trainingSet, + (k, v) -> VectorUtils.of(v.x), + (k, v) -> v.y + ); - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, - trainingSet, - (k, v) -> VectorUtils.of(v.x), - (k, v) -> v.y - ); + System.out.println(">>> Decision tree regression model: " + mdl); - System.out.println(">>> Decision tree regression model: " + mdl); + System.out.println(">>> ---------------------------------"); + System.out.println(">>> | Prediction\t| Ground Truth\t|"); + System.out.println(">>> ---------------------------------"); - System.out.println(">>> ---------------------------------"); - System.out.println(">>> | Prediction\t| Ground Truth\t|"); - System.out.println(">>> ---------------------------------"); + // Calculate score. + for (int x = 0; x < 10; x++) { + double predicted = mdl.apply(VectorUtils.of(x)); - // Calculate score. - for (int x = 0; x < 10; x++) { - double predicted = mdl.apply(VectorUtils.of(x)); + System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x)); + } - System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x)); - } + System.out.println(">>> ---------------------------------"); - System.out.println(">>> ---------------------------------"); - - System.out.println(">>> Decision tree regression trainer example completed."); - }); - - igniteThread.start(); - - igniteThread.join(); + System.out.println(">>> Decision tree regression trainer example completed."); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesClassificationTrainerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesClassificationTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesClassificationTrainerExample.java index e092e5c..5beb954 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesClassificationTrainerExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesClassificationTrainerExample.java @@ -27,7 +27,6 @@ import org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueCo import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.trainers.DatasetTrainer; import org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer; -import org.apache.ignite.thread.IgniteThread; import org.jetbrains.annotations.NotNull; /** @@ -50,45 +49,38 @@ public class GDBOnTreesClassificationTrainerExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - GDBBinaryClassifierOnTreesTrainer.class.getSimpleName(), () -> { + // Create cache with training data. + CacheConfiguration trainingSetCfg = createCacheConfiguration(); + IgniteCache trainingSet = fillTrainingData(ignite, trainingSetCfg); - // Create cache with training data. - CacheConfiguration trainingSetCfg = createCacheConfiguration(); - IgniteCache trainingSet = fillTrainingData(ignite, trainingSetCfg); + // Create regression trainer. + DatasetTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.) + .withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1)); - // Create regression trainer. - DatasetTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.) - .withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1)); + // Train decision tree model. + ModelsComposition mdl = trainer.fit( + ignite, + trainingSet, + (k, v) -> VectorUtils.of(v[0]), + (k, v) -> v[1] + ); - // Train decision tree model. - ModelsComposition mdl = trainer.fit( - ignite, - trainingSet, - (k, v) -> VectorUtils.of(v[0]), - (k, v) -> v[1] - ); + System.out.println(">>> ---------------------------------"); + System.out.println(">>> | Prediction\t| Valid answer\t|"); + System.out.println(">>> ---------------------------------"); - System.out.println(">>> ---------------------------------"); - System.out.println(">>> | Prediction\t| Valid answer\t|"); - System.out.println(">>> ---------------------------------"); + // Calculate score. + for (int x = -5; x < 5; x++) { + double predicted = mdl.apply(VectorUtils.of(x)); - // Calculate score. - for (int x = -5; x < 5; x++) { - double predicted = mdl.apply(VectorUtils.of(x)); + System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x) < 0 ? 0.0 : 1.0); + } - System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x) < 0 ? 0.0 : 1.0); - } + System.out.println(">>> ---------------------------------"); + System.out.println(">>> Count of trees = " + mdl.getModels().size()); + System.out.println(">>> ---------------------------------"); - System.out.println(">>> ---------------------------------"); - System.out.println(">>> Count of trees = " + mdl.getModels().size()); - System.out.println(">>> ---------------------------------"); - - System.out.println(">>> GDB classification trainer example completed."); - }); - - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> GDB classification trainer example completed."); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesRegressionTrainerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesRegressionTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesRegressionTrainerExample.java index 3662973..482a47c 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesRegressionTrainerExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/boosting/GDBOnTreesRegressionTrainerExample.java @@ -29,7 +29,6 @@ import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.trainers.DatasetTrainer; import org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer; -import org.apache.ignite.thread.IgniteThread; import org.jetbrains.annotations.NotNull; /** @@ -52,43 +51,35 @@ public class GDBOnTreesRegressionTrainerExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - GDBOnTreesRegressionTrainerExample.class.getSimpleName(), () -> { + // Create cache with training data. + CacheConfiguration trainingSetCfg = createCacheConfiguration(); + IgniteCache trainingSet = fillTrainingData(ignite, trainingSetCfg); - // Create cache with training data. - CacheConfiguration trainingSetCfg = createCacheConfiguration(); - IgniteCache trainingSet = fillTrainingData(ignite, trainingSetCfg); + // Create regression trainer. + DatasetTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.) + .withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001)); - // Create regression trainer. - DatasetTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.) - .withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001)); + // Train decision tree model. + Model mdl = trainer.fit( + ignite, + trainingSet, + (k, v) -> VectorUtils.of(v[0]), + (k, v) -> v[1] + ); - // Train decision tree model. - Model mdl = trainer.fit( - ignite, - trainingSet, - (k, v) -> VectorUtils.of(v[0]), - (k, v) -> v[1] - ); + System.out.println(">>> ---------------------------------"); + System.out.println(">>> | Prediction\t| Valid answer \t|"); + System.out.println(">>> ---------------------------------"); - System.out.println(">>> ---------------------------------"); - System.out.println(">>> | Prediction\t| Valid answer \t|"); - System.out.println(">>> ---------------------------------"); + // Calculate score. + for (int x = -5; x < 5; x++) { + double predicted = mdl.apply(VectorUtils.of(x)); - // Calculate score. - for (int x = -5; x < 5; x++) { - double predicted = mdl.apply(VectorUtils.of(x)); + System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.pow(x, 2)); + } - System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.pow(x, 2)); - } - - System.out.println(">>> ---------------------------------"); - - System.out.println(">>> GDB regression trainer example completed."); - }); - - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> ---------------------------------"); + System.out.println(">>> GDB regression trainer example completed."); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestClassificationExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestClassificationExample.java index 4693744..ea235ee 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestClassificationExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestClassificationExample.java @@ -33,7 +33,6 @@ import org.apache.ignite.ml.dataset.feature.FeatureMeta; import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.tree.randomforest.RandomForestClassifierTrainer; import org.apache.ignite.ml.tree.randomforest.data.FeaturesCountSelectionStrategies; -import org.apache.ignite.thread.IgniteThread; /** * Example represents a solution for the task of wine classification based on a @@ -62,57 +61,50 @@ public class RandomForestClassificationExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - RandomForestClassificationExample.class.getSimpleName(), () -> { - IgniteCache dataCache = new TestCache(ignite).fillCacheWith(data); + IgniteCache dataCache = new TestCache(ignite).fillCacheWith(data); - AtomicInteger idx = new AtomicInteger(0); - RandomForestClassifierTrainer classifier = new RandomForestClassifierTrainer( - IntStream.range(0, data[0].length - 1).mapToObj( - x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList()) - ).withAmountOfTrees(101) - .withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD) - .withMaxDepth(4) - .withMinImpurityDelta(0.) - .withSubSampleSize(0.3) - .withSeed(0); + AtomicInteger idx = new AtomicInteger(0); + RandomForestClassifierTrainer classifier = new RandomForestClassifierTrainer( + IntStream.range(0, data[0].length - 1).mapToObj( + x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList()) + ).withAmountOfTrees(101) + .withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD) + .withMaxDepth(4) + .withMinImpurityDelta(0.) + .withSubSampleSize(0.3) + .withSeed(0); - System.out.println(">>> Configured trainer: " + classifier.getClass().getSimpleName()); + System.out.println(">>> Configured trainer: " + classifier.getClass().getSimpleName()); - ModelsComposition randomForest = classifier.fit(ignite, dataCache, - (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 1, v.length)), - (k, v) -> v[0] - ); + ModelsComposition randomForest = classifier.fit(ignite, dataCache, + (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 1, v.length)), + (k, v) -> v[0] + ); - System.out.println(">>> Trained model: " + randomForest.toString(true)); + System.out.println(">>> Trained model: " + randomForest.toString(true)); - int amountOfErrors = 0; - int totalAmount = 0; + int amountOfErrors = 0; + int totalAmount = 0; - try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { - for (Cache.Entry observation : observations) { - double[] val = observation.getValue(); - double[] inputs = Arrays.copyOfRange(val, 1, val.length); - double groundTruth = val[0]; + try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { + for (Cache.Entry observation : observations) { + double[] val = observation.getValue(); + double[] inputs = Arrays.copyOfRange(val, 1, val.length); + double groundTruth = val[0]; - double prediction = randomForest.apply(VectorUtils.of(inputs)); + double prediction = randomForest.apply(VectorUtils.of(inputs)); - totalAmount++; - if (groundTruth != prediction) - amountOfErrors++; - - } - - System.out.println("\n>>> Evaluated model on " + totalAmount + " data points."); - - System.out.println("\n>>> Absolute amount of errors " + amountOfErrors); - System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount)); - System.out.println(">>> Random Forest multi-class classification algorithm over cached dataset usage example completed."); + totalAmount++; + if (groundTruth != prediction) + amountOfErrors++; } - }); - igniteThread.start(); - igniteThread.join(); + System.out.println("\n>>> Evaluated model on " + totalAmount + " data points."); + + System.out.println("\n>>> Absolute amount of errors " + amountOfErrors); + System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount)); + System.out.println(">>> Random Forest multi-class classification algorithm over cached dataset usage example completed."); + } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestRegressionExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestRegressionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestRegressionExample.java index ee0c1c2..9b4aece 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestRegressionExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/randomforest/RandomForestRegressionExample.java @@ -37,7 +37,6 @@ import org.apache.ignite.ml.environment.parallelism.ParallelismStrategy; import org.apache.ignite.ml.math.primitives.vector.VectorUtils; import org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer; import org.apache.ignite.ml.tree.randomforest.data.FeaturesCountSelectionStrategies; -import org.apache.ignite.thread.IgniteThread; /** * Example represents a solution for the task of price predictions for houses in Boston based on a @@ -66,68 +65,62 @@ public class RandomForestRegressionExample { try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { System.out.println(">>> Ignite grid started."); - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - RandomForestRegressionExample.class.getSimpleName(), () -> { - IgniteCache dataCache = new TestCache(ignite).fillCacheWith(data); + IgniteCache dataCache = new TestCache(ignite).fillCacheWith(data); - AtomicInteger idx = new AtomicInteger(0); - RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer( - IntStream.range(0, data[0].length - 1).mapToObj( - x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList()) - ).withAmountOfTrees(101) - .withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD) - .withMaxDepth(4) - .withMinImpurityDelta(0.) - .withSubSampleSize(0.3) - .withSeed(0); + AtomicInteger idx = new AtomicInteger(0); + RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer( + IntStream.range(0, data[0].length - 1).mapToObj( + x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList()) + ).withAmountOfTrees(101) + .withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD) + .withMaxDepth(4) + .withMinImpurityDelta(0.) + .withSubSampleSize(0.3) + .withSeed(0); - trainer.setEnvironment(LearningEnvironment.builder() - .withParallelismStrategy(ParallelismStrategy.Type.ON_DEFAULT_POOL) - .withLoggingFactory(ConsoleLogger.factory(MLLogger.VerboseLevel.LOW)) - .build() - ); + trainer.setEnvironment(LearningEnvironment.builder() + .withParallelismStrategy(ParallelismStrategy.Type.ON_DEFAULT_POOL) + .withLoggingFactory(ConsoleLogger.factory(MLLogger.VerboseLevel.LOW)) + .build() + ); - System.out.println(">>> Configured trainer: " + trainer.getClass().getSimpleName()); + System.out.println(">>> Configured trainer: " + trainer.getClass().getSimpleName()); - ModelsComposition randomForest = trainer.fit(ignite, dataCache, - (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 0, v.length - 1)), - (k, v) -> v[v.length - 1] - ); + ModelsComposition randomForest = trainer.fit(ignite, dataCache, + (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 0, v.length - 1)), + (k, v) -> v[v.length - 1] + ); - System.out.println(">>> Trained model: " + randomForest.toString(true)); + System.out.println(">>> Trained model: " + randomForest.toString(true)); - double mse = 0.0; - double mae = 0.0; - int totalAmount = 0; + double mse = 0.0; + double mae = 0.0; + int totalAmount = 0; - try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { - for (Cache.Entry observation : observations) { - double[] val = observation.getValue(); - double[] inputs = Arrays.copyOfRange(val, 0, val.length - 1); - double groundTruth = val[val.length - 1]; + try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { + for (Cache.Entry observation : observations) { + double[] val = observation.getValue(); + double[] inputs = Arrays.copyOfRange(val, 0, val.length - 1); + double groundTruth = val[val.length - 1]; - double prediction = randomForest.apply(VectorUtils.of(inputs)); + double prediction = randomForest.apply(VectorUtils.of(inputs)); - mse += Math.pow(prediction - groundTruth, 2.0); - mae += Math.abs(prediction - groundTruth); + mse += Math.pow(prediction - groundTruth, 2.0); + mae += Math.abs(prediction - groundTruth); - totalAmount++; - } - - System.out.println("\n>>> Evaluated model on " + totalAmount + " data points."); + totalAmount++; + } - mse = mse / totalAmount; - System.out.println("\n>>> Mean squared error (MSE) " + mse); + System.out.println("\n>>> Evaluated model on " + totalAmount + " data points."); - mae = mae / totalAmount; - System.out.println("\n>>> Mean absolute error (MAE) " + mae); + mse = mse / totalAmount; + System.out.println("\n>>> Mean squared error (MSE) " + mse); - System.out.println(">>> Random Forest regression algorithm over cached dataset usage example completed."); - } - }); + mae = mae / totalAmount; + System.out.println("\n>>> Mean absolute error (MAE) " + mae); - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> Random Forest regression algorithm over cached dataset usage example completed."); + } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_1_Read_and_Learn.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_1_Read_and_Learn.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_1_Read_and_Learn.java index 78ec9f5..264dbf4 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_1_Read_and_Learn.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_1_Read_and_Learn.java @@ -28,7 +28,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Usage of {@link DecisionTreeClassificationTrainer} to predict death in the disaster. @@ -48,47 +47,41 @@ public class Step_1_Read_and_Learn { System.out.println(">>> Tutorial step 1 (read and learn) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_1_Read_and_Learn.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - IgniteBiFunction featureExtractor - = (k, v) -> VectorUtils.of((double) v[0], (double) v[5], (double) v[6]); + IgniteBiFunction featureExtractor + = (k, v) -> VectorUtils.of((double) v[0], (double) v[5], (double) v[6]); - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - DecisionTreeNode mdl = trainer.fit( - ignite, - dataCache, - featureExtractor, // "pclass", "sibsp", "parch" - lbExtractor - ); + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + featureExtractor, // "pclass", "sibsp", "parch" + lbExtractor + ); - System.out.println("\n>>> Trained model: " + mdl); + System.out.println("\n>>> Trained model: " + mdl); - double accuracy = Evaluator.evaluate( - dataCache, - mdl, - featureExtractor, - lbExtractor, - new Accuracy<>() - ); + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + featureExtractor, + lbExtractor, + new Accuracy<>() + ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); - System.out.println(">>> Tutorial step 1 (read and learn) example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); - - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> Tutorial step 1 (read and learn) example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_2_Imputing.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_2_Imputing.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_2_Imputing.java index f86e1b6..df73235 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_2_Imputing.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_2_Imputing.java @@ -29,7 +29,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Usage of {@link ImputerTrainer} to fill missed data ({@code Double.NaN}) values in the chosen columns. @@ -50,54 +49,48 @@ public class Step_2_Imputing { System.out.println(">>> Tutorial step 2 (imputing) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_2_Imputing.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - IgniteBiFunction featureExtractor - = (k, v) -> VectorUtils.of((double) v[0], (double) v[5], (double) v[6]); + IgniteBiFunction featureExtractor + = (k, v) -> VectorUtils.of((double) v[0], (double) v[5], (double) v[6]); - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - IgniteBiFunction imputingPreprocessor = new ImputerTrainer() - .fit(ignite, - dataCache, - featureExtractor // "pclass", "sibsp", "parch" - ); - - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, + IgniteBiFunction imputingPreprocessor = new ImputerTrainer() + .fit(ignite, dataCache, - imputingPreprocessor, - lbExtractor + featureExtractor // "pclass", "sibsp", "parch" ); - System.out.println("\n>>> Trained model: " + mdl); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - double accuracy = Evaluator.evaluate( - dataCache, - mdl, - imputingPreprocessor, - lbExtractor, - new Accuracy<>() - ); + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + imputingPreprocessor, + lbExtractor + ); + + System.out.println("\n>>> Trained model: " + mdl); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + imputingPreprocessor, + lbExtractor, + new Accuracy<>() + ); - System.out.println(">>> Tutorial step 2 (imputing) example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> Tutorial step 2 (imputing) example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial.java index 03ff527..463a6ba 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial.java @@ -30,7 +30,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Let's add two categorial features "sex", "embarked" to predict more precisely than in {@link Step_1_Read_and_Learn}. @@ -53,65 +52,58 @@ public class Step_3_Categorial { System.out.println(">>> Tutorial step 3 (categorial) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_3_Categorial.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - // Defines first preprocessor that extracts features from an upstream data. - IgniteBiFunction featureExtractor - = (k, v) -> new Object[]{v[0], v[3], v[5], v[6], v[10]}; // "pclass", "sibsp", "parch", "sex", "embarked" + // Defines first preprocessor that extracts features from an upstream data. + IgniteBiFunction featureExtractor + = (k, v) -> new Object[]{v[0], v[3], v[5], v[6], v[10]}; // "pclass", "sibsp", "parch", "sex", "embarked" - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() - .withEncoderType(EncoderType.STRING_ENCODER) - .withEncodedFeature(1) - .withEncodedFeature(4) - .fit(ignite, - dataCache, - featureExtractor - ); - - IgniteBiFunction imputingPreprocessor = new ImputerTrainer() - .fit(ignite, - dataCache, - strEncoderPreprocessor - ); - - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, + IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() + .withEncoderType(EncoderType.STRING_ENCODER) + .withEncodedFeature(1) + .withEncodedFeature(4) + .fit(ignite, dataCache, - imputingPreprocessor, - lbExtractor - ); + featureExtractor + ); - System.out.println("\n>>> Trained model: " + mdl); - - double accuracy = Evaluator.evaluate( + IgniteBiFunction imputingPreprocessor = new ImputerTrainer() + .fit(ignite, dataCache, - mdl, - imputingPreprocessor, - lbExtractor, - new Accuracy<>() + strEncoderPreprocessor ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); - - System.out.println(">>> Tutorial step 3 (categorial) example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); - - igniteThread.start(); - - igniteThread.join(); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); + + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + imputingPreprocessor, + lbExtractor + ); + + System.out.println("\n>>> Trained model: " + mdl); + + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + imputingPreprocessor, + lbExtractor, + new Accuracy<>() + ); + + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); + + System.out.println(">>> Tutorial step 3 (categorial) example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial_with_One_Hot_Encoder.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial_with_One_Hot_Encoder.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial_with_One_Hot_Encoder.java index a4535ba..93e7e79 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial_with_One_Hot_Encoder.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_3_Categorial_with_One_Hot_Encoder.java @@ -30,7 +30,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Let's add two categorial features "sex", "embarked" to predict more precisely than in {@link Step_1_Read_and_Learn}.. @@ -54,68 +53,61 @@ public class Step_3_Categorial_with_One_Hot_Encoder { System.out.println(">>> Tutorial step 3 (categorial with One-hot encoder) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_3_Categorial_with_One_Hot_Encoder.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - - // Defines first preprocessor that extracts features from an upstream data. - IgniteBiFunction featureExtractor - = (k, v) -> new Object[]{v[0], v[3], v[5], v[6], v[10] - }; // "pclass", "sibsp", "parch", "sex", "embarked" - - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - - IgniteBiFunction oneHotEncoderPreprocessor = new EncoderTrainer() - .withEncoderType(EncoderType.ONE_HOT_ENCODER) - .withEncodedFeature(0) - .withEncodedFeature(1) - .withEncodedFeature(4) - .fit(ignite, - dataCache, - featureExtractor - ); - - IgniteBiFunction imputingPreprocessor = new ImputerTrainer() - .fit(ignite, - dataCache, - oneHotEncoderPreprocessor - ); - - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + + // Defines first preprocessor that extracts features from an upstream data. + IgniteBiFunction featureExtractor + = (k, v) -> new Object[]{v[0], v[3], v[5], v[6], v[10] + }; // "pclass", "sibsp", "parch", "sex", "embarked" + + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + + IgniteBiFunction oneHotEncoderPreprocessor = new EncoderTrainer() + .withEncoderType(EncoderType.ONE_HOT_ENCODER) + .withEncodedFeature(0) + .withEncodedFeature(1) + .withEncodedFeature(4) + .fit(ignite, + dataCache, + featureExtractor + ); - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, + IgniteBiFunction imputingPreprocessor = new ImputerTrainer() + .fit(ignite, dataCache, - imputingPreprocessor, - lbExtractor + oneHotEncoderPreprocessor ); - System.out.println("\n>>> Trained model: " + mdl); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - double accuracy = Evaluator.evaluate( - dataCache, - mdl, - imputingPreprocessor, - lbExtractor, - new Accuracy<>() - ); + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + imputingPreprocessor, + lbExtractor + ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); + System.out.println("\n>>> Trained model: " + mdl); - System.out.println(">>> Tutorial step 3 (categorial with One-hot encoder) example started."); + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + imputingPreprocessor, + lbExtractor, + new Accuracy<>() + ); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); - igniteThread.start(); + System.out.println(">>> Tutorial step 3 (categorial with One-hot encoder) example started."); - igniteThread.join(); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_4_Add_age_fare.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_4_Add_age_fare.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_4_Add_age_fare.java index 789d7e8..bbeedb6 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_4_Add_age_fare.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_4_Add_age_fare.java @@ -30,7 +30,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * Add yet two numerical features "age", "fare" to improve our model over {@link Step_3_Categorial}. @@ -51,66 +50,59 @@ public class Step_4_Add_age_fare { System.out.println(">>> Tutorial step 4 (add age and fare) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_4_Add_age_fare.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - // Defines first preprocessor that extracts features from an upstream data. - // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". - IgniteBiFunction featureExtractor - = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; + // Defines first preprocessor that extracts features from an upstream data. + // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". + IgniteBiFunction featureExtractor + = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() - .withEncoderType(EncoderType.STRING_ENCODER) - .withEncodedFeature(1) - .withEncodedFeature(6) // <--- Changed index here. - .fit(ignite, - dataCache, - featureExtractor - ); - - IgniteBiFunction imputingPreprocessor = new ImputerTrainer() - .fit(ignite, - dataCache, - strEncoderPreprocessor - ); - - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( - ignite, + IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() + .withEncoderType(EncoderType.STRING_ENCODER) + .withEncodedFeature(1) + .withEncodedFeature(6) // <--- Changed index here. + .fit(ignite, dataCache, - imputingPreprocessor, - lbExtractor - ); + featureExtractor + ); - System.out.println("\n>>> Trained model: " + mdl); - - double accuracy = Evaluator.evaluate( + IgniteBiFunction imputingPreprocessor = new ImputerTrainer() + .fit(ignite, dataCache, - mdl, - imputingPreprocessor, - lbExtractor, - new Accuracy<>() + strEncoderPreprocessor ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); - - System.out.println(">>> Tutorial step 4 (add age and fare) example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); - - igniteThread.start(); - - igniteThread.join(); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); + + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + imputingPreprocessor, + lbExtractor + ); + + System.out.println("\n>>> Trained model: " + mdl); + + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + imputingPreprocessor, + lbExtractor, + new Accuracy<>() + ); + + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); + + System.out.println(">>> Tutorial step 4 (add age and fare) example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling.java index e3de585..7d934d7 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling.java @@ -32,7 +32,6 @@ import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; import org.apache.ignite.ml.tree.DecisionTreeNode; -import org.apache.ignite.thread.IgniteThread; /** * {@link MinMaxScalerTrainer} and {@link NormalizationTrainer} are used in this example due to different values @@ -54,80 +53,74 @@ public class Step_5_Scaling { System.out.println(">>> Tutorial step 5 (scaling) example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_5_Scaling.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - - // Defines first preprocessor that extracts features from an upstream data. - // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". - IgniteBiFunction featureExtractor - = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; - - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - - IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() - .withEncoderType(EncoderType.STRING_ENCODER) - .withEncodedFeature(1) - .withEncodedFeature(6) // <--- Changed index here. - .fit(ignite, - dataCache, - featureExtractor + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + + // Defines first preprocessor that extracts features from an upstream data. + // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". + IgniteBiFunction featureExtractor + = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; + + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + + IgniteBiFunction strEncoderPreprocessor = new EncoderTrainer() + .withEncoderType(EncoderType.STRING_ENCODER) + .withEncodedFeature(1) + .withEncodedFeature(6) // <--- Changed index here. + .fit(ignite, + dataCache, + featureExtractor + ); + + IgniteBiFunction imputingPreprocessor = new ImputerTrainer() + .fit(ignite, + dataCache, + strEncoderPreprocessor ); - IgniteBiFunction imputingPreprocessor = new ImputerTrainer() - .fit(ignite, - dataCache, - strEncoderPreprocessor - ); - - IgniteBiFunction minMaxScalerPreprocessor = new MinMaxScalerTrainer() - .fit( - ignite, - dataCache, - imputingPreprocessor - ); - - IgniteBiFunction normalizationPreprocessor = new NormalizationTrainer() - .withP(1) - .fit( - ignite, - dataCache, - minMaxScalerPreprocessor - ); - - DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); - - // Train decision tree model. - DecisionTreeNode mdl = trainer.fit( + IgniteBiFunction minMaxScalerPreprocessor = new MinMaxScalerTrainer() + .fit( ignite, dataCache, - normalizationPreprocessor, - lbExtractor + imputingPreprocessor ); - System.out.println("\n>>> Trained model: " + mdl); - - double accuracy = Evaluator.evaluate( + IgniteBiFunction normalizationPreprocessor = new NormalizationTrainer() + .withP(1) + .fit( + ignite, dataCache, - mdl, - normalizationPreprocessor, - lbExtractor, - new Accuracy<>() + minMaxScalerPreprocessor ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); - - System.out.println(">>> Tutorial step 5 (scaling) example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); - - igniteThread.start(); - igniteThread.join(); + DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0); + + // Train decision tree model. + DecisionTreeNode mdl = trainer.fit( + ignite, + dataCache, + normalizationPreprocessor, + lbExtractor + ); + + System.out.println("\n>>> Trained model: " + mdl); + + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + normalizationPreprocessor, + lbExtractor, + new Accuracy<>() + ); + + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); + + System.out.println(">>> Tutorial step 5 (scaling) example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/609266fe/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling_with_Pipeline.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling_with_Pipeline.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling_with_Pipeline.java index 1d5900f..cc0a278 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling_with_Pipeline.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_5_Scaling_with_Pipeline.java @@ -32,7 +32,6 @@ import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer; import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator; import org.apache.ignite.ml.selection.scoring.metric.Accuracy; import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer; -import org.apache.ignite.thread.IgniteThread; /** * {@link MinMaxScalerTrainer} and {@link NormalizationTrainer} are used in this example due to different values @@ -54,54 +53,48 @@ public class Step_5_Scaling_with_Pipeline { System.out.println(">>> Tutorial step 5 (scaling) via Pipeline example started."); try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - Step_5_Scaling_with_Pipeline.class.getSimpleName(), () -> { - try { - IgniteCache dataCache = TitanicUtils.readPassengers(ignite); + try { + IgniteCache dataCache = TitanicUtils.readPassengers(ignite); - // Defines first preprocessor that extracts features from an upstream data. - // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". - IgniteBiFunction featureExtractor - = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; + // Defines first preprocessor that extracts features from an upstream data. + // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare". + IgniteBiFunction featureExtractor + = (k, v) -> new Object[]{v[0], v[3], v[4], v[5], v[6], v[8], v[10]}; - IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; + IgniteBiFunction lbExtractor = (k, v) -> (double) v[1]; - PipelineMdl mdl = new Pipeline() - .addFeatureExtractor(featureExtractor) - .addLabelExtractor(lbExtractor) - .addPreprocessor(new EncoderTrainer() - .withEncoderType(EncoderType.STRING_ENCODER) - .withEncodedFeature(1) - .withEncodedFeature(6)) - .addPreprocessor(new ImputerTrainer()) - .addPreprocessor(new MinMaxScalerTrainer()) - .addPreprocessor(new NormalizationTrainer() - .withP(1)) - .addTrainer(new DecisionTreeClassificationTrainer(5, 0)) - .fit(ignite, dataCache); + PipelineMdl mdl = new Pipeline() + .addFeatureExtractor(featureExtractor) + .addLabelExtractor(lbExtractor) + .addPreprocessor(new EncoderTrainer() + .withEncoderType(EncoderType.STRING_ENCODER) + .withEncodedFeature(1) + .withEncodedFeature(6)) + .addPreprocessor(new ImputerTrainer()) + .addPreprocessor(new MinMaxScalerTrainer()) + .addPreprocessor(new NormalizationTrainer() + .withP(1)) + .addTrainer(new DecisionTreeClassificationTrainer(5, 0)) + .fit(ignite, dataCache); - System.out.println("\n>>> Trained model: " + mdl); + System.out.println("\n>>> Trained model: " + mdl); - double accuracy = Evaluator.evaluate( - dataCache, - mdl, - mdl.getFeatureExtractor(), - mdl.getLabelExtractor(), - new Accuracy<>() - ); + double accuracy = Evaluator.evaluate( + dataCache, + mdl, + mdl.getFeatureExtractor(), + mdl.getLabelExtractor(), + new Accuracy<>() + ); - System.out.println("\n>>> Accuracy " + accuracy); - System.out.println("\n>>> Test Error " + (1 - accuracy)); + System.out.println("\n>>> Accuracy " + accuracy); + System.out.println("\n>>> Test Error " + (1 - accuracy)); - System.out.println(">>> Tutorial step 5 (scaling) via Pipeline example completed."); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - }); - - igniteThread.start(); - igniteThread.join(); + System.out.println(">>> Tutorial step 5 (scaling) via Pipeline example completed."); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } } } }