You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by dp...@apache.org on 2019/07/26 14:07:55 UTC
[ignite] branch master updated: Revert "[ML] Hyper-parameter tuning
via Genetic Algorithm (#6713)"
This is an automated email from the ASF dual-hosted git repository.
dpavlov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new ef55e4d Revert "[ML] Hyper-parameter tuning via Genetic Algorithm (#6713)"
ef55e4d is described below
commit ef55e4df147ca12efeaa48090fb592849deacca2
Author: Dmitriy Pavlov <dp...@apache.org>
AuthorDate: Fri Jul 26 17:07:45 2019 +0300
Revert "[ML] Hyper-parameter tuning via Genetic Algorithm (#6713)"
This reverts commit 63fbcbf8
---
.../ml/TrainingWithBinaryObjectExample.java | 2 -
.../clustering/CustomersClusterizationExample.java | 6 +-
.../ml/clustering/GmmClusterizationExample.java | 5 +-
.../ml/clustering/KMeansClusterizationExample.java | 6 +-
.../dataset/AlgorithmSpecificDatasetExample.java | 35 +-
.../ml/dataset/CacheBasedDatasetExample.java | 3 +-
.../IgniteModelDistributedInferenceExample.java | 11 +-
.../examples/ml/inference/ModelStorageExample.java | 2 -
.../TensorFlowDistributedInferenceExample.java | 2 -
.../inference/TensorFlowLocalInferenceExample.java | 1 -
.../TensorFlowThreadedInferenceExample.java | 1 -
.../examples/ml/knn/ANNClassificationExample.java | 2 -
.../examples/ml/knn/IrisClassificationExample.java | 6 +-
.../examples/ml/knn/KNNClassificationExample.java | 13 +-
.../examples/ml/knn/KNNRegressionExample.java | 6 +-
.../multiclass/OneVsRestClassificationExample.java | 9 +-
.../DiscreteNaiveBayesTrainerExample.java | 10 +-
.../GaussianNaiveBayesTrainerExample.java | 6 +-
.../ignite/examples/ml/nn/MLPTrainerExample.java | 2 -
.../ml/preprocessing/BinarizationExample.java | 2 -
.../examples/ml/preprocessing/ImputingExample.java | 2 -
.../ml/preprocessing/MaxAbsScalerExample.java | 4 +-
.../ml/preprocessing/MinMaxScalerExample.java | 4 +-
.../ml/preprocessing/NormalizationExample.java | 2 -
.../ml/preprocessing/StandardScalerExample.java | 4 +-
.../linear/BostonHousePricesPredictionExample.java | 9 +-
.../linear/LinearRegressionLSQRTrainerExample.java | 14 +-
...gressionLSQRTrainerWithMinMaxScalerExample.java | 9 +-
.../linear/LinearRegressionSGDTrainerExample.java | 6 +-
.../BaggedLogisticRegressionSGDTrainerExample.java | 26 +-
.../LogisticRegressionSGDTrainerExample.java | 9 +-
.../ml/selection/cv/CrossValidationExample.java | 31 +-
.../ml/selection/scoring/EvaluatorExample.java | 6 +-
.../selection/scoring/MultipleMetricsExample.java | 6 +-
.../selection/scoring/RegressionMetricExample.java | 6 +-
.../split/TrainTestDatasetSplitterExample.java | 9 +-
...eeClassificationTrainerSQLInferenceExample.java | 5 +-
...onTreeClassificationTrainerSQLTableExample.java | 5 +-
.../ml/svm/SVMBinaryClassificationExample.java | 6 +-
.../DecisionTreeClassificationTrainerExample.java | 5 +-
.../tree/DecisionTreeRegressionTrainerExample.java | 2 -
.../examples/ml/tree/FraudDetectionExample.java | 9 +-
.../GDBOnTreesClassificationTrainerExample.java | 2 -
.../GDBOnTreesRegressionTrainerExample.java | 2 -
.../RandomForestClassificationExample.java | 18 +-
.../RandomForestRegressionExample.java | 15 +-
.../ml/tutorial/Step_1_Read_and_Learn.java | 5 +-
.../examples/ml/tutorial/Step_2_Imputing.java | 5 +-
.../examples/ml/tutorial/Step_3_Categorial.java | 7 +-
.../Step_3_Categorial_with_One_Hot_Encoder.java | 7 +-
.../examples/ml/tutorial/Step_4_Add_age_fare.java | 7 +-
.../examples/ml/tutorial/Step_5_Scaling.java | 7 +-
.../ml/tutorial/Step_5_Scaling_with_Pipeline.java | 5 +-
.../ignite/examples/ml/tutorial/Step_6_KNN.java | 5 +-
.../ml/tutorial/Step_7_Split_train_test.java | 6 +-
.../ignite/examples/ml/tutorial/Step_8_CV.java | 26 +-
.../ml/tutorial/Step_8_CV_with_Param_Grid.java | 30 +-
.../Step_8_CV_with_Param_Grid_and_metrics.java | 31 +-
...V_with_Param_Grid_and_metrics_and_pipeline.java | 30 +-
.../ml/tutorial/Step_9_Scaling_With_Stacking.java | 21 +-
.../ml/tutorial/TutorialStepByStepExample.java | 12 -
.../hyperparametertuning/Step_10_RandomSearch.java | 198 ---------
.../Step_11_Parallel_BrutForce_Search.java | 202 ---------
.../Step_12_Parallel_Random_Search.java | 206 ---------
.../Step_13_Genetic_Programming_Search.java | 194 ---------
...tep_14_Parallel_Genetic_Programming_Search.java | 200 ---------
.../ignite/examples/ml/util/DatasetHelper.java | 2 -
.../ParametricVectorGeneratorExample.java | 1 -
.../util/generators/StandardGeneratorsExample.java | 1 -
.../generators/VectorGeneratorFamilyExample.java | 1 -
.../VectorGeneratorPrimitivesExample.java | 1 -
.../ml/xgboost/XGBoostModelParserExample.java | 2 -
.../parallelism/DefaultParallelismStrategy.java | 6 -
.../parallelism/NoParallelismStrategy.java | 5 -
.../parallelism/ParallelismStrategy.java | 12 +-
.../normalization/NormalizationTrainer.java | 4 +-
.../logistic/LogisticRegressionSGDTrainer.java | 12 +-
.../ml/selection/cv/AbstractCrossValidation.java | 476 ---------------------
.../ignite/ml/selection/cv/CrossValidation.java | 408 ++++++++++++++++--
.../ml/selection/cv/CrossValidationResult.java | 31 +-
.../ml/selection/cv/DebugCrossValidation.java | 116 -----
.../ml/selection/paramgrid/BruteForceStrategy.java | 28 --
.../paramgrid/EvolutionOptimizationStrategy.java | 142 ------
.../paramgrid/HyperParameterTuningStrategy.java | 28 --
.../ignite/ml/selection/paramgrid/ParamGrid.java | 48 +--
.../selection/paramgrid/ParameterSetGenerator.java | 6 +-
.../ml/selection/paramgrid/RandomStrategy.java | 84 ----
.../scoring/cursor/CacheBasedLabelPairCursor.java | 4 +-
.../scoring/cursor/LocalLabelPairCursor.java | 4 +-
.../ml/selection/scoring/metric/MetricValues.java | 6 +-
.../BinaryClassificationMetrics.java | 4 +-
.../metric/regression/RegressionMetrics.java | 10 +-
.../ml/tree/DecisionTreeClassificationTrainer.java | 18 -
.../apache/ignite/ml/util/genetic/Chromosome.java | 110 -----
.../ignite/ml/util/genetic/CrossoverStrategy.java | 35 --
.../ignite/ml/util/genetic/GeneticAlgorithm.java | 451 -------------------
.../apache/ignite/ml/util/genetic/Population.java | 159 -------
.../ignite/ml/util/genetic/SelectionStrategy.java | 32 --
.../ignite/ml/util/genetic/package-info.java | 23 -
.../org/apache/ignite/ml/common/TrainerTest.java | 7 +-
.../preprocessing/encoding/EncoderTrainerTest.java | 16 +-
.../ml/selection/cv/CrossValidationTest.java | 303 +++----------
.../org/apache/ignite/ml/util/UtilTestSuite.java | 6 +-
.../ml/util/genetic/GeneticAlgorithmTest.java | 100 -----
.../ignite/ml/util/genetic/PopulationTest.java | 126 ------
105 files changed, 705 insertions(+), 3673 deletions(-)
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/TrainingWithBinaryObjectExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/TrainingWithBinaryObjectExample.java
index 9eec9d1..59d96c5 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/TrainingWithBinaryObjectExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/TrainingWithBinaryObjectExample.java
@@ -60,8 +60,6 @@ public class TrainingWithBinaryObjectExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/CustomersClusterizationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/CustomersClusterizationExample.java
index 95e2bb4..2295ea2 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/CustomersClusterizationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/CustomersClusterizationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.clustering;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@@ -52,7 +52,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class CustomersClusterizationExample {
/** Runs example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
@@ -88,8 +88,6 @@ public class CustomersClusterizationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/GmmClusterizationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/GmmClusterizationExample.java
index 4c80c70..1769c36 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/GmmClusterizationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/GmmClusterizationExample.java
@@ -17,7 +17,6 @@
package org.apache.ignite.examples.ml.clustering;
-import java.util.concurrent.atomic.AtomicInteger;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -36,6 +35,8 @@ import org.apache.ignite.ml.util.generators.primitives.scalar.GaussRandomProduce
import org.apache.ignite.ml.util.generators.primitives.scalar.RandomProducer;
import org.apache.ignite.ml.util.generators.primitives.vector.VectorGeneratorsFamily;
+import java.util.concurrent.atomic.AtomicInteger;
+
/**
* Example of using GMM clusterization algorithm. Gaussian Mixture Algorithm (GMM, see {@link GmmModel}, {@link
* GmmTrainer}) can be used for input dataset data distribution representation as mixture of multivariance gaussians.
@@ -109,8 +110,6 @@ public class GmmClusterizationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/KMeansClusterizationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/KMeansClusterizationExample.java
index edbc617..3784aa3 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/clustering/KMeansClusterizationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/clustering/KMeansClusterizationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.clustering;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -49,7 +49,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class KMeansClusterizationExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> KMeans clustering algorithm over cached dataset usage example started.");
// Start ignite grid.
@@ -96,8 +96,6 @@ public class KMeansClusterizationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/dataset/AlgorithmSpecificDatasetExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/dataset/AlgorithmSpecificDatasetExample.java
index cff63ce..09a09a7 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/dataset/AlgorithmSpecificDatasetExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/dataset/AlgorithmSpecificDatasetExample.java
@@ -32,7 +32,6 @@ import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
import org.apache.ignite.ml.dataset.primitive.DatasetWrapper;
import org.apache.ignite.ml.dataset.primitive.builder.data.SimpleLabeledDatasetDataBuilder;
import org.apache.ignite.ml.dataset.primitive.data.SimpleLabeledDatasetData;
-import org.apache.ignite.ml.math.functions.IgniteBiFunction;
import org.apache.ignite.ml.math.functions.IgniteFunction;
import org.apache.ignite.ml.math.primitives.vector.Vector;
import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector;
@@ -78,7 +77,6 @@ public class AlgorithmSpecificDatasetExample {
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(1);
-
IgniteFunction<LabeledVector<Double>, LabeledVector<double[]>> func = lv -> new LabeledVector<>(lv.features(), new double[]{lv.label()});
//NOTE: This class is part of Developer API and all lambdas should be loaded on server manually.
@@ -88,24 +86,24 @@ public class AlgorithmSpecificDatasetExample {
SimpleLabeledDatasetDataBuilder<Integer, Vector, AlgorithmSpecificPartitionContext> builder =
new SimpleLabeledDatasetDataBuilder<>(preprocessor);
- IgniteBiFunction<SimpleLabeledDatasetData, AlgorithmSpecificPartitionContext, SimpleLabeledDatasetData> builderFun = (data, ctx) -> {
- double[] features = data.getFeatures();
- int rows = data.getRows();
-
- // Makes a copy of features to supplement it by columns with values equal to 1.0.
- double[] a = new double[features.length + rows];
- Arrays.fill(a, 1.0);
-
- System.arraycopy(features, 0, a, rows, features.length);
-
- return new SimpleLabeledDatasetData(a, data.getLabels(), rows);
- };
-
try (AlgorithmSpecificDataset dataset = DatasetFactory.create(
ignite,
persons,
(env, upstream, upstreamSize) -> new AlgorithmSpecificPartitionContext(),
- builder.andThen(builderFun)
+ builder.andThen((data, ctx) -> {
+ double[] features = data.getFeatures();
+ int rows = data.getRows();
+
+ // Makes a copy of features to supplement it by columns with values equal to 1.0.
+ double[] a = new double[features.length + rows];
+
+ for (int i = 0; i < rows; i++)
+ a[i] = 1.0;
+
+ System.arraycopy(features, 0, a, rows, features.length);
+
+ return new SimpleLabeledDatasetData(a, data.getLabels(), rows);
+ })
).wrap(AlgorithmSpecificDataset::new)) {
// Trains linear regression model using gradient descent.
double[] linearRegressionMdl = new double[2];
@@ -124,11 +122,10 @@ public class AlgorithmSpecificDatasetExample {
}
System.out.println(">>> Algorithm Specific Dataset example completed.");
- } finally {
+ }
+ finally {
persons.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/dataset/CacheBasedDatasetExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/dataset/CacheBasedDatasetExample.java
index cb8a990..f9cb0cf 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/dataset/CacheBasedDatasetExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/dataset/CacheBasedDatasetExample.java
@@ -67,9 +67,8 @@ public class CacheBasedDatasetExample {
} finally {
persons.destroy();
}
- } finally {
- System.out.flush();
}
+
}
/** */
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/IgniteModelDistributedInferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/IgniteModelDistributedInferenceExample.java
index 19535d0..7126c69 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/inference/IgniteModelDistributedInferenceExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/IgniteModelDistributedInferenceExample.java
@@ -17,10 +17,6 @@
package org.apache.ignite.examples.ml.inference;
-import java.io.IOException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -41,6 +37,11 @@ import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
import org.apache.ignite.ml.util.MLSandboxDatasets;
import org.apache.ignite.ml.util.SandboxMLCache;
+import javax.cache.Cache;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
/**
* This example is based on {@link LinearRegressionLSQRTrainerExample}, but to perform inference it uses an approach
* implemented in {@link org.apache.ignite.ml.inference} package.
@@ -100,8 +101,6 @@ public class IgniteModelDistributedInferenceExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/ModelStorageExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/ModelStorageExample.java
index 4787360..3f1d923 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/inference/ModelStorageExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/ModelStorageExample.java
@@ -82,8 +82,6 @@ public class ModelStorageExample {
System.out.println(i + " -> " + res);
}
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowDistributedInferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowDistributedInferenceExample.java
index 82b4f91..a81da7f 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowDistributedInferenceExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowDistributedInferenceExample.java
@@ -94,8 +94,6 @@ public class TensorFlowDistributedInferenceExample {
long t1 = System.currentTimeMillis();
System.out.println("Threaded model throughput: " + images.size() / ((t1 - t0) / 1000.0) + " req/sec");
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowLocalInferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowLocalInferenceExample.java
index 75b885f..baa1f00 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowLocalInferenceExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowLocalInferenceExample.java
@@ -81,6 +81,5 @@ public class TensorFlowLocalInferenceExample {
long t1 = System.currentTimeMillis();
System.out.println("Threaded model throughput: " + 1.0 * images.size() / ((t1 - t0) / 1000) + " req/sec");
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowThreadedInferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowThreadedInferenceExample.java
index b4fe6d5..900dcd4 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowThreadedInferenceExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/inference/TensorFlowThreadedInferenceExample.java
@@ -91,6 +91,5 @@ public class TensorFlowThreadedInferenceExample {
long t1 = System.currentTimeMillis();
System.out.println("Threaded model throughput: " + 1.0 * images.size() / ((t1 - t0) / 1000) + " req/sec");
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/knn/ANNClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/knn/ANNClassificationExample.java
index 4fe5418..a709a04 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/knn/ANNClassificationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/knn/ANNClassificationExample.java
@@ -124,8 +124,6 @@ public class ANNClassificationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/knn/IrisClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/knn/IrisClassificationExample.java
index 5a7cc75..d1de11e 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/knn/IrisClassificationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/knn/IrisClassificationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.knn;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -47,7 +47,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class IrisClassificationExample {
/** Runs example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
@@ -88,8 +88,6 @@ public class IrisClassificationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNClassificationExample.java
index 07f5abd..7715bb2 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNClassificationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNClassificationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.knn;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -41,14 +41,14 @@ import org.apache.ignite.ml.util.SandboxMLCache;
* After that it trains the model based on the specified data using
* <a href="https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm">kNN</a> algorithm.</p>
* <p>
- * Finally, this example loops over the test set of data points, applies the trained model to predict what cluster does
- * this point belong to, and compares prediction to expected outcome (ground truth).</p>
+ * Finally, this example loops over the test set of data points, applies the trained model to predict what cluster
+ * does this point belong to, and compares prediction to expected outcome (ground truth).</p>
* <p>
* You can change the test data used in this example and re-run it to explore this algorithm further.</p>
*/
public class KNNClassificationExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> kNN multi-class classification algorithm over cached dataset usage example started.");
// Start ignite grid.
@@ -76,11 +76,8 @@ public class KNNClassificationExample {
System.out.println("\n>>> Accuracy " + accuracy);
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java
index 79c36a7..e4ff356 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.knn;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -50,7 +50,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class KNNRegressionExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> kNN regression over cached dataset usage example started.");
// Start ignite grid.
@@ -82,8 +82,6 @@ public class KNNRegressionExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/multiclass/OneVsRestClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/multiclass/OneVsRestClassificationExample.java
index 270ca7a..56cddde 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/multiclass/OneVsRestClassificationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/multiclass/OneVsRestClassificationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.multiclass;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.Arrays;
import javax.cache.Cache;
import org.apache.commons.math3.util.Precision;
@@ -57,7 +57,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class OneVsRestClassificationExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> One-vs-Rest SVM Multi-class classification model over cached dataset usage example started.");
// Start ignite grid.
@@ -159,11 +159,8 @@ public class OneVsRestClassificationExample {
System.out.println(">>> One-vs-Rest SVM model over cache based dataset usage example completed.");
}
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/DiscreteNaiveBayesTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/DiscreteNaiveBayesTrainerExample.java
index e2a3ee1..b49be0e 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/DiscreteNaiveBayesTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/DiscreteNaiveBayesTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.naivebayes;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -47,7 +47,8 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class DiscreteNaiveBayesTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
+ System.out.println();
System.out.println(">>> Discrete naive Bayes classification model over partitioned dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
@@ -79,11 +80,8 @@ public class DiscreteNaiveBayesTrainerExample {
System.out.println(">>> Discrete Naive bayes model over partitioned dataset usage example completed.");
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/GaussianNaiveBayesTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/GaussianNaiveBayesTrainerExample.java
index e117bb0..fb095e6 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/GaussianNaiveBayesTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/naivebayes/GaussianNaiveBayesTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.naivebayes;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -47,7 +47,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class GaussianNaiveBayesTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Naive Bayes classification model over partitioned dataset usage example started.");
// Start ignite grid.
@@ -81,8 +81,6 @@ public class GaussianNaiveBayesTrainerExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPTrainerExample.java
index b777ab7..c137898 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPTrainerExample.java
@@ -130,8 +130,6 @@ public class MLPTrainerExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/BinarizationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/BinarizationExample.java
index 3257ef3..da122d5 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/BinarizationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/BinarizationExample.java
@@ -71,8 +71,6 @@ public class BinarizationExample {
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/ImputingExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/ImputingExample.java
index ee47976..e36e38b 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/ImputingExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/ImputingExample.java
@@ -71,8 +71,6 @@ public class ImputingExample {
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MaxAbsScalerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MaxAbsScalerExample.java
index 9adc36d..8601a8d 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MaxAbsScalerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MaxAbsScalerExample.java
@@ -62,12 +62,10 @@ public class MaxAbsScalerExample {
new DatasetHelper(dataset).describe();
}
- System.out.println(">>> Max abs example completed.");
+ System.out.println(">>> Imputing example completed.");
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MinMaxScalerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MinMaxScalerExample.java
index e725918..a7cc6aa 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MinMaxScalerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/MinMaxScalerExample.java
@@ -71,12 +71,10 @@ public class MinMaxScalerExample {
new DatasetHelper(dataset).describe();
}
- System.out.println(">>> MinMax preprocessing example completed.");
+ System.out.println(">>> Imputing example completed.");
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/NormalizationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/NormalizationExample.java
index 488e93d..c3706c3 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/NormalizationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/NormalizationExample.java
@@ -72,8 +72,6 @@ public class NormalizationExample {
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/StandardScalerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/StandardScalerExample.java
index 280b6de..0581f63 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/StandardScalerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/preprocessing/StandardScalerExample.java
@@ -62,12 +62,10 @@ public class StandardScalerExample {
new DatasetHelper(dataset).describe();
}
- System.out.println(">>> Standard scaler example completed.");
+ System.out.println(">>> Imputing example completed.");
} finally {
data.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/BostonHousePricesPredictionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/BostonHousePricesPredictionExample.java
index c4d13ea..57e65e7 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/BostonHousePricesPredictionExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/BostonHousePricesPredictionExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.linear;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.function.BiFunction;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -48,7 +48,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class BostonHousePricesPredictionExample {
/** Runs example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
@@ -85,11 +85,8 @@ public class BostonHousePricesPredictionExample {
System.out.println(">>> Model: " + toString(mdl));
System.out.println(">>> R^2 score: " + score);
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerExample.java
index 3bc3885..7f1c993 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.linear;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -46,7 +46,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class LinearRegressionLSQRTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Linear regression model over cache based dataset usage example started.");
// Start ignite grid.
@@ -70,15 +70,15 @@ public class LinearRegressionLSQRTrainerExample {
// DatasetTrainer#fit(Ignite, IgniteCache, IgniteBiFunction, IgniteBiFunction) method call
// where there is a separate lambda for extracting label from (key, value) and a separate labmda for
// extracting features.
+ Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>()
+ .labeled(Vectorizer.LabelCoordinate.FIRST);
- LinearRegressionModel mdl = trainer.fit(ignite, dataCache, new DummyVectorizer<Integer>()
- .labeled(Vectorizer.LabelCoordinate.FIRST));
+ LinearRegressionModel mdl = trainer.fit(ignite, dataCache, vectorizer);
double rmse = Evaluator.evaluate(
dataCache,
mdl,
- new DummyVectorizer<Integer>()
- .labeled(Vectorizer.LabelCoordinate.FIRST),
+ vectorizer,
new RegressionMetrics()
);
@@ -88,8 +88,6 @@ public class LinearRegressionLSQRTrainerExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerWithMinMaxScalerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerWithMinMaxScalerExample.java
index ef40fe6..ba109d3 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerWithMinMaxScalerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionLSQRTrainerWithMinMaxScalerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.linear;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -52,7 +52,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class LinearRegressionLSQRTrainerWithMinMaxScalerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Linear regression model with Min Max Scaling preprocessor over cached dataset usage example started.");
// Start ignite grid.
@@ -97,11 +97,8 @@ public class LinearRegressionLSQRTrainerWithMinMaxScalerExample {
System.out.println(">>> ---------------------------------");
System.out.println(">>> Linear regression model with MinMaxScaler preprocessor over cache based dataset usage example completed.");
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionSGDTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionSGDTrainerExample.java
index b7a3f31..dbb287a 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionSGDTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/LinearRegressionSGDTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.linear;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -51,7 +51,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class LinearRegressionSGDTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
// Start ignite grid.
@@ -92,8 +92,6 @@ public class LinearRegressionSGDTrainerExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/bagged/BaggedLogisticRegressionSGDTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/bagged/BaggedLogisticRegressionSGDTrainerExample.java
index 935c54b..651bd4f 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/bagged/BaggedLogisticRegressionSGDTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/bagged/BaggedLogisticRegressionSGDTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.logistic.bagged;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -54,7 +54,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class BaggedLogisticRegressionSGDTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Logistic regression model over partitioned dataset usage example started.");
// Start ignite grid.
@@ -93,15 +93,14 @@ public class BaggedLogisticRegressionSGDTrainerExample {
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>()
.labeled(Vectorizer.LabelCoordinate.FIRST);
- double[] score = new CrossValidation<BaggedModel, Double, Integer, Vector>()
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(baggedTrainer)
- .withMetric(new Accuracy<>())
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(3)
- .isRunningOnPipeline(false)
- .scoreByFolds();
+ double[] score = new CrossValidation<BaggedModel, Double, Integer, Vector>().score(
+ baggedTrainer,
+ new Accuracy<>(),
+ ignite,
+ dataCache,
+ vectorizer,
+ 3
+ );
System.out.println(">>> ---------------------------------");
@@ -109,11 +108,8 @@ public class BaggedLogisticRegressionSGDTrainerExample {
System.out.println(">>> Bagged logistic regression model over partitioned dataset usage example completed.");
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/binary/LogisticRegressionSGDTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/binary/LogisticRegressionSGDTrainerExample.java
index e3441ec..d7ec506 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/binary/LogisticRegressionSGDTrainerExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/logistic/binary/LogisticRegressionSGDTrainerExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.regression.logistic.binary;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -50,7 +50,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class LogisticRegressionSGDTrainerExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Logistic regression model over partitioned dataset usage example started.");
// Start ignite grid.
@@ -91,11 +91,8 @@ public class LogisticRegressionSGDTrainerExample {
System.out.println(">>> Logistic regression model over partitioned dataset usage example completed.");
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/selection/cv/CrossValidationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/selection/cv/CrossValidationExample.java
index 514ed6b..6e068fc 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/selection/cv/CrossValidationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/selection/cv/CrossValidationExample.java
@@ -76,19 +76,17 @@ public class CrossValidationExample {
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0);
LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>();
-
CrossValidation<DecisionTreeNode, Double, Integer, LabeledVector<Double>> scoreCalculator
= new CrossValidation<>();
- double[] accuracyScores = scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(trainingSet)
- .withTrainer(trainer)
- .withMetric(new Accuracy<>())
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(4)
- .isRunningOnPipeline(false)
- .scoreByFolds();
+ double[] accuracyScores = scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ ignite,
+ trainingSet,
+ vectorizer,
+ 4
+ );
System.out.println(">>> Accuracy: " + Arrays.toString(accuracyScores));
@@ -97,9 +95,14 @@ public class CrossValidationExample {
.withPositiveClsLb(1.0)
.withMetric(BinaryClassificationMetricValues::balancedAccuracy);
- double[] balancedAccuracyScores = scoreCalculator
- .withMetric(metrics)
- .scoreByFolds();
+ double[] balancedAccuracyScores = scoreCalculator.score(
+ trainer,
+ metrics,
+ ignite,
+ trainingSet,
+ vectorizer,
+ 4
+ );
System.out.println(">>> Balanced Accuracy: " + Arrays.toString(balancedAccuracyScores));
@@ -107,8 +110,6 @@ public class CrossValidationExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/EvaluatorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/EvaluatorExample.java
index b9638ca..54436325 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/EvaluatorExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/EvaluatorExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.selection.scoring;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -47,7 +47,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class EvaluatorExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> kNN multi-class classification algorithm over cached dataset usage example started.");
// Start ignite grid.
@@ -84,8 +84,6 @@ public class EvaluatorExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/MultipleMetricsExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/MultipleMetricsExample.java
index e213bb0..a410e0d 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/MultipleMetricsExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/MultipleMetricsExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.selection.scoring;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.Map;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -48,7 +48,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class MultipleMetricsExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
@@ -76,8 +76,6 @@ public class MultipleMetricsExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/RegressionMetricExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/RegressionMetricExample.java
index e5e630a..9ace1ef 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/RegressionMetricExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/selection/scoring/RegressionMetricExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.selection.scoring;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -48,7 +48,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class RegressionMetricExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> kNN regression over cached dataset usage example started.");
// Start ignite grid.
@@ -79,8 +79,6 @@ public class RegressionMetricExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/selection/split/TrainTestDatasetSplitterExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/selection/split/TrainTestDatasetSplitterExample.java
index 17f53e3..c28edb9 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/selection/split/TrainTestDatasetSplitterExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/selection/split/TrainTestDatasetSplitterExample.java
@@ -17,8 +17,6 @@
package org.apache.ignite.examples.ml.selection.split;
-import java.io.IOException;
-import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -34,6 +32,9 @@ import org.apache.ignite.ml.selection.split.TrainTestSplit;
import org.apache.ignite.ml.util.MLSandboxDatasets;
import org.apache.ignite.ml.util.SandboxMLCache;
+import javax.cache.Cache;
+import java.io.FileNotFoundException;
+
/**
* Run linear regression model over dataset split on train and test subsets ({@link TrainTestDatasetSplitter}).
* <p>
@@ -50,7 +51,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class TrainTestDatasetSplitterExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Linear regression model over cache based dataset usage example started.");
// Start ignite grid.
@@ -99,8 +100,6 @@ public class TrainTestDatasetSplitterExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLInferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLInferenceExample.java
index 7bf6d1d..926512e 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLInferenceExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLInferenceExample.java
@@ -17,7 +17,6 @@
package org.apache.ignite.examples.ml.sql;
-import java.util.List;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -32,6 +31,8 @@ import org.apache.ignite.ml.sql.SqlDatasetBuilder;
import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
import org.apache.ignite.ml.tree.DecisionTreeNode;
+import java.util.List;
+
/**
* Example of using distributed {@link DecisionTreeClassificationTrainer} on a data stored in SQL table and inference
* made as SQL select query.
@@ -138,8 +139,6 @@ public class DecisionTreeClassificationTrainerSQLInferenceExample {
cache.query(new SqlFieldsQuery("DROP TABLE titanik_test"));
cache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLTableExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLTableExample.java
index 93e02ba..23e4204 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLTableExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/sql/DecisionTreeClassificationTrainerSQLTableExample.java
@@ -17,7 +17,6 @@
package org.apache.ignite.examples.ml.sql;
-import java.util.List;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -32,6 +31,8 @@ import org.apache.ignite.ml.sql.SqlDatasetBuilder;
import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
import org.apache.ignite.ml.tree.DecisionTreeNode;
+import java.util.List;
+
/**
* Example of using distributed {@link DecisionTreeClassificationTrainer} on a data stored in SQL table.
*/
@@ -141,8 +142,6 @@ public class DecisionTreeClassificationTrainerSQLTableExample {
cache.query(new SqlFieldsQuery("DROP TABLE titanik_test"));
cache.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/svm/SVMBinaryClassificationExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/svm/SVMBinaryClassificationExample.java
index 78422d4..9a19b5e 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/svm/SVMBinaryClassificationExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/svm/SVMBinaryClassificationExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.svm;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -46,7 +46,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
*/
public class SVMBinaryClassificationExample {
/** Run example. */
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> SVM Binary classification model over cached dataset usage example started.");
// Start ignite grid.
@@ -78,8 +78,6 @@ public class SVMBinaryClassificationExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
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 a049703..9787719 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
@@ -17,7 +17,6 @@
package org.apache.ignite.examples.ml.tree;
-import java.util.Random;
import org.apache.commons.math3.util.Precision;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -30,6 +29,8 @@ import org.apache.ignite.ml.structures.LabeledVector;
import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
import org.apache.ignite.ml.tree.DecisionTreeNode;
+import java.util.Random;
+
/**
* Example of using distributed {@link DecisionTreeClassificationTrainer}.
* <p>
@@ -103,8 +104,6 @@ public class DecisionTreeClassificationTrainerExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
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 e7a3c6a..568aac3 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
@@ -91,8 +91,6 @@ public class DecisionTreeRegressionTrainerExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tree/FraudDetectionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tree/FraudDetectionExample.java
index 4d38ebf..84d0afd 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tree/FraudDetectionExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tree/FraudDetectionExample.java
@@ -17,7 +17,7 @@
package org.apache.ignite.examples.ml.tree;
-import java.io.IOException;
+import java.io.FileNotFoundException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -48,7 +48,7 @@ import org.apache.ignite.ml.util.SandboxMLCache;
* Description of entropy can be found in: https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers .
*/
public class FraudDetectionExample {
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
@@ -78,11 +78,10 @@ public class FraudDetectionExample {
.withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0)),
vectorizer, split
);
- } finally {
+ }
+ finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
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 3fd0f41..f9d5be4 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
@@ -87,8 +87,6 @@ public class GDBOnTreesClassificationTrainerExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
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 a2b7ed5..edc3eee 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
@@ -86,8 +86,6 @@ public class GDBOnTreesRegressionTrainerExample {
} finally {
trainingSet.destroy();
}
- } finally {
- System.out.flush();
}
}
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 e2488aa..13e6792 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
@@ -17,11 +17,6 @@
package org.apache.ignite.examples.ml.tree.randomforest;
-import java.io.IOException;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-import javax.cache.Cache;
import org.apache.commons.math3.util.Precision;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
@@ -38,6 +33,12 @@ import org.apache.ignite.ml.tree.randomforest.data.FeaturesCountSelectionStrateg
import org.apache.ignite.ml.util.MLSandboxDatasets;
import org.apache.ignite.ml.util.SandboxMLCache;
+import javax.cache.Cache;
+import java.io.FileNotFoundException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
/**
* Example represents a solution for the task of wine classification based on a
* <a href ="https://en.wikipedia.org/wiki/Random_forest">Random Forest</a> implementation for
@@ -58,7 +59,7 @@ public class RandomForestClassificationExample {
/**
* Run example.
*/
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Random Forest multi-class classification algorithm over cached dataset usage example started.");
// Start ignite grid.
@@ -112,11 +113,8 @@ public class RandomForestClassificationExample {
}
} finally {
- if (dataCache != null)
- dataCache.destroy();
+ dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
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 4f27b85..42f818e 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
@@ -17,11 +17,6 @@
package org.apache.ignite.examples.ml.tree.randomforest;
-import java.io.IOException;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
@@ -40,6 +35,12 @@ import org.apache.ignite.ml.tree.randomforest.data.FeaturesCountSelectionStrateg
import org.apache.ignite.ml.util.MLSandboxDatasets;
import org.apache.ignite.ml.util.SandboxMLCache;
+import javax.cache.Cache;
+import java.io.FileNotFoundException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
/**
* Example represents a solution for the task of price predictions for houses in Boston based on a
* <a href ="https://en.wikipedia.org/wiki/Random_forest">Random Forest</a> implementation for regression.
@@ -60,7 +61,7 @@ public class RandomForestRegressionExample {
/**
* Run example.
*/
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Random Forest regression algorithm over cached dataset usage example started.");
// Start ignite grid.
@@ -126,8 +127,6 @@ public class RandomForestRegressionExample {
} finally {
dataCache.destroy();
}
- } finally {
- System.out.flush();
}
}
}
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 cd9cff6..331aeee 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
@@ -73,11 +73,10 @@ public class Step_1_Read_and_Learn {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 1 (read and learn) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 ef5f5d3..a270c00 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
@@ -83,11 +83,10 @@ public class Step_2_Imputing {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 2 (imputing) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 9ac3290..e6fb3cd 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
@@ -67,7 +67,7 @@ public class Step_3_Categorial {
.fit(ignite,
dataCache,
vectorizer
- );
+ );
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
.fit(ignite,
@@ -97,11 +97,10 @@ public class Step_3_Categorial {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 3 (categorial) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 29e45a4..92a7714 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
@@ -69,7 +69,7 @@ public class Step_3_Categorial_with_One_Hot_Encoder {
.fit(ignite,
dataCache,
vectorizer
- );
+ );
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
.fit(ignite,
@@ -100,11 +100,10 @@ public class Step_3_Categorial_with_One_Hot_Encoder {
System.out.println(">>> Tutorial step 3 (categorial with One-hot encoder) example started.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 3975056..9d42a1f 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
@@ -66,7 +66,7 @@ public class Step_4_Add_age_fare {
.fit(ignite,
dataCache,
vectorizer
- );
+ );
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
.fit(ignite,
@@ -96,11 +96,10 @@ public class Step_4_Add_age_fare {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 4 (add age and fare) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 a40acfb..4fbf727 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
@@ -69,7 +69,7 @@ public class Step_5_Scaling {
.fit(ignite,
dataCache,
vectorizer
- );
+ );
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
.fit(ignite,
@@ -114,11 +114,10 @@ public class Step_5_Scaling {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 5 (scaling) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
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 f62a244..802359d 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
@@ -88,11 +88,10 @@ public class Step_5_Scaling_with_Pipeline {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 5 (scaling) via Pipeline example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_6_KNN.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_6_KNN.java
index 6116764..ad33c41 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_6_KNN.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_6_KNN.java
@@ -115,11 +115,10 @@ public class Step_6_KNN {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 6 (kNN) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_7_Split_train_test.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_7_Split_train_test.java
index 1d239c9..69de194 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_7_Split_train_test.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_7_Split_train_test.java
@@ -98,7 +98,6 @@ public class Step_7_Split_train_test {
minMaxScalerPreprocessor
);
-
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
// Train decision tree model.
@@ -123,11 +122,10 @@ public class Step_7_Split_train_test {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 7 (split to train and test) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV.java
index 4a6caf6..aa6f12c 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV.java
@@ -102,7 +102,6 @@ public class Step_8_CV {
imputingPreprocessor
);
-
// Tune hyperparams with K-fold Cross-Validation on the split training set.
int[] pSet = new int[]{1, 2};
int[] maxDeepSet = new int[]{1, 2, 3, 4, 5, 10, 20};
@@ -127,16 +126,15 @@ public class Step_8_CV {
CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
= new CrossValidation<>();
- double[] scores = scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(trainer)
- .withMetric(new Accuracy<>())
- .withFilter(split.getTrainFilter())
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .isRunningOnPipeline(false)
- .scoreByFolds();
+ double[] scores = scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ ignite,
+ dataCache,
+ split.getTrainFilter(),
+ normalizationPreprocessor,
+ 3
+ );
System.out.println("Scores are: " + Arrays.toString(scores));
@@ -162,7 +160,6 @@ public class Step_8_CV {
minMaxScalerPreprocessor
);
-
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(bestMaxDeep, 0);
// Train decision tree model.
@@ -187,11 +184,10 @@ public class Step_8_CV {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 8 (cross-validation) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid.java
index b7bc03d..8b9f365 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid.java
@@ -120,21 +120,19 @@ public class Step_8_CV_with_Param_Grid {
= new CrossValidation<>();
ParamGrid paramGrid = new ParamGrid()
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.25, 0.5});
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(trainerCV)
- .withMetric(new Accuracy<>())
- .withFilter(split.getTrainFilter())
- .isRunningOnPipeline(false)
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
+ .addHyperParam("maxDeep", new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 10.0})
+ .addHyperParam("minImpurityDecrease", new Double[]{0.0, 0.25, 0.5});
+
+ CrossValidationResult crossValidationRes = scoreCalculator.score(
+ trainerCV,
+ new Accuracy<>(),
+ ignite,
+ dataCache,
+ split.getTrainFilter(),
+ normalizationPreprocessor,
+ 3,
+ paramGrid
+ );
System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
+ " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
@@ -177,8 +175,6 @@ public class Step_8_CV_with_Param_Grid {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics.java
index 80d166e..7dcc104 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics.java
@@ -122,26 +122,24 @@ public class Step_8_CV_with_Param_Grid_and_metrics {
= new CrossValidation<>();
ParamGrid paramGrid = new ParamGrid()
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.25, 0.5});
+ .addHyperParam("maxDeep", new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0})
+ .addHyperParam("minImpurityDecrease", new Double[] {0.0, 0.25, 0.5});
BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
.withNegativeClsLb(0.0)
.withPositiveClsLb(1.0)
.withMetric(BinaryClassificationMetricValues::accuracy);
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(trainerCV)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .isRunningOnPipeline(false)
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
+ CrossValidationResult crossValidationRes = scoreCalculator.score(
+ trainerCV,
+ metrics,
+ ignite,
+ dataCache,
+ split.getTrainFilter(),
+ normalizationPreprocessor,
+ 3,
+ paramGrid
+ );
System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
+ " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
@@ -181,11 +179,10 @@ public class Step_8_CV_with_Param_Grid_and_metrics {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics_and_pipeline.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics_and_pipeline.java
index 62c6737..8bdc683 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics_and_pipeline.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_8_CV_with_Param_Grid_and_metrics_and_pipeline.java
@@ -80,13 +80,11 @@ public class Step_8_CV_with_Param_Grid_and_metrics_and_pipeline {
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
.split(0.75);
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
-
Pipeline<Integer, Vector, Integer, Double> pipeline = new Pipeline<Integer, Vector, Integer, Double>()
.addVectorizer(vectorizer)
.addPreprocessingTrainer(new ImputerTrainer<Integer, Vector>())
.addPreprocessingTrainer(new MinMaxScalerTrainer<Integer, Vector>())
- .addTrainer(trainer);
+ .addTrainer(new DecisionTreeClassificationTrainer(5, 0));
// Tune hyperparams with K-fold Cross-Validation on the split training set.
@@ -94,25 +92,23 @@ public class Step_8_CV_with_Param_Grid_and_metrics_and_pipeline {
= new CrossValidation<>();
ParamGrid paramGrid = new ParamGrid()
- .addHyperParam("maxDeep", trainer::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainer::withMinImpurityDecrease, new Double[]{0.0, 0.25, 0.5});
+ .addHyperParam("maxDeep", new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 10.0})
+ .addHyperParam("minImpurityDecrease", new Double[]{0.0, 0.25, 0.5});
BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
.withNegativeClsLb(0.0)
.withPositiveClsLb(1.0)
.withMetric(BinaryClassificationMetricValues::accuracy);
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withPipeline(pipeline)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
+ CrossValidationResult crossValidationRes = scoreCalculator.score(
+ pipeline,
+ metrics,
+ ignite,
+ dataCache,
+ split.getTrainFilter(),
+ 3,
+ paramGrid
+ );
System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
+ " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
@@ -129,8 +125,6 @@ public class Step_8_CV_with_Param_Grid_and_metrics_and_pipeline {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_9_Scaling_With_Stacking.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_9_Scaling_With_Stacking.java
index babef3b..55d553d 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_9_Scaling_With_Stacking.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/Step_9_Scaling_With_Stacking.java
@@ -108,14 +108,14 @@ public class Step_9_Scaling_With_Stacking {
StackedModel<Vector, Vector, Double, LogisticRegressionModel> mdl =
new StackedVectorDatasetTrainer<>(aggregator)
- .addTrainerWithDoubleOutput(trainer)
- .addTrainerWithDoubleOutput(trainer1)
- .addTrainerWithDoubleOutput(trainer2)
- .fit(
- ignite,
- dataCache,
- normalizationPreprocessor
- );
+ .addTrainerWithDoubleOutput(trainer)
+ .addTrainerWithDoubleOutput(trainer1)
+ .addTrainerWithDoubleOutput(trainer2)
+ .fit(
+ ignite,
+ dataCache,
+ normalizationPreprocessor
+ );
System.out.println("\n>>> Trained model: " + mdl);
@@ -130,11 +130,10 @@ public class Step_9_Scaling_With_Stacking {
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 5 (scaling) example completed.");
- } catch (FileNotFoundException e) {
+ }
+ catch (FileNotFoundException e) {
e.printStackTrace();
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/TutorialStepByStepExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/TutorialStepByStepExample.java
index 0c96a0f..24eb634 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/TutorialStepByStepExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/TutorialStepByStepExample.java
@@ -17,12 +17,6 @@
package org.apache.ignite.examples.ml.tutorial;
-import org.apache.ignite.examples.ml.tutorial.hyperparametertuning.Step_10_RandomSearch;
-import org.apache.ignite.examples.ml.tutorial.hyperparametertuning.Step_11_Parallel_BrutForce_Search;
-import org.apache.ignite.examples.ml.tutorial.hyperparametertuning.Step_12_Parallel_Random_Search;
-import org.apache.ignite.examples.ml.tutorial.hyperparametertuning.Step_13_Genetic_Programming_Search;
-import org.apache.ignite.examples.ml.tutorial.hyperparametertuning.Step_14_Parallel_Genetic_Programming_Search;
-
/**
* Run all the tutorial examples step by step with primary purpose to provide
* automatic execution from {@code IgniteExamplesMLTestSuite}.
@@ -40,11 +34,5 @@ public class TutorialStepByStepExample {
Step_7_Split_train_test.main(args);
Step_8_CV.main(args);
Step_8_CV_with_Param_Grid.main(args);
- Step_9_Scaling_With_Stacking.main(args);
- Step_10_RandomSearch.main(args);
- Step_11_Parallel_BrutForce_Search.main(args);
- Step_12_Parallel_Random_Search.main(args);
- Step_13_Genetic_Programming_Search.main(args);
- Step_14_Parallel_Genetic_Programming_Search.main(args);
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_10_RandomSearch.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_10_RandomSearch.java
deleted file mode 100644
index dcafedc..0000000
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_10_RandomSearch.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * 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.tutorial.hyperparametertuning;
-
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.examples.ml.tutorial.TitanicUtils;
-import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
-import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderType;
-import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
-import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer;
-import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
-import org.apache.ignite.ml.selection.cv.CrossValidation;
-import org.apache.ignite.ml.selection.cv.CrossValidationResult;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.paramgrid.RandomStrategy;
-import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
-import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
-import org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter;
-import org.apache.ignite.ml.selection.split.TrainTestSplit;
-import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
-import org.apache.ignite.ml.tree.DecisionTreeNode;
-
-/**
- * To choose the best hyperparameters the cross-validation with {@link ParamGrid} will be used in this example.
- * <p>
- * Code in this example launches Ignite grid and fills the cache with test data (based on Titanic passengers data).</p>
- * <p>
- * After that it defines how to split the data to train and test sets and configures preprocessors that extract
- * features from an upstream data and perform other desired changes over the extracted data.</p>
- * <p>
- * Then, it tunes hyperparams with K-fold Cross-Validation on the split training set and trains the model based on
- * the processed data using decision tree classification and the obtained hyperparams.</p>
- * <p>
- * Finally, this example uses {@link Evaluator} functionality to compute metrics from predictions.</p>
- * <p>
- * The purpose of cross-validation is model checking, not model building.</p>
- * <p>
- * We train {@code k} different models.</p>
- * <p>
- * They differ in that {@code 1/(k-1)}th of the training data is exchanged against other cases.</p>
- * <p>
- * These models are sometimes called surrogate models because the (average) performance measured for these models
- * is taken as a surrogate of the performance of the model trained on all cases.</p>
- * <p>
- * All scenarios are described there: https://sebastianraschka.com/faq/docs/evaluate-a-model.html</p>
- */
-public class Step_10_RandomSearch {
- /** Run example. */
- public static void main(String[] args) {
- try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
- try {
- IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
-
- // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer
- = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
-
- TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
- .split(0.75);
-
- Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>()
- .withEncoderType(EncoderType.STRING_ENCODER)
- .withEncodedFeature(1)
- .withEncodedFeature(6)
- .fit(ignite,
- dataCache,
- vectorizer
- );
-
- Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
- .fit(ignite,
- dataCache,
- strEncoderPreprocessor
- );
-
- Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>()
- .fit(
- ignite,
- dataCache,
- imputingPreprocessor
- );
-
- NormalizationTrainer<Integer, Vector> normalizationTrainer = new NormalizationTrainer<Integer, Vector>()
- .withP(1);
-
- Preprocessor<Integer, Vector> normalizationPreprocessor = normalizationTrainer
- .fit(
- ignite,
- dataCache,
- minMaxScalerPreprocessor
- );
-
- // Tune hyperparams with K-fold Cross-Validation on the split training set.
-
- DecisionTreeClassificationTrainer trainerCV = new DecisionTreeClassificationTrainer();
-
- CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
- = new CrossValidation<>();
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(
- new RandomStrategy()
- .withMaxTries(10)
- .withSeed(12L)
- )
- .addHyperParam("p", normalizationTrainer::withP, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0});
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withNegativeClsLb(0.0)
- .withPositiveClsLb(1.0)
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(trainerCV)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .isRunningOnPipeline(false)
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
- + " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
-
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer()
- .withMaxDeep(crossValidationRes.getBest("maxDeep"))
- .withMinImpurityDecrease(crossValidationRes.getBest("minImpurityDecrease"));
-
- System.out.println(crossValidationRes);
-
- System.out.println("Best score: " + Arrays.toString(crossValidationRes.getBestScore()));
- System.out.println("Best hyper params: " + crossValidationRes.getBestHyperParams());
- System.out.println("Best average score: " + crossValidationRes.getBestAvgScore());
-
- crossValidationRes.getScoringBoard().forEach((hyperParams, score)
- -> System.out.println("Score " + Arrays.toString(score) + " for hyper params " + hyperParams));
-
- // Train decision tree model.
- DecisionTreeNode bestMdl = trainer.fit(
- ignite,
- dataCache,
- split.getTrainFilter(),
- normalizationPreprocessor
- );
-
- System.out.println("\n>>> Trained model: " + bestMdl);
-
- double accuracy = Evaluator.evaluate(
- dataCache,
- split.getTestFilter(),
- bestMdl,
- normalizationPreprocessor,
- new Accuracy<>()
- );
-
- System.out.println("\n>>> Accuracy " + accuracy);
- System.out.println("\n>>> Test Error " + (1 - accuracy));
-
- System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } finally {
- System.out.flush();
- }
- }
-}
\ No newline at end of file
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_11_Parallel_BrutForce_Search.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_11_Parallel_BrutForce_Search.java
deleted file mode 100644
index 3428627..0000000
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_11_Parallel_BrutForce_Search.java
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * 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.tutorial.hyperparametertuning;
-
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.examples.ml.tutorial.TitanicUtils;
-import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
-import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
-import org.apache.ignite.ml.environment.LearningEnvironmentBuilder;
-import org.apache.ignite.ml.environment.logging.ConsoleLogger;
-import org.apache.ignite.ml.environment.parallelism.ParallelismStrategy;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderType;
-import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
-import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer;
-import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
-import org.apache.ignite.ml.selection.cv.CrossValidation;
-import org.apache.ignite.ml.selection.cv.CrossValidationResult;
-import org.apache.ignite.ml.selection.paramgrid.BruteForceStrategy;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
-import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
-import org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter;
-import org.apache.ignite.ml.selection.split.TrainTestSplit;
-import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
-import org.apache.ignite.ml.tree.DecisionTreeNode;
-
-/**
- * To choose the best hyperparameters the cross-validation with {@link ParamGrid} will be used in this example.
- * <p>
- * Code in this example launches Ignite grid and fills the cache with test data (based on Titanic passengers data).</p>
- * <p>
- * After that it defines how to split the data to train and test sets and configures preprocessors that extract
- * features from an upstream data and perform other desired changes over the extracted data.</p>
- * <p>
- * Then, it tunes hyperparams with K-fold Cross-Validation on the split training set and trains the model based on
- * the processed data using decision tree classification and the obtained hyperparams.</p>
- * <p>
- * Finally, this example uses {@link Evaluator} functionality to compute metrics from predictions.</p>
- * <p>
- * The purpose of cross-validation is model checking, not model building.</p>
- * <p>
- * We train {@code k} different models.</p>
- * <p>
- * They differ in that {@code 1/(k-1)}th of the training data is exchanged against other cases.</p>
- * <p>
- * These models are sometimes called surrogate models because the (average) performance measured for these models
- * is taken as a surrogate of the performance of the model trained on all cases.</p>
- * <p>
- * All scenarios are described there: https://sebastianraschka.com/faq/docs/evaluate-a-model.html</p>
- */
-public class Step_11_Parallel_BrutForce_Search {
- /** Run example. */
- public static void main(String[] args) {
- try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
- try {
- IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
-
- // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer
- = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
-
- TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
- .split(0.75);
-
- Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>()
- .withEncoderType(EncoderType.STRING_ENCODER)
- .withEncodedFeature(1)
- .withEncodedFeature(6)
- .fit(ignite,
- dataCache,
- vectorizer
- );
-
- Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
- .fit(ignite,
- dataCache,
- strEncoderPreprocessor
- );
-
- Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>()
- .fit(
- ignite,
- dataCache,
- imputingPreprocessor
- );
-
-
- NormalizationTrainer<Integer, Vector> normalizationTrainer = new NormalizationTrainer<Integer, Vector>()
- .withP(1);
-
- Preprocessor<Integer, Vector> normalizationPreprocessor = normalizationTrainer
- .fit(
- ignite,
- dataCache,
- minMaxScalerPreprocessor
- );
-
- // Tune hyperparams with K-fold Cross-Validation on the split training set.
-
- DecisionTreeClassificationTrainer trainerCV = new DecisionTreeClassificationTrainer();
-
- CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
- = new CrossValidation<>();
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(new BruteForceStrategy())
- .addHyperParam("p", normalizationTrainer::withP, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0});
-
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withNegativeClsLb(0.0)
- .withPositiveClsLb(1.0)
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder()
- .withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL)
- .withLoggingFactoryDependency(ConsoleLogger.Factory.LOW))
- .withTrainer(trainerCV)
- .isRunningOnPipeline(false)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
- + " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
-
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer()
- .withMaxDeep(crossValidationRes.getBest("maxDeep"))
- .withMinImpurityDecrease(crossValidationRes.getBest("minImpurityDecrease"));
-
- System.out.println(crossValidationRes);
-
- System.out.println("Best score: " + Arrays.toString(crossValidationRes.getBestScore()));
- System.out.println("Best hyper params: " + crossValidationRes.getBestHyperParams());
- System.out.println("Best average score: " + crossValidationRes.getBestAvgScore());
-
- crossValidationRes.getScoringBoard().forEach((hyperParams, score)
- -> System.out.println("Score " + Arrays.toString(score) + " for hyper params " + hyperParams));
-
- // Train decision tree model.
- DecisionTreeNode bestMdl = trainer.fit(
- ignite,
- dataCache,
- split.getTrainFilter(),
- normalizationPreprocessor
- );
-
- System.out.println("\n>>> Trained model: " + bestMdl);
-
- double accuracy = Evaluator.evaluate(
- dataCache,
- split.getTestFilter(),
- bestMdl,
- normalizationPreprocessor,
- new Accuracy<>()
- );
-
- System.out.println("\n>>> Accuracy " + accuracy);
- System.out.println("\n>>> Test Error " + (1 - accuracy));
-
- System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } finally {
- System.out.flush();
- }
- }
-}
\ No newline at end of file
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_12_Parallel_Random_Search.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_12_Parallel_Random_Search.java
deleted file mode 100644
index aaa3c15c..0000000
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_12_Parallel_Random_Search.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * 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.tutorial.hyperparametertuning;
-
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.examples.ml.tutorial.TitanicUtils;
-import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
-import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
-import org.apache.ignite.ml.environment.LearningEnvironmentBuilder;
-import org.apache.ignite.ml.environment.logging.ConsoleLogger;
-import org.apache.ignite.ml.environment.parallelism.ParallelismStrategy;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderType;
-import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
-import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer;
-import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
-import org.apache.ignite.ml.selection.cv.CrossValidation;
-import org.apache.ignite.ml.selection.cv.CrossValidationResult;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.paramgrid.RandomStrategy;
-import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
-import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
-import org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter;
-import org.apache.ignite.ml.selection.split.TrainTestSplit;
-import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
-import org.apache.ignite.ml.tree.DecisionTreeNode;
-
-/**
- * To choose the best hyperparameters the cross-validation with {@link ParamGrid} will be used in this example.
- * <p>
- * Code in this example launches Ignite grid and fills the cache with test data (based on Titanic passengers data).</p>
- * <p>
- * After that it defines how to split the data to train and test sets and configures preprocessors that extract
- * features from an upstream data and perform other desired changes over the extracted data.</p>
- * <p>
- * Then, it tunes hyperparams with K-fold Cross-Validation on the split training set and trains the model based on
- * the processed data using decision tree classification and the obtained hyperparams.</p>
- * <p>
- * Finally, this example uses {@link Evaluator} functionality to compute metrics from predictions.</p>
- * <p>
- * The purpose of cross-validation is model checking, not model building.</p>
- * <p>
- * We train {@code k} different models.</p>
- * <p>
- * They differ in that {@code 1/(k-1)}th of the training data is exchanged against other cases.</p>
- * <p>
- * These models are sometimes called surrogate models because the (average) performance measured for these models
- * is taken as a surrogate of the performance of the model trained on all cases.</p>
- * <p>
- * All scenarios are described there: https://sebastianraschka.com/faq/docs/evaluate-a-model.html</p>
- */
-public class Step_12_Parallel_Random_Search {
- /** Run example. */
- public static void main(String[] args) {
- try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
- try {
- IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
-
- // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer
- = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
-
- TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
- .split(0.75);
-
- Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>()
- .withEncoderType(EncoderType.STRING_ENCODER)
- .withEncodedFeature(1)
- .withEncodedFeature(6)
- .fit(ignite,
- dataCache,
- vectorizer
- );
-
- Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
- .fit(ignite,
- dataCache,
- strEncoderPreprocessor
- );
-
- Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>()
- .fit(
- ignite,
- dataCache,
- imputingPreprocessor
- );
-
-
- NormalizationTrainer<Integer, Vector> normalizationTrainer = new NormalizationTrainer<Integer, Vector>()
- .withP(1);
-
- Preprocessor<Integer, Vector> normalizationPreprocessor = normalizationTrainer
- .fit(
- ignite,
- dataCache,
- minMaxScalerPreprocessor
- );
-
- // Tune hyperparams with K-fold Cross-Validation on the split training set.
-
- DecisionTreeClassificationTrainer trainerCV = new DecisionTreeClassificationTrainer();
-
- CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
- = new CrossValidation<>();
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(
- new RandomStrategy()
- .withMaxTries(10)
- .withSeed(12L)
- )
- .addHyperParam("p", normalizationTrainer::withP, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0});
-
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withNegativeClsLb(0.0)
- .withPositiveClsLb(1.0)
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder()
- .withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL)
- .withLoggingFactoryDependency(ConsoleLogger.Factory.LOW))
- .withTrainer(trainerCV)
- .isRunningOnPipeline(false)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
- + " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
-
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer()
- .withMaxDeep(crossValidationRes.getBest("maxDeep"))
- .withMinImpurityDecrease(crossValidationRes.getBest("minImpurityDecrease"));
-
- System.out.println(crossValidationRes);
-
- System.out.println("Best score: " + Arrays.toString(crossValidationRes.getBestScore()));
- System.out.println("Best hyper params: " + crossValidationRes.getBestHyperParams());
- System.out.println("Best average score: " + crossValidationRes.getBestAvgScore());
-
- crossValidationRes.getScoringBoard().forEach((hyperParams, score)
- -> System.out.println("Score " + Arrays.toString(score) + " for hyper params " + hyperParams));
-
- // Train decision tree model.
- DecisionTreeNode bestMdl = trainer.fit(
- ignite,
- dataCache,
- split.getTrainFilter(),
- normalizationPreprocessor
- );
-
- System.out.println("\n>>> Trained model: " + bestMdl);
-
- double accuracy = Evaluator.evaluate(
- dataCache,
- split.getTestFilter(),
- bestMdl,
- normalizationPreprocessor,
- new Accuracy<>()
- );
-
- System.out.println("\n>>> Accuracy " + accuracy);
- System.out.println("\n>>> Test Error " + (1 - accuracy));
-
- System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } finally {
- System.out.flush();
- }
- }
-}
\ No newline at end of file
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_13_Genetic_Programming_Search.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_13_Genetic_Programming_Search.java
deleted file mode 100644
index 2deed1c..0000000
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_13_Genetic_Programming_Search.java
+++ /dev/null
@@ -1,194 +0,0 @@
-/*
- * 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.tutorial.hyperparametertuning;
-
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.examples.ml.tutorial.TitanicUtils;
-import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
-import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderType;
-import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
-import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer;
-import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
-import org.apache.ignite.ml.selection.cv.CrossValidation;
-import org.apache.ignite.ml.selection.cv.CrossValidationResult;
-import org.apache.ignite.ml.selection.paramgrid.EvolutionOptimizationStrategy;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
-import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
-import org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter;
-import org.apache.ignite.ml.selection.split.TrainTestSplit;
-import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
-import org.apache.ignite.ml.tree.DecisionTreeNode;
-
-/**
- * To choose the best hyperparameters the cross-validation with {@link ParamGrid} will be used in this example.
- * <p>
- * Code in this example launches Ignite grid and fills the cache with test data (based on Titanic passengers data).</p>
- * <p>
- * After that it defines how to split the data to train and test sets and configures preprocessors that extract
- * features from an upstream data and perform other desired changes over the extracted data.</p>
- * <p>
- * Then, it tunes hyperparams with K-fold Cross-Validation on the split training set and trains the model based on
- * the processed data using decision tree classification and the obtained hyperparams.</p>
- * <p>
- * Finally, this example uses {@link Evaluator} functionality to compute metrics from predictions.</p>
- * <p>
- * The purpose of cross-validation is model checking, not model building.</p>
- * <p>
- * We train {@code k} different models.</p>
- * <p>
- * They differ in that {@code 1/(k-1)}th of the training data is exchanged against other cases.</p>
- * <p>
- * These models are sometimes called surrogate models because the (average) performance measured for these models
- * is taken as a surrogate of the performance of the model trained on all cases.</p>
- * <p>
- * All scenarios are described there: https://sebastianraschka.com/faq/docs/evaluate-a-model.html</p>
- */
-public class Step_13_Genetic_Programming_Search {
- /** Run example. */
- public static void main(String[] args) {
- try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
- try {
- IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
-
- // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer
- = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
-
- TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
- .split(0.75);
-
- Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>()
- .withEncoderType(EncoderType.STRING_ENCODER)
- .withEncodedFeature(1)
- .withEncodedFeature(6)
- .fit(ignite,
- dataCache,
- vectorizer
- );
-
- Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
- .fit(ignite,
- dataCache,
- strEncoderPreprocessor
- );
-
- Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>()
- .fit(
- ignite,
- dataCache,
- imputingPreprocessor
- );
-
- NormalizationTrainer<Integer, Vector> normalizationTrainer = new NormalizationTrainer<Integer, Vector>()
- .withP(1);
-
- Preprocessor<Integer, Vector> normalizationPreprocessor = normalizationTrainer
- .fit(
- ignite,
- dataCache,
- minMaxScalerPreprocessor
- );
-
- // Tune hyperparams with K-fold Cross-Validation on the split training set.
-
- DecisionTreeClassificationTrainer trainerCV = new DecisionTreeClassificationTrainer();
-
- CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
- = new CrossValidation<>();
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(new EvolutionOptimizationStrategy())
- .addHyperParam("p", normalizationTrainer::withP, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0});
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withNegativeClsLb(0.0)
- .withPositiveClsLb(1.0)
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withTrainer(trainerCV)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .isRunningOnPipeline(false)
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
- + " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
-
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer()
- .withMaxDeep(crossValidationRes.getBest("maxDeep"))
- .withMinImpurityDecrease(crossValidationRes.getBest("minImpurityDecrease"));
-
- System.out.println(crossValidationRes);
-
- System.out.println("Best score: " + Arrays.toString(crossValidationRes.getBestScore()));
- System.out.println("Best hyper params: " + crossValidationRes.getBestHyperParams());
- System.out.println("Best average score: " + crossValidationRes.getBestAvgScore());
-
- crossValidationRes.getScoringBoard().forEach((hyperParams, score)
- -> System.out.println("Score " + Arrays.toString(score) + " for hyper params " + hyperParams));
-
- // Train decision tree model.
- DecisionTreeNode bestMdl = trainer.fit(
- ignite,
- dataCache,
- split.getTrainFilter(),
- normalizationPreprocessor
- );
-
- System.out.println("\n>>> Trained model: " + bestMdl);
-
- double accuracy = Evaluator.evaluate(
- dataCache,
- split.getTestFilter(),
- bestMdl,
- normalizationPreprocessor,
- new Accuracy<>()
- );
-
- System.out.println("\n>>> Accuracy " + accuracy);
- System.out.println("\n>>> Test Error " + (1 - accuracy));
-
- System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } finally {
- System.out.flush();
- }
- }
-}
\ No newline at end of file
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_14_Parallel_Genetic_Programming_Search.java b/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_14_Parallel_Genetic_Programming_Search.java
deleted file mode 100644
index dd43dac..0000000
--- a/examples/src/main/java/org/apache/ignite/examples/ml/tutorial/hyperparametertuning/Step_14_Parallel_Genetic_Programming_Search.java
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * 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.tutorial.hyperparametertuning;
-
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.examples.ml.tutorial.TitanicUtils;
-import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
-import org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer;
-import org.apache.ignite.ml.environment.LearningEnvironmentBuilder;
-import org.apache.ignite.ml.environment.logging.ConsoleLogger;
-import org.apache.ignite.ml.environment.parallelism.ParallelismStrategy;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer;
-import org.apache.ignite.ml.preprocessing.encoding.EncoderType;
-import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
-import org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer;
-import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
-import org.apache.ignite.ml.selection.cv.CrossValidation;
-import org.apache.ignite.ml.selection.cv.CrossValidationResult;
-import org.apache.ignite.ml.selection.paramgrid.EvolutionOptimizationStrategy;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
-import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
-import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
-import org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter;
-import org.apache.ignite.ml.selection.split.TrainTestSplit;
-import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
-import org.apache.ignite.ml.tree.DecisionTreeNode;
-
-/**
- * To choose the best hyperparameters the cross-validation with {@link ParamGrid} will be used in this example.
- * <p>
- * Code in this example launches Ignite grid and fills the cache with test data (based on Titanic passengers data).</p>
- * <p>
- * After that it defines how to split the data to train and test sets and configures preprocessors that extract
- * features from an upstream data and perform other desired changes over the extracted data.</p>
- * <p>
- * Then, it tunes hyperparams with K-fold Cross-Validation on the split training set and trains the model based on
- * the processed data using decision tree classification and the obtained hyperparams.</p>
- * <p>
- * Finally, this example uses {@link Evaluator} functionality to compute metrics from predictions.</p>
- * <p>
- * The purpose of cross-validation is model checking, not model building.</p>
- * <p>
- * We train {@code k} different models.</p>
- * <p>
- * They differ in that {@code 1/(k-1)}th of the training data is exchanged against other cases.</p>
- * <p>
- * These models are sometimes called surrogate models because the (average) performance measured for these models
- * is taken as a surrogate of the performance of the model trained on all cases.</p>
- * <p>
- * All scenarios are described there: https://sebastianraschka.com/faq/docs/evaluate-a-model.html</p>
- */
-public class Step_14_Parallel_Genetic_Programming_Search {
- /** Run example. */
- public static void main(String[] args) {
- try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
- try {
- IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
-
- // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer
- = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
-
- TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>()
- .split(0.75);
-
- Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>()
- .withEncoderType(EncoderType.STRING_ENCODER)
- .withEncodedFeature(1)
- .withEncodedFeature(6)
- .fit(ignite,
- dataCache,
- vectorizer
- );
-
- Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>()
- .fit(ignite,
- dataCache,
- strEncoderPreprocessor
- );
-
- Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>()
- .fit(
- ignite,
- dataCache,
- imputingPreprocessor
- );
-
- NormalizationTrainer<Integer, Vector> normalizationTrainer = new NormalizationTrainer<Integer, Vector>()
- .withP(1);
-
- Preprocessor<Integer, Vector> normalizationPreprocessor = normalizationTrainer
- .fit(
- ignite,
- dataCache,
- minMaxScalerPreprocessor
- );
-
- // Tune hyperparams with K-fold Cross-Validation on the split training set.
-
- DecisionTreeClassificationTrainer trainerCV = new DecisionTreeClassificationTrainer();
-
- CrossValidation<DecisionTreeNode, Double, Integer, Vector> scoreCalculator
- = new CrossValidation<>();
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(new EvolutionOptimizationStrategy())
- .addHyperParam("p", normalizationTrainer::withP, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("maxDeep", trainerCV::withMaxDeep, new Double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0})
- .addHyperParam("minImpurityDecrease", trainerCV::withMinImpurityDecrease, new Double[]{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0});
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withNegativeClsLb(0.0)
- .withPositiveClsLb(1.0)
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withIgnite(ignite)
- .withUpstreamCache(dataCache)
- .withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder()
- .withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL)
- .withLoggingFactoryDependency(ConsoleLogger.Factory.LOW))
- .withTrainer(trainerCV)
- .withMetric(metrics)
- .withFilter(split.getTrainFilter())
- .isRunningOnPipeline(false)
- .withPreprocessor(normalizationPreprocessor)
- .withAmountOfFolds(3)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- System.out.println("Train with maxDeep: " + crossValidationRes.getBest("maxDeep")
- + " and minImpurityDecrease: " + crossValidationRes.getBest("minImpurityDecrease"));
-
- DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer()
- .withMaxDeep(crossValidationRes.getBest("maxDeep"))
- .withMinImpurityDecrease(crossValidationRes.getBest("minImpurityDecrease"));
-
- System.out.println(crossValidationRes);
-
- System.out.println("Best score: " + Arrays.toString(crossValidationRes.getBestScore()));
- System.out.println("Best hyper params: " + crossValidationRes.getBestHyperParams());
- System.out.println("Best average score: " + crossValidationRes.getBestAvgScore());
-
- crossValidationRes.getScoringBoard().forEach((hyperParams, score)
- -> System.out.println("Score " + Arrays.toString(score) + " for hyper params " + hyperParams));
-
- // Train decision tree model.
- DecisionTreeNode bestMdl = trainer.fit(
- ignite,
- dataCache,
- split.getTrainFilter(),
- normalizationPreprocessor
- );
-
- System.out.println("\n>>> Trained model: " + bestMdl);
-
- double accuracy = Evaluator.evaluate(
- dataCache,
- split.getTestFilter(),
- bestMdl,
- normalizationPreprocessor,
- new Accuracy<>()
- );
-
- System.out.println("\n>>> Accuracy " + accuracy);
- System.out.println("\n>>> Test Error " + (1 - accuracy));
-
- System.out.println(">>> Tutorial step 8 (cross-validation with param grid) example started.");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } finally {
- System.out.flush();
- }
- }
-}
\ No newline at end of file
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/util/DatasetHelper.java b/examples/src/main/java/org/apache/ignite/examples/ml/util/DatasetHelper.java
index c904c27..96de9ad 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/util/DatasetHelper.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/util/DatasetHelper.java
@@ -53,7 +53,5 @@ public class DatasetHelper {
System.out.println("Correlation matrix ");
for (double[] row : corr)
System.out.println("\t" + Arrays.toString(row));
-
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/ParametricVectorGeneratorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/ParametricVectorGeneratorExample.java
index fb35b8a..217bb36 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/ParametricVectorGeneratorExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/ParametricVectorGeneratorExample.java
@@ -61,6 +61,5 @@ public class ParametricVectorGeneratorExample {
).asDataStream();
Tracer.showClassificationDatasetHtml("Butterfly", butterfly, 2000, 0, 1, false);
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/StandardGeneratorsExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/StandardGeneratorsExample.java
index 621ebc4..65ff98c 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/StandardGeneratorsExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/StandardGeneratorsExample.java
@@ -71,6 +71,5 @@ public class StandardGeneratorsExample {
RegressionDataStream regression = RegressionDataStream.twoDimensional(
x -> Math.abs(x * x - 10), -10, 10);
Tracer.showRegressionDatasetInHtml("|x^2 - 10|", regression, 1000, 0);
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorFamilyExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorFamilyExample.java
index ff71877..6bd5d30 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorFamilyExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorFamilyExample.java
@@ -61,6 +61,5 @@ public class VectorGeneratorFamilyExample {
Tracer.showClassificationDatasetHtml("Family of ring sectors [both families as two calsses]", family.asDataStream(),
2000, 0, 1, true);
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorPrimitivesExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorPrimitivesExample.java
index e1494cc..cc2a7af 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorPrimitivesExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/util/generators/VectorGeneratorPrimitivesExample.java
@@ -93,6 +93,5 @@ public class VectorGeneratorPrimitivesExample {
}).rotate(Math.PI / 2);
Tracer.showClassificationDatasetHtml("Axe blade", axeBlade.asDataStream(), 1500, 0, 1, false);
- System.out.flush();
}
}
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/xgboost/XGBoostModelParserExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/xgboost/XGBoostModelParserExample.java
index 39162d9..5f75310 100644
--- a/examples/src/main/java/org/apache/ignite/examples/ml/xgboost/XGBoostModelParserExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/xgboost/XGBoostModelParserExample.java
@@ -95,8 +95,6 @@ public class XGBoostModelParserExample {
System.out.println("Expected: " + expPrediction + ", prediction: " + prediction);
}
}
- } finally {
- System.out.flush();
}
}
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/DefaultParallelismStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/DefaultParallelismStrategy.java
index e787aea..66d2fdb 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/DefaultParallelismStrategy.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/DefaultParallelismStrategy.java
@@ -20,7 +20,6 @@ package org.apache.ignite.ml.environment.parallelism;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -39,11 +38,6 @@ public class DefaultParallelismStrategy implements ParallelismStrategy {
return new FutureWrapper<>(pool.submit(task::get));
}
- /** {@inheritDoc} */
- @Override public int getParallelism() {
- return ((ForkJoinPool) pool).getParallelism();
- }
-
/**
* Wrapper for future class.
*
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/NoParallelismStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/NoParallelismStrategy.java
index ecbb4d7..759e06a 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/NoParallelismStrategy.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/NoParallelismStrategy.java
@@ -36,11 +36,6 @@ public class NoParallelismStrategy implements ParallelismStrategy {
}
/** {@inheritDoc} */
- @Override public int getParallelism() {
- return 1;
- }
-
- /** {@inheritDoc} */
@Override public <T> Promise<T> submit(IgniteSupplier<T> task) {
return new Stub<>(task.get());
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/ParallelismStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/ParallelismStrategy.java
index 11d5f55..f27281f 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/ParallelismStrategy.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/environment/parallelism/ParallelismStrategy.java
@@ -17,11 +17,12 @@
package org.apache.ignite.ml.environment.parallelism;
-import java.util.ArrayList;
-import java.util.List;
import org.apache.ignite.ml.math.functions.IgniteFunction;
import org.apache.ignite.ml.math.functions.IgniteSupplier;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Specifies the behaviour of processes in ML-algorithms that can may be parallelized such as parallel learning in
* bagging, learning submodels for One-vs-All model, Cross-Validation etc.
@@ -39,20 +40,15 @@ public interface ParallelismStrategy {
* Submit task.
*
* @param task Task.
- *
- * @return The result of submit operation.
*/
public <T> Promise<T> submit(IgniteSupplier<T> task);
- /** Returns default parallelism. */
- public int getParallelism();
-
/**
* Submit the list of tasks.
*
* @param tasks The task list.
* @param <T> The type of return value.
- * @return The results of the submitted operations list.
+ * @return The result of submit operation.
*/
public default <T> List<Promise<T>> submit(List<IgniteSupplier<T>> tasks) {
List<Promise<T>> results = new ArrayList<>();
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/normalization/NormalizationTrainer.java b/modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/normalization/NormalizationTrainer.java
index b1e50d4..a6493f7 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/normalization/NormalizationTrainer.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/normalization/NormalizationTrainer.java
@@ -54,9 +54,9 @@ public class NormalizationTrainer<K, V> implements PreprocessingTrainer<K, V> {
* @param p The given value.
* @return The Normalization trainer.
*/
- public NormalizationTrainer<K, V> withP(double p) {
+ public NormalizationTrainer<K, V> withP(int p) {
assert p > 0;
- this.p = (int) p;
+ this.p = p;
return this;
}
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionSGDTrainer.java b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionSGDTrainer.java
index 63dfb95..9358aeb 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionSGDTrainer.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionSGDTrainer.java
@@ -158,8 +158,8 @@ public class LogisticRegressionSGDTrainer extends SingleLabelDatasetTrainer<Logi
* @param maxIterations The parameter value.
* @return Model with new max number of iterations before convergence parameter value.
*/
- public LogisticRegressionSGDTrainer withMaxIterations(double maxIterations) {
- this.maxIterations = (int) maxIterations;
+ public LogisticRegressionSGDTrainer withMaxIterations(int maxIterations) {
+ this.maxIterations = maxIterations;
return this;
}
@@ -169,8 +169,8 @@ public class LogisticRegressionSGDTrainer extends SingleLabelDatasetTrainer<Logi
* @param batchSize The size of learning batch.
* @return Trainer with new batch size parameter value.
*/
- public LogisticRegressionSGDTrainer withBatchSize(double batchSize) {
- this.batchSize = (int) batchSize;
+ public LogisticRegressionSGDTrainer withBatchSize(int batchSize) {
+ this.batchSize = batchSize;
return this;
}
@@ -180,8 +180,8 @@ public class LogisticRegressionSGDTrainer extends SingleLabelDatasetTrainer<Logi
* @param amountOfLocIterations The parameter value.
* @return Trainer with new locIterations parameter value.
*/
- public LogisticRegressionSGDTrainer withLocIterations(double amountOfLocIterations) {
- this.locIterations = (int) amountOfLocIterations;
+ public LogisticRegressionSGDTrainer withLocIterations(int amountOfLocIterations) {
+ this.locIterations = amountOfLocIterations;
return this;
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/AbstractCrossValidation.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/AbstractCrossValidation.java
deleted file mode 100644
index be676f7..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/AbstractCrossValidation.java
+++ /dev/null
@@ -1,476 +0,0 @@
-/*
- * 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.ml.selection.cv;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-import java.util.function.BiFunction;
-import java.util.function.DoubleConsumer;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-import org.apache.ignite.lang.IgniteBiPredicate;
-import org.apache.ignite.ml.IgniteModel;
-import org.apache.ignite.ml.dataset.DatasetBuilder;
-import org.apache.ignite.ml.environment.LearningEnvironment;
-import org.apache.ignite.ml.environment.LearningEnvironmentBuilder;
-import org.apache.ignite.ml.environment.parallelism.Promise;
-import org.apache.ignite.ml.math.functions.IgniteSupplier;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.pipeline.Pipeline;
-import org.apache.ignite.ml.pipeline.PipelineMdl;
-import org.apache.ignite.ml.preprocessing.Preprocessor;
-import org.apache.ignite.ml.selection.paramgrid.BruteForceStrategy;
-import org.apache.ignite.ml.selection.paramgrid.EvolutionOptimizationStrategy;
-import org.apache.ignite.ml.selection.paramgrid.HyperParameterTuningStrategy;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.paramgrid.ParameterSetGenerator;
-import org.apache.ignite.ml.selection.paramgrid.RandomStrategy;
-import org.apache.ignite.ml.selection.scoring.cursor.LabelPairCursor;
-import org.apache.ignite.ml.selection.scoring.metric.Metric;
-import org.apache.ignite.ml.selection.split.mapper.SHA256UniformMapper;
-import org.apache.ignite.ml.selection.split.mapper.UniformMapper;
-import org.apache.ignite.ml.trainers.DatasetTrainer;
-import org.apache.ignite.ml.util.genetic.Chromosome;
-import org.apache.ignite.ml.util.genetic.GeneticAlgorithm;
-import org.jetbrains.annotations.NotNull;
-
-/**
- * Cross validation score calculator. Cross validation is an approach that allows to avoid overfitting that is made the
- * following way: the training set is split into k smaller sets. The following procedure is followed for each of the k
- * “folds”:
- * <ul>
- * <li>A model is trained using k-1 of the folds as training data;</li>
- * <li>the resulting model is validated on the remaining part of the data (i.e., it is used as a test set to compute
- * a performance measure such as accuracy).</li>
- * </ul>
- *
- * @param <M> Type of model.
- * @param <L> Type of a label (truth or prediction).
- * @param <K> Type of a key in {@code upstream} data.
- * @param <V> Type of a value in {@code upstream} data.
- */
-public abstract class AbstractCrossValidation<M extends IgniteModel<Vector, L>, L, K, V> {
- /** Learning environment builder. */
- protected LearningEnvironmentBuilder envBuilder = LearningEnvironmentBuilder.defaultBuilder();
-
- /** Learning Environment. */
- protected LearningEnvironment environment = envBuilder.buildForTrainer();
-
- /** Trainer. */
- protected DatasetTrainer<M, L> trainer;
-
- /** Pipeline. */
- protected Pipeline<K, V, Integer, Double> pipeline;
-
- /** Metric. */
- protected Metric<L> metric;
-
- /** Preprocessor. */
- protected Preprocessor<K, V> preprocessor;
-
- /** Filter. */
- protected IgniteBiPredicate<K, V> filter = (k, v) -> true;
-
- /** Amount of folds. */
- protected int amountOfFolds;
-
- /** Parts. */
- protected int parts;
-
- /** Parameter grid. */
- protected ParamGrid paramGrid;
-
- /** Execution over the pipeline or the chain of preprocessors and separate trainer, otherwise. */
- protected boolean isRunningOnPipeline = true;
-
- /** Mapper. */
- protected UniformMapper<K, V> mapper = new SHA256UniformMapper<>();
-
- /**
- * Finds the best set of hyperparameters based on parameter search strategy.
- */
- public CrossValidationResult tuneHyperParamterers() {
- HyperParameterTuningStrategy hyperParamTuningStgy = paramGrid.getHyperParameterTuningStrategy();
-
- if (hyperParamTuningStgy instanceof BruteForceStrategy) return scoreBruteForceHyperparameterOptimization();
- if (hyperParamTuningStgy instanceof RandomStrategy) return scoreRandomSearchHyperparameterOptimization();
- if (hyperParamTuningStgy instanceof EvolutionOptimizationStrategy)
- return scoreEvolutionAlgorithmSearchHyperparameterOptimization();
- else throw new UnsupportedOperationException("This strategy is not supported yet [strategy="
- + paramGrid.getHyperParameterTuningStrategy().getName() + "]");
- }
-
- /**
- * Finds the best set of hyperparameters based on Genetic Programming approach.
- */
- private CrossValidationResult scoreEvolutionAlgorithmSearchHyperparameterOptimization() {
- EvolutionOptimizationStrategy stgy = (EvolutionOptimizationStrategy) paramGrid.getHyperParameterTuningStrategy();
-
- List<Double[]> paramSets = new ParameterSetGenerator(paramGrid.getParamValuesByParamIdx()).generate();
-
- // initialization
- List<Double[]> paramSetsCp = new ArrayList<>(paramSets);
- Collections.shuffle(paramSetsCp, new Random(stgy.getSeed()));
-
- int sizeOfPopulation = 20;
- List<Double[]> rndParamSets = paramSetsCp.subList(0, sizeOfPopulation);
-
- CrossValidationResult cvRes = new CrossValidationResult();
-
- Function<Chromosome, Double> fitnessFunction = (Chromosome chromosome) -> {
- TaskResult tr = calculateScoresForFixedParamSet(chromosome.toDoubleArray());
-
- cvRes.addScores(tr.locScores, tr.paramMap);
-
- final double locAvgScore = Arrays.stream(tr.locScores).average().orElse(Double.MIN_VALUE);
-
- if (locAvgScore >= cvRes.getBestAvgScore()) {
- cvRes.setBestScore(tr.locScores);
- cvRes.setBestHyperParams(tr.paramMap);
- }
-
- return locAvgScore;
- };
-
- Random rnd = new Random(stgy.getSeed()); //TODO: common seed for shared lambdas can produce the same value on each function call? or sequent?
-
- BiFunction<Integer, Double, Double> mutator = (Integer geneIdx, Double geneValue) -> {
- Double newGeneVal;
-
- Double[] possibleGeneValues = paramGrid.getParamRawData().get(geneIdx);
- newGeneVal = possibleGeneValues[rnd.nextInt(possibleGeneValues.length)];
-
- return newGeneVal;
- };
-
- GeneticAlgorithm ga = new GeneticAlgorithm(rndParamSets);
- ga.withFitnessFunction(fitnessFunction)
- .withMutationOperator(mutator)
- .withAmountOfEliteChromosomes(stgy.getAmountOfEliteChromosomes())
- .withCrossingoverProbability(stgy.getCrossingoverProbability())
- .withCrossoverStgy(stgy.getCrossoverStgy())
- .withAmountOfGenerations(stgy.getAmountOfGenerations())
- .withSelectionStgy(stgy.getSelectionStgy())
- .withMutationProbability(stgy.getMutationProbability());
-
- if (environment.parallelismStrategy().getParallelism() > 1)
- ga.runParallel(environment);
- else
- ga.run();
-
- return cvRes;
- }
-
- /**
- * Finds the best set of hyperparameters based on Random Serach.
- */
- private CrossValidationResult scoreRandomSearchHyperparameterOptimization() {
- RandomStrategy stgy = (RandomStrategy) paramGrid.getHyperParameterTuningStrategy();
-
- List<Double[]> paramSets = new ParameterSetGenerator(paramGrid.getParamValuesByParamIdx()).generate();
-
- List<Double[]> paramSetsCp = new ArrayList<>(paramSets);
- Collections.shuffle(paramSetsCp, new Random(stgy.getSeed()));
-
- CrossValidationResult cvRes = new CrossValidationResult();
-
- List<Double[]> rndParamSets = paramSetsCp.subList(0, stgy.getMaxTries());
-
- List<IgniteSupplier<TaskResult>> tasks = rndParamSets.stream()
- .map(paramSet -> (IgniteSupplier<TaskResult>) (() -> calculateScoresForFixedParamSet(paramSet)))
- .collect(Collectors.toList());
-
- List<TaskResult> taskResults = environment.parallelismStrategy().submit(tasks).stream()
- .map(Promise::unsafeGet)
- .collect(Collectors.toList());
-
- taskResults.forEach(tr -> {
- cvRes.addScores(tr.locScores, tr.paramMap);
-
- final double locAvgScore = Arrays.stream(tr.locScores).average().orElse(Double.MIN_VALUE);
-
- if (locAvgScore >= cvRes.getBestAvgScore()) {
- cvRes.setBestScore(tr.locScores);
- cvRes.setBestHyperParams(tr.paramMap);
- }
- });
-
- return cvRes;
- }
-
- /**
- * Finds the best set of hyperparameters based on brute force approach .
- */
- private CrossValidationResult scoreBruteForceHyperparameterOptimization() {
- List<Double[]> paramSets = new ParameterSetGenerator(paramGrid.getParamValuesByParamIdx()).generate();
-
- CrossValidationResult cvRes = new CrossValidationResult();
-
- List<IgniteSupplier<TaskResult>> tasks = paramSets.stream()
- .map(paramSet -> (IgniteSupplier<TaskResult>) (() -> calculateScoresForFixedParamSet(paramSet)))
- .collect(Collectors.toList());
-
- List<TaskResult> taskResults = environment.parallelismStrategy().submit(tasks).stream()
- .map(Promise::unsafeGet)
- .collect(Collectors.toList());
-
- taskResults.forEach(tr -> {
- cvRes.addScores(tr.locScores, tr.paramMap);
-
- final double locAvgScore = Arrays.stream(tr.locScores).average().orElse(Double.MIN_VALUE);
-
- if (locAvgScore > cvRes.getBestAvgScore()) {
- cvRes.setBestScore(tr.locScores);
- cvRes.setBestHyperParams(tr.paramMap);
- }
-
- });
- return cvRes;
- }
-
- /**
- * Represents the scores and map of parameters.
- */
- public static class TaskResult {
- /** Parameter map. */
- private Map<String, Double> paramMap;
-
- /** Local scores. */
- private double[] locScores;
-
- /**
- * @param paramMap Parameter map.
- * @param locScores Locale scores.
- */
- public TaskResult(Map<String, Double> paramMap, double[] locScores) {
- this.paramMap = paramMap;
- this.locScores = locScores;
- }
-
- /**
- * @param paramMap Parameter map.
- */
- public void setParamMap(Map<String, Double> paramMap) {
- this.paramMap = paramMap;
- }
-
- /**
- * @param locScores Local scores.
- */
- public void setLocScores(double[] locScores) {
- this.locScores = locScores;
- }
- }
-
- /**
- * Calculates scores by folds and wrap it to TaskResult object.
- *
- * @param paramSet Parameter set.
- */
- private TaskResult calculateScoresForFixedParamSet(Double[] paramSet) {
- Map<String, Double> paramMap = injectAndGetParametersFromPipeline(paramGrid, paramSet);
-
- double[] locScores = scoreByFolds();
-
- return new TaskResult(paramMap, locScores);
- }
-
- /**
- * Calculates score by folds.
- */
- public abstract double[] scoreByFolds();
-
- /**
- * Forms the parameter map from parameter grid and parameter set.
- *
- * @param paramGrid Parameter grid.
- * @param paramSet Parameter set.
- */
- @NotNull private Map<String, Double> injectAndGetParametersFromPipeline(ParamGrid paramGrid, Double[] paramSet) {
- Map<String, Double> paramMap = new HashMap<>();
-
- for (int paramIdx = 0; paramIdx < paramSet.length; paramIdx++) {
- DoubleConsumer setter = paramGrid.getSetterByIndex(paramIdx);
-
- Double paramVal = paramSet[paramIdx];
- setter.accept(paramVal);
-
- paramMap.put(paramGrid.getParamNameByIndex(paramIdx), paramVal);
-
- }
- return paramMap;
- }
-
- /**
- * Computes cross-validated metrics.
- *
- * @param datasetBuilderSupplier Dataset builder supplier.
- * @param testDataIterSupplier Test data iterator supplier.
- * @return Array of scores of the estimator for each run of the cross validation.
- */
- protected double[] score(Function<IgniteBiPredicate<K, V>, DatasetBuilder<K, V>> datasetBuilderSupplier,
- BiFunction<IgniteBiPredicate<K, V>, M, LabelPairCursor<L>> testDataIterSupplier) {
- double[] scores = new double[amountOfFolds];
-
- double foldSize = 1.0 / amountOfFolds;
- for (int i = 0; i < amountOfFolds; i++) {
- double from = foldSize * i;
- double to = foldSize * (i + 1);
-
- IgniteBiPredicate<K, V> trainSetFilter = (k, v) -> {
- double pnt = mapper.map(k, v);
- return pnt < from || pnt > to;
- };
-
- DatasetBuilder<K, V> datasetBuilder = datasetBuilderSupplier.apply(trainSetFilter);
- M mdl = trainer.fit(datasetBuilder, preprocessor); //TODO: IGNITE-11580
-
- try (LabelPairCursor<L> cursor = testDataIterSupplier.apply(trainSetFilter, mdl)) {
- scores[i] = metric.score(cursor.iterator());
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- return scores;
- }
-
- /**
- * Computes cross-validated metrics.
- *
- * @param datasetBuilderSupplier Dataset builder supplier.
- * @param testDataIterSupplier Test data iterator supplier.
- * @return Array of scores of the estimator for each run of the cross validation.
- */
- protected double[] scorePipeline(Function<IgniteBiPredicate<K, V>, DatasetBuilder<K, V>> datasetBuilderSupplier,
- BiFunction<IgniteBiPredicate<K, V>, M, LabelPairCursor<L>> testDataIterSupplier) {
- double[] scores = new double[amountOfFolds];
-
- double foldSize = 1.0 / amountOfFolds;
- for (int i = 0; i < amountOfFolds; i++) {
- double from = foldSize * i;
- double to = foldSize * (i + 1);
-
- IgniteBiPredicate<K, V> trainSetFilter = (k, v) -> {
- double pnt = mapper.map(k, v);
- return pnt < from || pnt > to;
- };
-
- DatasetBuilder<K, V> datasetBuilder = datasetBuilderSupplier.apply(trainSetFilter);
- PipelineMdl<K, V> mdl = pipeline.fit(datasetBuilder);
-
- try (LabelPairCursor<L> cursor = testDataIterSupplier.apply(trainSetFilter, (M) mdl)) {
- scores[i] = metric.score(cursor.iterator());
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- return scores;
- }
-
- /**
- * @param trainer Trainer.
- */
- public AbstractCrossValidation<M, L, K, V> withTrainer(DatasetTrainer<M, L> trainer) {
- this.trainer = trainer;
- return this;
- }
-
- /**
- * @param metric Metric.
- */
- public AbstractCrossValidation<M, L, K, V> withMetric(Metric<L> metric) {
- this.metric = metric;
- return this;
- }
-
- /**
- * @param preprocessor Preprocessor.
- */
- public AbstractCrossValidation<M, L, K, V> withPreprocessor(Preprocessor<K, V> preprocessor) {
- this.preprocessor = preprocessor;
- return this;
- }
-
- /**
- * @param filter Filter.
- */
- public AbstractCrossValidation<M, L, K, V> withFilter(IgniteBiPredicate<K, V> filter) {
- this.filter = filter;
- return this;
- }
-
- /**
- * @param amountOfFolds Amount of folds.
- */
- public AbstractCrossValidation<M, L, K, V> withAmountOfFolds(int amountOfFolds) {
- this.amountOfFolds = amountOfFolds;
- return this;
- }
-
- /**
- * @param paramGrid Parameter grid.
- */
- public AbstractCrossValidation<M, L, K, V> withParamGrid(ParamGrid paramGrid) {
- this.paramGrid = paramGrid;
- return this;
- }
-
- /**
- * @param runningOnPipeline Running on pipeline.
- */
- public AbstractCrossValidation<M, L, K, V> isRunningOnPipeline(boolean runningOnPipeline) {
- isRunningOnPipeline = runningOnPipeline;
- return this;
- }
-
- /**
- * Changes learning Environment.
- *
- * @param envBuilder Learning environment builder.
- */
- // TODO: IGNITE-10441 Think about more elegant ways to perform fluent API.
- public AbstractCrossValidation<M, L, K, V> withEnvironmentBuilder(LearningEnvironmentBuilder envBuilder) {
- this.envBuilder = envBuilder;
- environment = envBuilder.buildForTrainer();
-
- return this;
- }
-
- /**
- * @param pipeline Pipeline.
- */
- public AbstractCrossValidation<M, L, K, V> withPipeline(Pipeline<K, V, Integer, Double> pipeline) {
- this.pipeline = pipeline;
- return this;
- }
-
- /**
- * @param mapper Mapper.
- */
- public AbstractCrossValidation<M, L, K, V> withMapper(UniformMapper<K, V> mapper) {
- this.mapper = mapper;
- return this;
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java
index 159285b..b3c70e5 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java
@@ -17,21 +17,42 @@
package org.apache.ignite.ml.selection.cv;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.Function;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
+import org.apache.ignite.lang.IgniteBiPredicate;
import org.apache.ignite.ml.IgniteModel;
+import org.apache.ignite.ml.dataset.DatasetBuilder;
import org.apache.ignite.ml.dataset.impl.cache.CacheBasedDatasetBuilder;
+import org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder;
import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.pipeline.Pipeline;
import org.apache.ignite.ml.pipeline.PipelineMdl;
+import org.apache.ignite.ml.preprocessing.Preprocessor;
+import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
+import org.apache.ignite.ml.selection.paramgrid.ParameterSetGenerator;
import org.apache.ignite.ml.selection.scoring.cursor.CacheBasedLabelPairCursor;
+import org.apache.ignite.ml.selection.scoring.cursor.LabelPairCursor;
+import org.apache.ignite.ml.selection.scoring.cursor.LocalLabelPairCursor;
+import org.apache.ignite.ml.selection.scoring.metric.Metric;
+import org.apache.ignite.ml.selection.split.mapper.SHA256UniformMapper;
+import org.apache.ignite.ml.selection.split.mapper.UniformMapper;
+import org.apache.ignite.ml.trainers.DatasetTrainer;
/**
* Cross validation score calculator. Cross validation is an approach that allows to avoid overfitting that is made the
* following way: the training set is split into k smaller sets. The following procedure is followed for each of the k
* “folds”:
* <ul>
- * <li>A model is trained using k-1 of the folds as training data;</li>
- * <li>the resulting model is validated on the remaining part of the data (i.e., it is used as a test set to compute
+ * <li>A model is trained using k-1 of the folds as training data;</li>
+ * <li>the resulting model is validated on the remaining part of the data (i.e., it is used as a test set to compute
* a performance measure such as accuracy).</li>
* </ul>
*
@@ -40,31 +61,136 @@ import org.apache.ignite.ml.selection.scoring.cursor.CacheBasedLabelPairCursor;
* @param <K> Type of a key in {@code upstream} data.
* @param <V> Type of a value in {@code upstream} data.
*/
-public class CrossValidation<M extends IgniteModel<Vector, L>, L, K, V> extends AbstractCrossValidation<M, L, K, V> {
- /** Ignite. */
- private Ignite ignite;
+public class CrossValidation<M extends IgniteModel<Vector, L>, L, K, V> {
+ /**
+ * Computes cross-validated metrics.
+ *
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Score calculator.
+ * @param ignite Ignite instance.
+ * @param upstreamCache Ignite cache with {@code upstream} data.
+ * @param preprocessor Preprocessor.
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
+ */
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Ignite ignite,
+ IgniteCache<K, V> upstreamCache, Preprocessor<K, V> preprocessor, int cv) {
+ return score(trainer, scoreCalculator, ignite, upstreamCache, (k, v) -> true, preprocessor,
+ new SHA256UniformMapper<>(), cv);
+ }
- /** Upstream cache. */
- private IgniteCache<K, V> upstreamCache;
+ /**
+ * Computes cross-validated metrics.
+ *
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param ignite Ignite instance.
+ * @param upstreamCache Ignite cache with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param preprocessor Preprocessor.
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
+ */
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Ignite ignite,
+ IgniteCache<K, V> upstreamCache, IgniteBiPredicate<K, V> filter,
+ Preprocessor<K, V> preprocessor, int cv) {
+ return score(trainer, scoreCalculator, ignite, upstreamCache, filter, preprocessor,
+ new SHA256UniformMapper<>(), cv);
+ }
/**
- * Calculates score by folds.
+ * Computes cross-validated metrics with a passed parameter grid.
+ *
+ * The real cross-validation training will be called each time for each parameter set.
+ *
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param ignite Ignite instance.
+ * @param upstreamCache Ignite cache with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param preprocessor Preprocessor.
+ * @param amountOfFolds Amount of folds.
+ * @param paramGrid Parameter grid.
+ * @return Array of scores of the estimator for each run of the cross validation.
*/
- @Override public double[] scoreByFolds() {
- double[] locScores;
+ public CrossValidationResult score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Ignite ignite,
+ IgniteCache<K, V> upstreamCache, IgniteBiPredicate<K, V> filter,
+ Preprocessor<K, V> preprocessor, int amountOfFolds,
+ ParamGrid paramGrid) {
+
+ List<Double[]> paramSets = new ParameterSetGenerator(paramGrid.getParamValuesByParamIdx()).generate();
+
+ CrossValidationResult cvRes = new CrossValidationResult();
+
+ paramSets.forEach(paramSet -> {
+ Map<String, Double> paramMap = new HashMap<>();
+
+ for (int paramIdx = 0; paramIdx < paramSet.length; paramIdx++) {
+ String paramName = paramGrid.getParamNameByIndex(paramIdx);
+ Double paramVal = paramSet[paramIdx];
+
+ paramMap.put(paramName, paramVal);
+
+ try {
+ final String mtdName = "with" +
+ paramName.substring(0, 1).toUpperCase() +
+ paramName.substring(1);
+
+ Method trainerSetter = null;
+
+ // We should iterate along all methods due to we have no info about signature and passed types.
+ for (Method method : trainer.getClass().getDeclaredMethods()) {
+ if (method.getName().equals(mtdName))
+ trainerSetter = method;
+ }
+
+ if (trainerSetter != null)
+ trainerSetter.invoke(trainer, paramVal);
+ else
+ throw new NoSuchMethodException(mtdName);
- locScores = isRunningOnPipeline ? scorePipelineOnIgnite() : scoreOnIgnite();
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ }
- return locScores;
+ double[] locScores = score(trainer, scoreCalculator, ignite, upstreamCache, filter, preprocessor,
+ new SHA256UniformMapper<>(), amountOfFolds);
+
+ cvRes.addScores(locScores, paramMap);
+
+ final double locAvgScore = Arrays.stream(locScores).average().orElse(Double.MIN_VALUE);
+
+ if (locAvgScore > cvRes.getBestAvgScore()) {
+ cvRes.setBestScore(locScores);
+ cvRes.setBestHyperParams(paramMap);
+ System.out.println(paramMap.toString());
+ }
+ });
+
+ return cvRes;
}
/**
- * Calculate score on pipeline based on Ignite data (upstream cache).
+ * Computes cross-validated metrics.
*
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param ignite Ignite instance.
+ * @param upstreamCache Ignite cache with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param preprocessor Preprocessor.
+ * @param mapper Mapper used to map a key-value pair to a point on the segment (0, 1).
+ * @param cv Number of folds.
* @return Array of scores of the estimator for each run of the cross validation.
*/
- private double[] scorePipelineOnIgnite() {
- return scorePipeline(
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator,
+ Ignite ignite, IgniteCache<K, V> upstreamCache, IgniteBiPredicate<K, V> filter,
+ Preprocessor<K, V> preprocessor,
+ UniformMapper<K, V> mapper, int cv) {
+
+ return score(
+ trainer,
predicate -> new CacheBasedDatasetBuilder<>(
ignite,
upstreamCache,
@@ -73,46 +199,262 @@ public class CrossValidation<M extends IgniteModel<Vector, L>, L, K, V> extends
(predicate, mdl) -> new CacheBasedLabelPairCursor<>(
upstreamCache,
(k, v) -> filter.apply(k, v) && !predicate.apply(k, v),
- ((PipelineMdl<K, V>) mdl).getPreprocessor(),
+ preprocessor,
mdl
- )
+ ),
+ preprocessor,
+ scoreCalculator,
+ mapper,
+ cv
);
}
/**
* Computes cross-validated metrics.
*
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param upstreamMap Map with {@code upstream} data.
+ * @param parts Number of partitions.
+ * @param preprocessor Preprocessor.
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
+ */
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Map<K, V> upstreamMap,
+ int parts, Preprocessor<K, V> preprocessor, int cv) {
+ return score(trainer, scoreCalculator, upstreamMap, (k, v) -> true, parts, preprocessor,
+ new SHA256UniformMapper<>(), cv);
+ }
+
+ /**
+ * Computes cross-validated metrics.
+ *
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param upstreamMap Map with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param parts Number of partitions.
+ * @param preprocessor Preprocessor.
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
+ */
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Map<K, V> upstreamMap,
+ IgniteBiPredicate<K, V> filter, int parts, Preprocessor<K, V> preprocessor, int cv) {
+ return score(trainer, scoreCalculator, upstreamMap, filter, parts, preprocessor,
+ new SHA256UniformMapper<>(), cv);
+ }
+
+ /**
+ * Computes cross-validated metrics.
+ *
+ * @param trainer Trainer of the model.
+ * @param scoreCalculator Base score calculator.
+ * @param upstreamMap Map with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param parts Number of partitions.
+ * @param preprocessor Preprocessor.
+ * @param mapper Mapper used to map a key-value pair to a point on the segment (0, 1).
+ * @param cv Number of folds.
* @return Array of scores of the estimator for each run of the cross validation.
*/
- private double[] scoreOnIgnite() {
+ public double[] score(DatasetTrainer<M, L> trainer, Metric<L> scoreCalculator, Map<K, V> upstreamMap,
+ IgniteBiPredicate<K, V> filter, int parts, Preprocessor<K, V> preprocessor, UniformMapper<K, V> mapper, int cv) {
return score(
- predicate -> new CacheBasedDatasetBuilder<>(
- ignite,
- upstreamCache,
- (k, v) -> filter.apply(k, v) && predicate.apply(k, v)
+ trainer,
+ predicate -> new LocalDatasetBuilder<>(
+ upstreamMap,
+ (k, v) -> filter.apply(k, v) && predicate.apply(k, v),
+ parts
),
- (predicate, mdl) -> new CacheBasedLabelPairCursor<>(
- upstreamCache,
+ (predicate, mdl) -> new LocalLabelPairCursor<>(
+ upstreamMap,
(k, v) -> filter.apply(k, v) && !predicate.apply(k, v),
preprocessor,
mdl
- )
+ ),
+ preprocessor,
+ scoreCalculator,
+ mapper,
+ cv
);
}
/**
- * @param ignite Ignite.
+ * Computes cross-validated metrics.
+ *
+ * @param trainer Trainer of the model.
+ * @param datasetBuilderSupplier Dataset builder supplier.
+ * @param testDataIterSupplier Test data iterator supplier.
+ * @param scoreCalculator Base score calculator.
+ * @param mapper Mapper used to map a key-value pair to a point on the segment (0, 1).
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
+ */
+ private double[] score(DatasetTrainer<M, L> trainer, Function<IgniteBiPredicate<K, V>,
+ DatasetBuilder<K, V>> datasetBuilderSupplier,
+ BiFunction<IgniteBiPredicate<K, V>, M, LabelPairCursor<L>> testDataIterSupplier,
+ Preprocessor<K, V> preprocessor,
+ Metric<L> scoreCalculator, UniformMapper<K, V> mapper, int cv) {
+
+ double[] scores = new double[cv];
+
+ double foldSize = 1.0 / cv;
+ for (int i = 0; i < cv; i++) {
+ double from = foldSize * i;
+ double to = foldSize * (i + 1);
+
+ IgniteBiPredicate<K, V> trainSetFilter = (k, v) -> {
+ double pnt = mapper.map(k, v);
+ return pnt < from || pnt > to;
+ };
+
+ DatasetBuilder<K, V> datasetBuilder = datasetBuilderSupplier.apply(trainSetFilter);
+ M mdl = trainer.fit(datasetBuilder, preprocessor); //TODO: IGNITE-11580
+
+ try (LabelPairCursor<L> cursor = testDataIterSupplier.apply(trainSetFilter, mdl)) {
+ scores[i] = scoreCalculator.score(cursor.iterator());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return scores;
+ }
+
+ /**
+ * Computes cross-validated metrics with a passed parameter grid.
+ *
+ * The real cross-validation training will be called each time for each parameter set.
+ *
+ * @param pipeline Pipeline of stages.
+ * @param scoreCalculator Base score calculator.
+ * @param ignite Ignite instance.
+ * @param upstreamCache Ignite cache with {@code upstream} data.
+ * @param filter Base {@code upstream} data filter.
+ * @param amountOfFolds Amount of folds.
+ * @param paramGrid Parameter grid.
+ * @return Array of scores of the estimator for each run of the cross validation.
*/
- public CrossValidation<M, L, K, V> withIgnite(Ignite ignite) {
- this.ignite = ignite;
- return this;
+ public CrossValidationResult score(Pipeline<K, V, Integer, Double> pipeline,
+ Metric<L> scoreCalculator,
+ Ignite ignite,
+ IgniteCache<K, V> upstreamCache,
+ IgniteBiPredicate<K, V> filter,
+ int amountOfFolds,
+ ParamGrid paramGrid) {
+
+ List<Double[]> paramSets = new ParameterSetGenerator(paramGrid.getParamValuesByParamIdx()).generate();
+
+ CrossValidationResult cvRes = new CrossValidationResult();
+
+ DatasetTrainer trainer = pipeline.getTrainer();
+
+ paramSets.forEach(paramSet -> {
+ Map<String, Double> paramMap = new HashMap<>();
+
+ for (int paramIdx = 0; paramIdx < paramSet.length; paramIdx++) {
+ String paramName = paramGrid.getParamNameByIndex(paramIdx);
+ Double paramVal = paramSet[paramIdx];
+
+ paramMap.put(paramName, paramVal);
+
+ try {
+ final String mtdName = "with" +
+ paramName.substring(0, 1).toUpperCase() +
+ paramName.substring(1);
+
+ Method trainerSetter = null;
+
+ // We should iterate along all methods due to we have no info about signature and passed types.
+ for (Method method : trainer.getClass().getDeclaredMethods()) {
+ if (method.getName().equals(mtdName))
+ trainerSetter = method;
+ }
+
+ if (trainerSetter != null)
+ trainerSetter.invoke(trainer, paramVal);
+ else
+ throw new NoSuchMethodException(mtdName);
+
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ }
+
+ double[] locScores = scorePipeline(
+ pipeline,
+ predicate -> new CacheBasedDatasetBuilder<>(
+ ignite,
+ upstreamCache,
+ (k, v) -> filter.apply(k, v) && predicate.apply(k, v)
+ ),
+ (predicate, mdl) -> new CacheBasedLabelPairCursor<>(
+ upstreamCache,
+ (k, v) -> filter.apply(k, v) && !predicate.apply(k, v),
+ ((PipelineMdl<K, V>) mdl).getPreprocessor(),
+ mdl
+ ),
+ scoreCalculator,
+ new SHA256UniformMapper<>(),
+ amountOfFolds
+ );
+
+ cvRes.addScores(locScores, paramMap);
+
+ final double locAvgScore = Arrays.stream(locScores).average().orElse(Double.MIN_VALUE);
+
+ if (locAvgScore > cvRes.getBestAvgScore()) {
+ cvRes.setBestScore(locScores);
+ cvRes.setBestHyperParams(paramMap);
+ System.out.println(paramMap.toString());
+ }
+ });
+
+ return cvRes;
+
}
/**
- * @param upstreamCache Upstream cache.
+ * Computes cross-validated metrics.
+ *
+ * @param pipeline Pipeline of stages.
+ * @param datasetBuilderSupplier Dataset builder supplier.
+ * @param testDataIterSupplier Test data iterator supplier.
+ * @param scoreCalculator Base score calculator.
+ * @param mapper Mapper used to map a key-value pair to a point on the segment (0, 1).
+ * @param cv Number of folds.
+ * @return Array of scores of the estimator for each run of the cross validation.
*/
- public CrossValidation<M, L, K, V> withUpstreamCache(IgniteCache<K, V> upstreamCache) {
- this.upstreamCache = upstreamCache;
- return this;
+ private double[] scorePipeline(Pipeline<K, V, Integer, Double> pipeline,
+ Function<IgniteBiPredicate<K, V>, DatasetBuilder<K, V>> datasetBuilderSupplier,
+ BiFunction<IgniteBiPredicate<K, V>, M, LabelPairCursor<L>> testDataIterSupplier,
+ Metric<L> scoreCalculator,
+ UniformMapper<K, V> mapper,
+ int cv
+ ) {
+
+ double[] scores = new double[cv];
+
+ double foldSize = 1.0 / cv;
+ for (int i = 0; i < cv; i++) {
+ double from = foldSize * i;
+ double to = foldSize * (i + 1);
+
+ IgniteBiPredicate<K, V> trainSetFilter = (k, v) -> {
+ double pnt = mapper.map(k, v);
+ return pnt < from || pnt > to;
+ };
+
+ DatasetBuilder<K, V> datasetBuilder = datasetBuilderSupplier.apply(trainSetFilter);
+ PipelineMdl<K, V> mdl = pipeline.fit(datasetBuilder);
+
+ try (LabelPairCursor<L> cursor = testDataIterSupplier.apply(trainSetFilter, (M) mdl)) {
+ scores[i] = scoreCalculator.score(cursor.iterator());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return scores;
}
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidationResult.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidationResult.java
index f7ddae3..6f11eb6 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidationResult.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidationResult.java
@@ -18,7 +18,6 @@
package org.apache.ignite.ml.selection.cv;
import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -41,12 +40,18 @@ public class CrossValidationResult {
private Map<Map<String, Double>, double[]> scoringBoard = new HashMap<>();
/**
+ * Default constructor.
+ */
+ CrossValidationResult() {
+ }
+
+ /**
* Gets the best value for the specific hyper parameter.
*
* @param hyperParamName Hyper parameter name.
* @return The value.
*/
- public synchronized double getBest(String hyperParamName) {
+ public double getBest(String hyperParamName) {
return bestHyperParams.get(hyperParamName);
}
@@ -55,7 +60,7 @@ public class CrossValidationResult {
*
* @return The value.
*/
- public synchronized double[] getBestScore() {
+ public double[] getBestScore() {
return bestScore;
}
@@ -65,18 +70,16 @@ public class CrossValidationResult {
* @param locScores The scores.
* @param paramMap The parameter set associated with the given scores.
*/
- synchronized void addScores(double[] locScores, Map<String, Double> paramMap) {
+ void addScores(double[] locScores, Map<String, Double> paramMap) {
scoringBoard.put(paramMap, locScores);
}
/**
* Gets the the average value of best score array.
*
- * Default value is Double.MIN_VALUE.
- *
* @return The value.
*/
- public synchronized double getBestAvgScore() {
+ public double getBestAvgScore() {
if (bestScore == null)
return Double.MIN_VALUE;
return Arrays.stream(bestScore).average().orElse(Double.MIN_VALUE);
@@ -87,7 +90,7 @@ public class CrossValidationResult {
*
* @param bestScore The best score.
*/
- synchronized void setBestScore(double[] bestScore) {
+ void setBestScore(double[] bestScore) {
this.bestScore = bestScore;
}
@@ -96,7 +99,7 @@ public class CrossValidationResult {
*
* @param bestHyperParams The best hyper parameters.
*/
- public synchronized void setBestHyperParams(Map<String, Double> bestHyperParams) {
+ void setBestHyperParams(Map<String, Double> bestHyperParams) {
this.bestHyperParams = bestHyperParams;
}
@@ -108,8 +111,8 @@ public class CrossValidationResult {
*
* @return The Scoring Board.
*/
- public synchronized Map<Map<String, Double>, double[]> getScoringBoard() {
- return Collections.unmodifiableMap(scoringBoard);
+ public Map<Map<String, Double>, double[]> getScoringBoard() {
+ return scoringBoard;
}
/**
@@ -117,12 +120,12 @@ public class CrossValidationResult {
*
* @return The value.
*/
- public synchronized Map<String, Double> getBestHyperParams() {
- return Collections.unmodifiableMap(bestHyperParams);
+ public Map<String, Double> getBestHyperParams() {
+ return bestHyperParams;
}
/** {@inheritDoc} */
- @Override public synchronized String toString() {
+ @Override public String toString() {
return "CrossValidationResult{" +
"bestHyperParams=" + bestHyperParams +
", bestScore=" + Arrays.toString(bestScore) +
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/DebugCrossValidation.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/DebugCrossValidation.java
deleted file mode 100644
index 6a1ba27..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/DebugCrossValidation.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * 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.ml.selection.cv;
-
-import java.util.Map;
-import org.apache.ignite.ml.IgniteModel;
-import org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder;
-import org.apache.ignite.ml.math.primitives.vector.Vector;
-import org.apache.ignite.ml.selection.scoring.cursor.LocalLabelPairCursor;
-
-/**
- * Cross validation score calculator. Cross validation is an approach that allows to avoid overfitting that is made the
- * following way: the training set is split into k smaller sets. The following procedure is followed for each of the k
- * “folds”:
- * <ul>
- * <li>A model is trained using k-1 of the folds as training data;</li>
- * <li>the resulting model is validated on the remaining part of the data (i.e., it is used as a test set to compute
- * a performance measure such as accuracy).</li>
- * </ul>
- *
- * @param <M> Type of model.
- * @param <L> Type of a label (truth or prediction).
- * @param <K> Type of a key in {@code upstream} data.
- * @param <V> Type of a value in {@code upstream} data.
- */
-public class DebugCrossValidation<M extends IgniteModel<Vector, L>, L, K, V> extends AbstractCrossValidation<M, L, K, V> {
- /** Upstream map. */
- private Map<K, V> upstreamMap;
-
- /** Parts. */
- private int parts;
-
- /**
- * Calculates score by folds.
- */
- @Override public double[] scoreByFolds() {
- double[] locScores;
-
- locScores = isRunningOnPipeline ? scorePipelineLocally() : scoreLocally();
-
- return locScores;
- }
-
- /**
- * Calculate score on pipeline based on local data (upstream map).
- *
- * @return Array of scores of the estimator for each run of the cross validation.
- */
- private double[] scorePipelineLocally() {
- return scorePipeline(
- predicate -> new LocalDatasetBuilder<>(
- upstreamMap,
- (k, v) -> filter.apply(k, v) && predicate.apply(k, v),
- parts
- ),
- (predicate, mdl) -> new LocalLabelPairCursor<>(
- upstreamMap,
- (k, v) -> filter.apply(k, v) && !predicate.apply(k, v),
- preprocessor,
- mdl
- )
- );
- }
-
- /**
- * Computes cross-validated metrics.
- *
- * @return Array of scores of the estimator for each run of the cross validation.
- */
- private double[] scoreLocally() {
- return score(
- predicate -> new LocalDatasetBuilder<>(
- upstreamMap,
- (k, v) -> filter.apply(k, v) && predicate.apply(k, v),
- parts
- ),
- (predicate, mdl) -> new LocalLabelPairCursor<>(
- upstreamMap,
- (k, v) -> filter.apply(k, v) && !predicate.apply(k, v),
- preprocessor,
- mdl
- )
- );
- }
-
- /**
- * @param upstreamMap Upstream map.
- */
- public DebugCrossValidation<M, L, K, V> withUpstreamMap(Map<K, V> upstreamMap) {
- this.upstreamMap = upstreamMap;
- return this;
- }
-
- /**
- * @param parts Parts.
- */
- public DebugCrossValidation<M, L, K, V> withAmountOfParts(int parts) {
- this.parts = parts;
- return this;
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/BruteForceStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/BruteForceStrategy.java
deleted file mode 100644
index fbc7a72..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/BruteForceStrategy.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.ml.selection.paramgrid;
-
-/**
- * This strategy enables the brute-force search in hyper-parameter space.
- */
-public class BruteForceStrategy extends HyperParameterTuningStrategy {
- /** {@inheritDoc} */
- @Override public String getName() {
- return "Brute Force";
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/EvolutionOptimizationStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/EvolutionOptimizationStrategy.java
deleted file mode 100644
index 11869cd..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/EvolutionOptimizationStrategy.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * 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.ml.selection.paramgrid;
-
-import org.apache.ignite.ml.util.genetic.CrossoverStrategy;
-import org.apache.ignite.ml.util.genetic.SelectionStrategy;
-
-/** Represents the Genetic algorithms usage for finding the best set of hyper-parameters. */
-public class EvolutionOptimizationStrategy extends HyperParameterTuningStrategy {
- /** Seed. */
- private long seed = 1234L;
-
- /** Amount of elite chromosomes. */
- private int amountOfEliteChromosomes = 2;
-
- /** Amount of generations. */
- private int amountOfGenerations = 10;
-
- /** Crossingover probability. */
- private double crossingoverProbability = 0.9;
-
- /** Mutation probability. */
- private double mutationProbability = 0.1;
-
- /** Crossover strategy. */
- private CrossoverStrategy crossoverStgy = CrossoverStrategy.UNIFORM;
-
- /** Selection strategy. */
- private SelectionStrategy selectionStgy = SelectionStrategy.ROULETTE_WHEEL;
-
- /**
- *
- */
- public long getSeed() {
- return seed;
- }
-
- /**
- *
- */
- public int getAmountOfGenerations() {
- return amountOfGenerations;
- }
-
- /**
- *
- */
- public int getAmountOfEliteChromosomes() {
- return amountOfEliteChromosomes;
- }
-
- /**
- *
- */
- public double getCrossingoverProbability() {
- return crossingoverProbability;
- }
-
- /**
- *
- */
- public double getMutationProbability() {
- return mutationProbability;
- }
-
- /**
- *
- */
- public CrossoverStrategy getCrossoverStgy() {
- return crossoverStgy;
- }
-
- /**
- *
- */
- public SelectionStrategy getSelectionStgy() {
- return selectionStgy;
- }
-
- /**
- * Set up the seed number.
- *
- * @param seed Seed.
- */
- public EvolutionOptimizationStrategy withSeed(long seed) {
- this.seed = seed;
- return this;
- }
-
- /**
- * @param crossingoverProbability Crossingover probability.
- */
- public EvolutionOptimizationStrategy withCrossingoverProbability(double crossingoverProbability) {
- this.crossingoverProbability = crossingoverProbability;
- return this;
- }
-
- /**
- * @param mutationProbability Mutation probability.
- */
- public EvolutionOptimizationStrategy withMutationProbability(double mutationProbability) {
- this.mutationProbability = mutationProbability;
- return this;
- }
-
- /**
- * @param crossoverStgy Crossover strategy.
- */
- public EvolutionOptimizationStrategy withCrossoverStgy(CrossoverStrategy crossoverStgy) {
- this.crossoverStgy = crossoverStgy;
- return this;
- }
-
- /**
- * @param selectionStgy Selection strategy.
- */
- public EvolutionOptimizationStrategy withSelectionStgy(SelectionStrategy selectionStgy) {
- this.selectionStgy = selectionStgy;
- return this;
- }
-
-
- /** {@inheritDoc} */
- @Override public String getName() {
- return "Evolution Optimization";
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/HyperParameterTuningStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/HyperParameterTuningStrategy.java
deleted file mode 100644
index 2d5b6b8..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/HyperParameterTuningStrategy.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.ml.selection.paramgrid;
-
-/**
- * The parent abstract class of all hyper-parameter tuning strategies.
- */
-public abstract class HyperParameterTuningStrategy {
- /**
- * Returns the name of strategy.
- */
- public abstract String getName();
-}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParamGrid.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParamGrid.java
index 48a0af1..f9c5bd2 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParamGrid.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParamGrid.java
@@ -17,12 +17,8 @@
package org.apache.ignite.ml.selection.paramgrid;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import java.util.function.DoubleConsumer;
/**
* Keeps the grid of parameters.
@@ -32,69 +28,31 @@ public class ParamGrid {
private Map<Integer, Double[]> paramValuesByParamIdx = new HashMap<>();
/** Parameter names by parameter index. */
- private Map<Integer, DoubleConsumer> settersByParamIdx = new HashMap<>();
-
- /** Parameter names by parameter index. */
private Map<Integer, String> paramNamesByParamIdx = new HashMap<>();
/** Parameter counter. */
private int paramCntr;
- /** Parameter search strategy. */
- private HyperParameterTuningStrategy paramSearchStgy = new BruteForceStrategy();
-
/** */
public Map<Integer, Double[]> getParamValuesByParamIdx() {
- return Collections.unmodifiableMap(paramValuesByParamIdx);
+ return paramValuesByParamIdx;
}
/**
* Adds a grid for the specific hyper parameter.
* @param paramName The parameter name.
- * @param setter The method reference to trainer or preprocessor trainer setter.
* @param params The array of the given hyper parameter values.
* @return The updated ParamGrid.
*/
- public ParamGrid addHyperParam(String paramName, DoubleConsumer setter, Double[] params) {
+ public ParamGrid addHyperParam(String paramName, Double[] params) {
paramValuesByParamIdx.put(paramCntr, params);
paramNamesByParamIdx.put(paramCntr, paramName);
- settersByParamIdx.put(paramCntr, setter);
paramCntr++;
return this;
}
- /**
- * Set up the hyperparameter searching strategy.
- *
- * @param paramSearchStgy Parameter search strategy.
- */
- public ParamGrid withParameterSearchStrategy(HyperParameterTuningStrategy paramSearchStgy) {
- this.paramSearchStgy = paramSearchStgy;
- return this;
- }
-
-
- /** Returns the Hyper-parameter tuning strategy. */
- public HyperParameterTuningStrategy getHyperParameterTuningStrategy() {
- return paramSearchStgy;
- }
-
- /** Returns setter for parameter with the given index. */
- public DoubleConsumer getSetterByIndex(int idx) {
- return settersByParamIdx.get(idx);
- }
-
- /** Returns the name of hyper-parameter by the given index. */
+ /** */
public String getParamNameByIndex(int idx) {
return paramNamesByParamIdx.get(idx);
}
-
- /**
- * Prepare data for hyper-parameter tuning.
- */
- public List<Double[]> getParamRawData() {
- List<Double[]> res = new ArrayList<>();
- paramValuesByParamIdx.forEach(res::add);
- return res;
- }
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParameterSetGenerator.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParameterSetGenerator.java
index 0aedfbf..b6c86a1 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParameterSetGenerator.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/ParameterSetGenerator.java
@@ -19,7 +19,6 @@ package org.apache.ignite.ml.selection.paramgrid;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -50,7 +49,7 @@ public class ParameterSetGenerator {
assert !map.isEmpty();
this.map = map;
- sizeOfParamVector = map.size();
+ this.sizeOfParamVector = map.size();
}
/**
@@ -62,7 +61,7 @@ public class ParameterSetGenerator {
traverseTree(map, nextPnt, -1);
- return Collections.unmodifiableList(params);
+ return params;
}
/**
@@ -77,6 +76,7 @@ public class ParameterSetGenerator {
if (dimensionNum == sizeOfParamVector){
Double[] paramSet = Arrays.copyOf(nextPnt, sizeOfParamVector);
+ System.out.println(Arrays.toString(paramSet));
params.add(paramSet);
return;
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/RandomStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/RandomStrategy.java
deleted file mode 100644
index 5f6d7d3..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/paramgrid/RandomStrategy.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * 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.ml.selection.paramgrid;
-
-/**
- * This strategy enables the random search in hyper-parameter space.
- */
-public class RandomStrategy extends HyperParameterTuningStrategy {
- /** Satisfactory fitness to stop the hyperparameter search. */
- private double satisfactoryFitness = 0.5;
-
- /** Max tries to stop the hyperparameter search. */
- private int maxTries = 100;
-
- /** Seed. */
- private long seed = 1234L;
-
- /** Returns the seed. */
- public long getSeed() {
- return seed;
- }
-
- /**
- * Set up the seed number.
- *
- * @param seed Seed.
- */
- public RandomStrategy withSeed(long seed) {
- this.seed = seed;
- return this;
- }
-
- /**
- *
- */
- public double getSatisfactoryFitness() {
- return satisfactoryFitness;
- }
-
- /**
- * Set up the satisfactory fitness to stop the hyperparameter search.
- *
- * @param fitness Fitness.
- */
- public RandomStrategy withSatisfactoryFitness(double fitness) {
- satisfactoryFitness = fitness;
- return this;
- }
-
- /** Returns the max number of tries to stop the hyperparameter search. */
- public int getMaxTries() {
- return maxTries;
- }
-
- /**
- * Set up the max number of tries to stop the hyperparameter search.
- *
- * @param maxTries Max tries.
- */
- public RandomStrategy withMaxTries(int maxTries) {
- this.maxTries = maxTries;
- return this;
- }
-
- /** {@inheritDoc} */
- @Override public String getName() {
- return "Random Search";
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java
index 32a24d8..818a84e 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java
@@ -58,7 +58,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> {
public CacheBasedLabelPairCursor(IgniteCache<K, V> upstreamCache, IgniteBiPredicate<K, V> filter,
Preprocessor<K, V> preprocessor,
IgniteModel<Vector, L> mdl) {
- cursor = query(upstreamCache, filter);
+ this.cursor = query(upstreamCache, filter);
this.preprocessor = preprocessor;
this.mdl = mdl;
}
@@ -73,7 +73,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> {
public CacheBasedLabelPairCursor(IgniteCache<K, V> upstreamCache,
Preprocessor<K, V> preprocessor,
IgniteModel<Vector, L> mdl) {
- cursor = query(upstreamCache);
+ this.cursor = query(upstreamCache);
this.preprocessor = preprocessor;
this.mdl = mdl;
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java
index f0be6ca..a9221ae 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java
@@ -97,7 +97,7 @@ public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> {
@Override public boolean hasNext() {
if (filter == null) {
Map.Entry<K, V> entry = iter.next();
- nextEntry = entry;
+ this.nextEntry = entry;
return iter.hasNext();
}
@@ -130,7 +130,7 @@ public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> {
Map.Entry<K, V> entry = iter.next();
if (filter.apply(entry.getKey(), entry.getValue())) {
- nextEntry = entry;
+ this.nextEntry = entry;
break;
}
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/MetricValues.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/MetricValues.java
index c942d96..7241ee1 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/MetricValues.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/MetricValues.java
@@ -28,14 +28,14 @@ public interface MetricValues {
/** Returns the pair of metric name and metric value. */
public default Map<String, Double> toMap() {
Map<String, Double> metricValues = new HashMap<>();
- Class<? extends MetricValues> aCls = getClass();
- for (Field field : aCls.getDeclaredFields()) {
+ Class<? extends MetricValues> aClass = getClass();
+ for (Field field : aClass.getDeclaredFields()) {
try {
field.setAccessible(true);
metricValues.put(field.getName(), field.getDouble(this));
}
catch (IllegalAccessException e) {
- throw new RuntimeException("Cannot read field [class=" + aCls.getSimpleName() + ", field_name=" + field.getName() + "]", e);
+ throw new RuntimeException("Cannot read field [class=" + aClass.getSimpleName() + ", field_name=" + field.getName() + "]", e);
}
}
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/classification/BinaryClassificationMetrics.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/classification/BinaryClassificationMetrics.java
index b941613..e1966bd 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/classification/BinaryClassificationMetrics.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/classification/BinaryClassificationMetrics.java
@@ -42,6 +42,7 @@ public class BinaryClassificationMetrics extends AbstractMetrics<BinaryClassific
{
metric = BinaryClassificationMetricValues::accuracy;
}
+
/**
* Calculates binary metrics values.
*
@@ -121,7 +122,7 @@ public class BinaryClassificationMetrics extends AbstractMetrics<BinaryClassific
/** */
public BinaryClassificationMetrics withEnablingROCAUC(boolean enableROCAUC) {
- this.enableROCAUC = enableROCAUC;
+ this.enableROCAUC = this.enableROCAUC;
return this;
}
@@ -130,6 +131,7 @@ public class BinaryClassificationMetrics extends AbstractMetrics<BinaryClassific
return enableROCAUC;
}
+
/** {@inheritDoc} */
@Override public String name() {
return "Binary classification metrics";
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/regression/RegressionMetrics.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/regression/RegressionMetrics.java
index 1e80cee..a0eaedf 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/regression/RegressionMetrics.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/metric/regression/RegressionMetrics.java
@@ -66,8 +66,14 @@ public class RegressionMetrics extends AbstractMetrics<RegressionMetricValues> {
double tss = totalAmount * (meanOfLblSquares - Math.pow(meanOfLbls, 2));
double r2 = 0.0;
- if (Math.abs(tss) < EPS) r2 = Math.abs(rss) < EPS ? 1.0 : 0.0;
- else r2 = 1 - rss / tss;
+ if (Math.abs(tss) < EPS) {
+ if (Math.abs(rss) < EPS)
+ r2 = 1.0;
+ else
+ r2 = 0.0;
+ } else {
+ r2 = 1 - rss / tss;
+ }
mae /= totalAmount;
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/tree/DecisionTreeClassificationTrainer.java b/modules/ml/src/main/java/org/apache/ignite/ml/tree/DecisionTreeClassificationTrainer.java
index 688fd50..321e65f 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/tree/DecisionTreeClassificationTrainer.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/tree/DecisionTreeClassificationTrainer.java
@@ -96,24 +96,6 @@ public class DecisionTreeClassificationTrainer extends DecisionTree<GiniImpurity
return this;
}
- /**
- * Returns a hyper-parameter value.
- *
- * @return The max deep of tree.
- */
- public double getMaxDeep() {
- return maxDeep;
- }
-
- /**
- * Returns a hyper-parameter value.
- *
- * @return The min impurity decrease.
- */
- public double getMinImpurityDecrease() {
- return minImpurityDecrease;
- }
-
/** {@inheritDoc} */
@Override protected ImpurityMeasureCalculator<GiniImpurityMeasure> getImpurityMeasureCalculator(
Dataset<EmptyContext, DecisionTreeData> dataset) {
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Chromosome.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Chromosome.java
deleted file mode 100644
index 778885f..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Chromosome.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-import org.jetbrains.annotations.NotNull;
-
-/**
- * Representes the set of genes, known as chromosome in genetic programming.
- *
- * Note: this class has a natural ordering that is inconsistent with equals.
- */
-public class Chromosome implements Comparable {
- /** Genes. */
- private Double[] genes;
-
- /** Fitness. */
- private Double fitness = Double.NaN;
-
- /**
- * @param size Size.
- */
- public Chromosome(int size) {
- this.genes = new Double[size];
- }
-
- /**
- * @param doubles Doubles.
- */
- public Chromosome(Double[] doubles) {
- genes = doubles.clone();
- }
-
- /**
- * Returns the double array chromosome representation.
- */
- public Double[] toDoubleArray() {
- return genes;
- }
-
- /**
- * Returns the fitness value.
- */
- public Double getFitness() {
- return fitness;
- }
-
- /**
- * Sets the fitness value.
- *
- * @param fitness Fitness.
- */
- public void setFitness(Double fitness) {
- this.fitness = fitness;
- }
-
- /**
- * Returns the amount of genes in chromosome.
- */
- public int size() {
- return genes.length;
- }
-
- /**
- * Returns the gene value by index.
- *
- * @param idx Index.
- */
- public double getGene(int idx) {
- return genes[idx];
- }
-
- /**
- * Sets gene value by index.
- *
- * @param idx Index.
- * @param geneValue Gene value.
- */
- public void setGene(int idx, double geneValue) {
- genes[idx] = geneValue;
- }
-
- /**
- * Creates chromosome copy.
- */
- public Chromosome copy() {
- Chromosome cp = new Chromosome(genes);
- cp.fitness = fitness;
- return cp;
- }
-
- /** {@inheritDoc} */
- @Override public int compareTo(@NotNull Object o) {
- return (int) Math.signum(getFitness() - ((Chromosome) o).getFitness());
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/CrossoverStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/CrossoverStrategy.java
deleted file mode 100644
index a925646..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/CrossoverStrategy.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-/**
- * Represents the crossover strategy depending of locus point amount.
- */
-public enum CrossoverStrategy {
- /** One point. */
- ONE_POINT,
-
- /** Two point. */
- TWO_POINT,
-
- /** Multi point. */
- MULTI_POINT,
-
- /** Uniform. */
- UNIFORM
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithm.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithm.java
deleted file mode 100644
index bd351a5..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithm.java
+++ /dev/null
@@ -1,451 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Random;
-import java.util.function.BiFunction;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-import org.apache.commons.math3.util.Pair;
-import org.apache.ignite.ml.environment.LearningEnvironment;
-import org.apache.ignite.ml.environment.parallelism.Promise;
-import org.apache.ignite.ml.math.functions.IgniteSupplier;
-
-/**
- * This class is an entry point to use Genetic Algorithm to solve optimization problem.
- */
-public class GeneticAlgorithm {
- /** Uniform rate. */
- private static final double UNIFORM_RATE = 0.5;
-
- /** Population size. */
- private int populationSize;
-
- /** Amount of elite chromosomes. */
- private int amountOfEliteChromosomes = 2;
-
- /** Amount of generations. */
- private int amountOfGenerations = 10;
-
- /** Seed. */
- private long seed = 123L;
-
- /** Crossingover probability. */
- private double crossingoverProbability = 0.9;
-
- /** Mutation probability. */
- private double mutationProbability = 0.1;
-
- /** Random generator. */
- private Random rnd = new Random(seed);
-
- /** Population. */
- private Population population;
-
- /** Fitness function. */
- private Function<Chromosome, Double> fitnessFunction;
-
- /** Mutation operator. */
- private BiFunction<Integer, Double, Double> mutationOperator;
-
- /** Crossover strategy. */
- private CrossoverStrategy crossoverStgy = CrossoverStrategy.UNIFORM;
-
- /** Selection strategy. */
- private SelectionStrategy selectionStgy = SelectionStrategy.ROULETTE_WHEEL;
-
- /**
- * @param paramSet The list of sets of parameter's values.
- */
- public GeneticAlgorithm(List<Double[]> paramSet) {
- initializePopulation(paramSet);
- }
-
- /**
- * Forms the initial population.
- *
- * @param rawDataForPopulationFormation Rnd parameter sets.
- */
- private Population initializePopulation(List<Double[]> rawDataForPopulationFormation) {
- // validate that population size should be even
- // elite chromosome should be even too or we should handle odd case especially
- populationSize = rawDataForPopulationFormation.size();
- population = new Population(populationSize);
- for (int i = 0; i < populationSize; i++)
- population.setChromosome(i, new Chromosome(rawDataForPopulationFormation.get(i)));
-
- return population;
- }
-
- /**
- * The main method for genetic algorithm.
- */
- public void run() {
- if (population != null) {
- population.calculateFitnessForAll(fitnessFunction);
- int i = 0;
- while (stopCriteriaIsReached(i)) {
- Population newPopulation = new Population(populationSize);
-
- newPopulation = selectEliteChromosomes(newPopulation);
-
- Population parents = selectionParents();
-
- newPopulation = crossingover(parents, newPopulation);
-
- newPopulation = mutate(newPopulation);
-
- // update fitness for new population
- for (int j = amountOfEliteChromosomes; j < populationSize; j++)
- newPopulation.calculateFitnessForChromosome(j, fitnessFunction);
-
- population = newPopulation;
-
- i++;
- }
- }
- }
-
- /**
- * The main method for genetic algorithm.
- *
- * @param environment The environment.
- */
- public void runParallel(LearningEnvironment environment) {
- if (population != null) {
- population.calculateFitnessForAll(fitnessFunction); // TODO: parallelize this
- int i = 0;
- while (stopCriteriaIsReached(i)) {
- Population newPopulation = new Population(populationSize);
-
- newPopulation = selectEliteChromosomes(newPopulation);
-
- Population parents = selectionParents();
-
- newPopulation = crossingover(parents, newPopulation);
-
- newPopulation = mutate(newPopulation);
-
- // update fitness for new population
- List<IgniteSupplier<Pair<Integer, Double>>> tasks = new ArrayList<>();
- for (int j = amountOfEliteChromosomes; j < populationSize; j++) {
- int finalJ = j;
- Population finalNewPopulation1 = newPopulation;
- IgniteSupplier<Pair<Integer, Double>> task = () -> new Pair<>(finalJ, fitnessFunction.apply(finalNewPopulation1.getChromosome(finalJ)));
- tasks.add(task);
- }
-
- List<Pair<Integer, Double>> taskResults = environment.parallelismStrategy().submit(tasks).stream()
- .map(Promise::unsafeGet)
- .collect(Collectors.toList());
-
- Population finalNewPopulation = newPopulation;
- taskResults.forEach(p -> finalNewPopulation.setFitness(p.getKey(), p.getValue()));
-
- population = newPopulation;
-
- i++;
- }
- }
- }
-
- /**
- * The common method of parent population building with different selection strategies.
- */
- private Population selectionParents() {
- switch (selectionStgy) {
- case ROULETTE_WHEEL:
- return selectParentsByRouletteWheel();
- default:
- throw new UnsupportedOperationException("This strategy "
- + selectionStgy.name() + " is not supported yet.");
- }
- }
-
- /**
- * Form the parent population via wheel-roulette algorithm.
- * For more information, please have a look http://www.edc.ncl.ac.uk/highlight/rhjanuary2007g02.php/.
- */
- private Population selectParentsByRouletteWheel() {
- double totalFitness = population.getTotalFitness();
- double[] sectors = new double[population.size()];
-
- for (int i = 0; i < population.size(); i++)
- sectors[i] = population.getChromosome(i).getFitness() / totalFitness;
-
- Population parentPopulation = new Population(population.size());
-
- for (int i = 0; i < parentPopulation.size(); i++) {
- double rouletteVal = rnd.nextDouble();
- double accumulatedSectorLen = 0.0;
- int selectedChromosomeIdx = Integer.MIN_VALUE;
- int sectorIdx = 0;
-
- while (selectedChromosomeIdx == Integer.MIN_VALUE && sectorIdx < sectors.length) {
- accumulatedSectorLen += sectors[sectorIdx];
- if (rouletteVal < accumulatedSectorLen)
- selectedChromosomeIdx = sectorIdx;
- sectorIdx++;
- }
-
- if (selectedChromosomeIdx == Integer.MIN_VALUE)
- selectedChromosomeIdx = rnd.nextInt(population.size()); // or get last
-
- parentPopulation.setChromosome(i, population.getChromosome(selectedChromosomeIdx));
- }
-
- return parentPopulation;
- }
-
- /**
- * Simple stop criteria condition based on max amount of generations.
- *
- * @param iterationNum Iteration number.
- */
- private boolean stopCriteriaIsReached(int iterationNum) {
- return iterationNum < amountOfGenerations;
- }
-
- /**
- * Selects and injects the best chromosomes to form the elite of new population.
- *
- * @param newPopulation New population.
- */
- private Population selectEliteChromosomes(Population newPopulation) {
- boolean elitism = amountOfEliteChromosomes > 0;
-
- if (elitism) {
- Chromosome[] elite = population.selectBestKChromosome(amountOfEliteChromosomes);
- for (int i = 0; i < elite.length; i++)
- newPopulation.setChromosome(i, elite[i]);
- }
- return newPopulation;
- }
-
- /**
- * Forms the new population based on parent population with crossingover operator.
- *
- * @param parents Parents.
- * @param newPopulation New population.
- */
- private Population crossingover(Population parents, Population newPopulation) {
- // because parent population is less than new population on amount of elite chromosome
- for (int j = 0; j < populationSize - amountOfEliteChromosomes; j += 2) {
- Chromosome ch1 = parents.getChromosome(j);
- Chromosome ch2 = parents.getChromosome(j + 1);
-
- if (rnd.nextDouble() < crossingoverProbability) {
- List<Chromosome> twoChildren = crossover(ch1, ch2);
- newPopulation.setChromosome(amountOfEliteChromosomes + j, twoChildren.get(0));
- newPopulation.setChromosome(amountOfEliteChromosomes + j + 1, twoChildren.get(1));
- } else {
- newPopulation.setChromosome(amountOfEliteChromosomes + j, ch1);
- newPopulation.setChromosome(amountOfEliteChromosomes + j + 1, ch2);
- }
- }
- return newPopulation;
- }
-
-
- /**
- * Applies mutation operator to each chromosome in population with mutation probability.
- *
- * @param newPopulation New population.
- */
- private Population mutate(Population newPopulation) {
- for (int j = amountOfEliteChromosomes; j < populationSize; j++) {
- Chromosome possibleMutant = newPopulation.getChromosome(j);
- for (int geneIdx = 0; geneIdx < possibleMutant.size(); geneIdx++) {
- if (rnd.nextDouble() < mutationProbability) {
- Double gene = possibleMutant.getGene(geneIdx);
- Double newGeneVal = mutationOperator.apply(geneIdx, gene);
- possibleMutant.setGene(geneIdx, newGeneVal);
- }
- }
- }
- return newPopulation;
- }
-
- /**
- * Produce the two child chromsome from two parent chromosome according crossover strategy.
- *
- * @param firstParent First parent.
- * @param secondParent Second parent.
- */
- private List<Chromosome> crossover(Chromosome firstParent, Chromosome secondParent) {
- if (firstParent.size() != secondParent.size())
- throw new RuntimeException("Different length of hyper-parameter vectors!");
- switch (crossoverStgy) {
- case UNIFORM:
- return uniformStrategy(firstParent, secondParent);
- case ONE_POINT:
- return onePointStrategy(firstParent, secondParent);
- default:
- throw new UnsupportedOperationException("This strategy "
- + crossoverStgy.name() + " is not supported yet.");
-
- }
-
- }
-
- /**
- * Produce the two child chromsome from two parent chromosome according one-point strategy.
- *
- * @param firstParent First parent.
- * @param secondParent Second parent.
- */
- private List<Chromosome> onePointStrategy(Chromosome firstParent, Chromosome secondParent) {
- int size = firstParent.size();
-
- Chromosome child1 = new Chromosome(size);
- Chromosome child2 = new Chromosome(size);
- int locusPnt = rnd.nextInt(size);
-
- for (int i = 0; i < locusPnt; i++) {
- child1.setGene(i, firstParent.getGene(i));
- child2.setGene(i, secondParent.getGene(i));
- }
-
-
- for (int i = locusPnt; i < size; i++) {
- child1.setGene(i, secondParent.getGene(i));
- child2.setGene(i, firstParent.getGene(i));
- }
-
- List<Chromosome> res = new ArrayList<>();
- res.add(child1);
- res.add(child2);
-
- return res;
- }
-
- /**
- * Produce the two child chromsome from two parent chromosome according uniform strategy.
- *
- * @param firstParent First parent.
- * @param secondParent Second parent.
- */
- private List<Chromosome> uniformStrategy(Chromosome firstParent, Chromosome secondParent) {
- int size = firstParent.size();
-
- Chromosome child1 = new Chromosome(size);
- Chromosome child2 = new Chromosome(size);
-
- for (int i = 0; i < firstParent.size(); i++) {
- if (rnd.nextDouble() < UNIFORM_RATE) {
- child1.setGene(i, firstParent.getGene(i));
- child2.setGene(i, secondParent.getGene(i));
- } else {
- child1.setGene(i, secondParent.getGene(i));
- child2.setGene(i, firstParent.getGene(i));
- }
- }
-
- List<Chromosome> res = new ArrayList<>();
- res.add(child1);
- res.add(child2);
-
- return res;
- }
-
- /**
- * Sets the custom fitness function.
- *
- * @param fitnessFunction Fitness function.
- */
- public GeneticAlgorithm withFitnessFunction(Function<Chromosome, Double> fitnessFunction) {
- this.fitnessFunction = fitnessFunction;
- return this;
- }
-
- /**
- * Returns the best chromosome's genes presented as double array.
- */
- public double[] getTheBestSolution() {
- Double[] boxed = population.selectBestKChromosome(1)[0].toDoubleArray();
- return Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();
- }
-
- /**
- * @param amountOfEliteChromosomes Amount of elite chromosomes.
- */
- public GeneticAlgorithm withAmountOfEliteChromosomes(int amountOfEliteChromosomes) {
- this.amountOfEliteChromosomes = amountOfEliteChromosomes;
- return this;
- }
-
- /**
- * @param amountOfGenerations Amount of generations.
- */
- public GeneticAlgorithm withAmountOfGenerations(int amountOfGenerations) {
- this.amountOfGenerations = amountOfGenerations;
- return this;
- }
-
- /**
- * @param mutationOperator Mutation operator.
- */
- public GeneticAlgorithm withMutationOperator(
- BiFunction<Integer, Double, Double> mutationOperator) {
- this.mutationOperator = mutationOperator;
- return this;
- }
-
- /**
- * @param populationSize Population size.
- */
- public GeneticAlgorithm withPopulationSize(int populationSize) {
- this.populationSize = populationSize;
- return this;
- }
-
- /**
- * @param crossingoverProbability Crossingover probability.
- */
- public GeneticAlgorithm withCrossingoverProbability(double crossingoverProbability) {
- this.crossingoverProbability = crossingoverProbability;
- return this;
- }
-
- /**
- * @param mutationProbability Mutation probability.
- */
- public GeneticAlgorithm withMutationProbability(double mutationProbability) {
- this.mutationProbability = mutationProbability;
- return this;
- }
-
- /**
- * @param crossoverStgy Crossover strategy.
- */
- public GeneticAlgorithm withCrossoverStgy(CrossoverStrategy crossoverStgy) {
- this.crossoverStgy = crossoverStgy;
- return this;
- }
-
- /**
- * @param selectionStgy Selection strategy.
- */
- public GeneticAlgorithm withSelectionStgy(SelectionStrategy selectionStgy) {
- this.selectionStgy = selectionStgy;
- return this;
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Population.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Population.java
deleted file mode 100644
index 7f57b30..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/Population.java
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.function.Function;
-
-/**
- * Represents a populations of chromosomes.
- */
-public class Population {
- /** Chromosomes. */
- private Chromosome[] chromosomes;
-
- /** Fitness calculated flags. */
- private BitSet fitnessCalculatedFlags;
-
- /**
- * @param size Size.
- */
- public Population(int size) {
- chromosomes = new Chromosome[size];
- fitnessCalculatedFlags = new BitSet(size);
- }
-
- /**
- * Returns an individual chromosome.
- *
- * @param idx Index of chromosome.
- */
- public Chromosome getChromosome(int idx) {
- return chromosomes[idx];
- }
-
- /**
- * Calculates fitness for chromosome found by index with custom fitness function.
- *
- * @param idx Index.
- * @param fitnessFunction Fitness function.
- */
- public double calculateFitnessForChromosome(int idx, Function<Chromosome, Double> fitnessFunction) {
- double fitness = fitnessFunction.apply(chromosomes[idx]);
- chromosomes[idx].setFitness(fitness);
- fitnessCalculatedFlags.set(idx);
- return fitness;
- }
-
- /**
- * Calculates fintness for all chromosomes with custom fitness function.
- *
- * @param fitnessFunction Fitness function.
- */
- public void calculateFitnessForAll(Function<Chromosome, Double> fitnessFunction) {
- for (int i = 0; i < chromosomes.length; i++)
- calculateFitnessForChromosome(i, fitnessFunction);
- }
-
- /**
- * Sets the chromsome for given index.
- *
- * @param idx Index.
- * @param chromosome Chromosome.
- */
- public void setChromosome(int idx, Chromosome chromosome) {
- chromosomes[idx] = chromosome;
- if (!Double.isNaN(chromosome.getFitness())) fitnessCalculatedFlags.set(idx);
-
- }
-
- /**
- * Returns the chromosome by given index.
- *
- * @param idx Index.
- */
- public Chromosome getChromosome(Integer idx) {
- return chromosomes[idx];
- }
-
- /**
- * Selects the top K chromosomes by fitness value from the smallest to the largest.
- *
- * @param k The amount of top chromosome with highest value of the fitness.
- *
- * Returns null if not all fitnesses are calculated for all chromosomes.
- */
- public Chromosome[] selectBestKChromosome(int k) {
- if (fitnessCalculatedFlags.cardinality() == chromosomes.length) {
- Chromosome[] cp = Arrays.copyOf(chromosomes, chromosomes.length);
- Arrays.sort(cp);
- return Arrays.copyOfRange(cp, cp.length - k, cp.length);
- }
- return null;
- }
-
- /**
- * Returns the total fitness value of population or Double.NaN if not all fitnesses are calculated for all chromosomes.
- */
- public double getTotalFitness() {
- if (fitnessCalculatedFlags.cardinality() == chromosomes.length) {
- double totalFitness = 0.0;
-
- for (int i = 0; i < chromosomes.length; i++)
- totalFitness += chromosomes[i].getFitness();
-
- return totalFitness;
- }
- return Double.NaN;
- }
-
- /**
- * Returns the average fitness of population or Double.NaN if not all fitnesses are calculated for all chromosomes.
- */
- public double getAverageFitness() {
- if (fitnessCalculatedFlags.cardinality() == chromosomes.length) {
- double totalFitness = 0.0;
-
- for (int i = 0; i < chromosomes.length; i++)
- totalFitness += chromosomes[i].getFitness();
-
- return totalFitness / chromosomes.length;
- }
- return Double.NaN;
- }
-
-
- /**
- * Returns the size of population.
- */
- public int size() {
- return chromosomes.length;
- }
-
- /**
- * Sets the fitness value for chromosome with the given index.
- *
- * @param idx Index.
- * @param fitness Fitness.
- */
- public void setFitness(Integer idx, Double fitness) {
- chromosomes[idx].setFitness(fitness);
- fitnessCalculatedFlags.set(idx);
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/SelectionStrategy.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/SelectionStrategy.java
deleted file mode 100644
index 86fe9f0..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/SelectionStrategy.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-/**
- * Please, have a look at https://en.wikipedia.org/wiki/Selection_(genetic_algorithm).
- */
-public enum SelectionStrategy {
- /** Roulette wheel. */
- ROULETTE_WHEEL,
-
- /** Truncation. */
- TRUNCATION,
-
- /** Tournament. */
- TOURNAMENT
-}
\ No newline at end of file
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/package-info.java
deleted file mode 100644
index c10df20..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/util/genetic/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * 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 description. -->
- * Contains some genetic algorithms for discrete optimization task in ML module locally.
- */
-package org.apache.ignite.ml.util.genetic;
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/common/TrainerTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/common/TrainerTest.java
index 1079271..10ed9da 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/common/TrainerTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/common/TrainerTest.java
@@ -17,12 +17,13 @@
package org.apache.ignite.ml.common;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
/**
* Basic fields and methods for the trainer tests.
@@ -255,7 +256,7 @@ public class TrainerTest {
};
/** The data is easy separated with classifier by y = x. */
- public static final double[][] twoLinearlySeparableClasses = {
+ protected static final double[][] twoLinearlySeparableClasses = {
{0.0, -122.69914721554494, -152.90003228835155},
{1.0, -988.7803093110984, 39.64498230320555},
{1.0, -721.0342526056645, -167.29469954420483},
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainerTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainerTest.java
index 6fb760e..54e6aaa 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainerTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainerTest.java
@@ -43,14 +43,14 @@ public class EncoderTrainerTest extends TrainerTest {
@Test
public void testFitOnStringCategorialFeatures() {
Map<Integer, Vector> data = new HashMap<>();
- data.put(1, new DenseVector(new Serializable[]{1.0, "Monday", "September"}));
- data.put(2, new DenseVector(new Serializable[]{2.0, "Monday", "August"}));
- data.put(3, new DenseVector(new Serializable[]{3.0, "Monday", "August"}));
- data.put(4, new DenseVector(new Serializable[]{4.0, "Friday", "June"}));
- data.put(5, new DenseVector(new Serializable[]{5.0, "Friday", "June"}));
- data.put(6, new DenseVector(new Serializable[]{6.0, "Sunday", "August"}));
+ data.put(1, new DenseVector(new Serializable[]{"Monday", "September"}));
+ data.put(2, new DenseVector(new Serializable[]{"Monday", "August"}));
+ data.put(3, new DenseVector(new Serializable[]{"Monday", "August"}));
+ data.put(4, new DenseVector(new Serializable[]{"Friday", "June"}));
+ data.put(5, new DenseVector(new Serializable[]{"Friday", "June"}));
+ data.put(6, new DenseVector(new Serializable[]{"Sunday", "August"}));
- final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(1, 2).labeled(0);
+ final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0 , 1);
DatasetBuilder<Integer, Vector> datasetBuilder = new LocalDatasetBuilder<>(data, parts);
@@ -65,7 +65,7 @@ public class EncoderTrainerTest extends TrainerTest {
vectorizer
);
- assertArrayEquals(new double[]{0.0, 2.0}, preprocessor.apply(7, new DenseVector(new Serializable[]{7.0, "Monday", "September"})).features().asArray(), 1e-8);
+ assertArrayEquals(new double[]{0.0, 2.0}, preprocessor.apply(7, new DenseVector(new Serializable[]{"Monday", "September"})).features().asArray(), 1e-8);
}
/** Tests {@code fit()} method. */
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/selection/cv/CrossValidationTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/selection/cv/CrossValidationTest.java
index 9b45da8..b161e58 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/selection/cv/CrossValidationTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/selection/cv/CrossValidationTest.java
@@ -21,14 +21,6 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
import org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer;
-import org.apache.ignite.ml.nn.UpdatesStrategy;
-import org.apache.ignite.ml.optimization.updatecalculators.SimpleGDParameterUpdate;
-import org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator;
-import org.apache.ignite.ml.pipeline.Pipeline;
-import org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel;
-import org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer;
-import org.apache.ignite.ml.selection.paramgrid.ParamGrid;
-import org.apache.ignite.ml.selection.paramgrid.RandomStrategy;
import org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy;
import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetricValues;
import org.apache.ignite.ml.selection.scoring.metric.classification.BinaryClassificationMetrics;
@@ -36,8 +28,8 @@ import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
import org.apache.ignite.ml.tree.DecisionTreeNode;
import org.junit.Test;
-import static org.apache.ignite.ml.common.TrainerTest.twoLinearlySeparableClasses;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
/**
* Tests for {@link CrossValidation}.
@@ -53,23 +45,31 @@ public class CrossValidationTest {
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
- DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
- new DebugCrossValidation<>();
+ CrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
+ new CrossValidation<>();
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
int folds = 4;
- scoreCalculator
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(new Accuracy<>())
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(folds)
- .isRunningOnPipeline(false);
-
- verifyScores(folds, scoreCalculator.scoreByFolds());
+ verifyScores(folds, scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ data,
+ 1,
+ vectorizer,
+ folds
+ ));
+
+ verifyScores(folds, scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ data,
+ (e1, e2) -> true,
+ 1,
+ vectorizer,
+ folds
+ ));
}
/** */
@@ -84,223 +84,32 @@ public class CrossValidationTest {
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
- DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
- new DebugCrossValidation<>();
+ CrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
+ new CrossValidation<>();
int folds = 4;
BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
.withMetric(BinaryClassificationMetricValues::accuracy);
- scoreCalculator
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(metrics)
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(folds)
- .isRunningOnPipeline(false);
-
- verifyScores(folds, scoreCalculator.scoreByFolds());
- }
-
- /**
- *
- */
- @Test
- public void testBasicFunctionality() {
- Map<Integer, double[]> data = new HashMap<>();
-
- for (int i = 0; i < twoLinearlySeparableClasses.length; i++)
- data.put(i, twoLinearlySeparableClasses[i]);
-
- LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer()
- .withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2),
- SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG))
- .withMaxIterations(100000)
- .withLocIterations(100)
- .withBatchSize(14)
- .withSeed(123L);
-
- Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
-
- DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]> scoreCalculator =
- new DebugCrossValidation<>();
-
- int folds = 4;
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- scoreCalculator
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(metrics)
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(folds)
- .isRunningOnPipeline(false);
-
- double[] scores = scoreCalculator.scoreByFolds();
-
- assertEquals(0.8389830508474576, scores[0], 1e-6);
- assertEquals(0.9402985074626866, scores[1], 1e-6);
- assertEquals(0.8809523809523809, scores[2], 1e-6);
- assertEquals(0.9921259842519685, scores[3], 1e-6);
- }
-
- /**
- *
- */
- @Test
- public void testGridSearch() {
- Map<Integer, double[]> data = new HashMap<>();
-
- for (int i = 0; i < twoLinearlySeparableClasses.length; i++)
- data.put(i, twoLinearlySeparableClasses[i]);
-
- LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer()
- .withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2),
- SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG))
- .withMaxIterations(100000)
- .withLocIterations(100)
- .withBatchSize(14)
- .withSeed(123L);
-
- Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- ParamGrid paramGrid = new ParamGrid()
- .addHyperParam("maxIterations", trainer::withMaxIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("locIterations", trainer::withLocIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("batchSize", trainer::withBatchSize, new Double[]{1.0, 2.0, 4.0, 8.0, 16.0});
-
- DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]> scoreCalculator =
- (DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>) new DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>()
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(metrics)
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(4)
- .isRunningOnPipeline(false)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
-
- assertArrayEquals(crossValidationRes.getBestScore(), new double[]{0.9745762711864406, 1.0, 0.8968253968253969, 0.8661417322834646}, 1e-6);
- assertEquals(crossValidationRes.getBestAvgScore(), 0.9343858500738256, 1e-6);
- assertEquals(crossValidationRes.getScoringBoard().size(), 80);
- }
-
- /**
- *
- */
- @Test
- public void testRandomSearch() {
- Map<Integer, double[]> data = new HashMap<>();
-
- for (int i = 0; i < twoLinearlySeparableClasses.length; i++)
- data.put(i, twoLinearlySeparableClasses[i]);
-
- LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer()
- .withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2),
- SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG))
- .withMaxIterations(100000)
- .withLocIterations(100)
- .withBatchSize(14)
- .withSeed(123L);
-
- Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(
- new RandomStrategy()
- .withMaxTries(10)
- .withSeed(1234L)
- .withSatisfactoryFitness(0.9)
- )
- .addHyperParam("maxIterations", trainer::withMaxIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("locIterations", trainer::withLocIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("batchSize", trainer::withBatchSize, new Double[]{1.0, 2.0, 4.0, 8.0, 16.0});
-
- DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]> scoreCalculator =
- (DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>) new DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>()
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(metrics)
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(4)
- .isRunningOnPipeline(false)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- assertEquals(crossValidationRes.getBestAvgScore(), 0.9343858500738256, 1e-6);
- assertEquals(crossValidationRes.getScoringBoard().size(), 10);
- }
-
- /**
- *
- */
- @Test
- public void testRandomSearchWithPipeline() {
- Map<Integer, double[]> data = new HashMap<>();
-
- for (int i = 0; i < twoLinearlySeparableClasses.length; i++)
- data.put(i, twoLinearlySeparableClasses[i]);
-
- LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer()
- .withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2),
- SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG))
- .withMaxIterations(100000)
- .withLocIterations(100)
- .withBatchSize(14)
- .withSeed(123L);
-
- Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
-
- BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics()
- .withMetric(BinaryClassificationMetricValues::accuracy);
-
- ParamGrid paramGrid = new ParamGrid()
- .withParameterSearchStrategy(
- new RandomStrategy()
- .withMaxTries(10)
- .withSeed(1234L)
- .withSatisfactoryFitness(0.9)
- )
- .addHyperParam("maxIterations", trainer::withMaxIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("locIterations", trainer::withLocIterations, new Double[]{10.0, 100.0, 1000.0, 10000.0})
- .addHyperParam("batchSize", trainer::withBatchSize, new Double[]{1.0, 2.0, 4.0, 8.0, 16.0});
-
-
- Pipeline<Integer, double[], Integer, Double> pipeline = new Pipeline<Integer, double[], Integer, Double>()
- .addVectorizer(vectorizer)
- .addTrainer(trainer);
-
- DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]> scoreCalculator =
- (DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>) new DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]>()
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withPipeline(pipeline)
- .withMetric(metrics)
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(4)
- .isRunningOnPipeline(true)
- .withParamGrid(paramGrid);
-
- CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParamterers();
-
- assertEquals(crossValidationRes.getBestAvgScore(), 0.9343858500738256, 1e-6);
- assertEquals(crossValidationRes.getScoringBoard().size(), 10);
+ verifyScores(folds, scoreCalculator.score(
+ trainer,
+ metrics,
+ data,
+ 1,
+ vectorizer,
+ folds
+ ));
+
+ verifyScores(folds, scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ data,
+ (e1, e2) -> true,
+ 1,
+ vectorizer,
+ folds
+ ));
}
/** */
@@ -315,23 +124,19 @@ public class CrossValidationTest {
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
- DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
- new DebugCrossValidation<>();
+ CrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator =
+ new CrossValidation<>();
int folds = 4;
- scoreCalculator
- .withUpstreamMap(data)
- .withAmountOfParts(1)
- .withTrainer(trainer)
- .withMetric(new Accuracy<>())
-
- .withPreprocessor(vectorizer)
- .withAmountOfFolds(folds)
- .isRunningOnPipeline(false);
-
-
- double[] scores = scoreCalculator.scoreByFolds();
+ double[] scores = scoreCalculator.score(
+ trainer,
+ new Accuracy<>(),
+ data,
+ 1,
+ vectorizer,
+ folds
+ );
assertEquals(folds, scores.length);
@@ -347,7 +152,3 @@ public class CrossValidationTest {
assertEquals(1, scores[i], 1e-1);
}
}
-
-
-
-
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/util/UtilTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/util/UtilTestSuite.java
index 08dbf13..201ab5d 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/util/UtilTestSuite.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/util/UtilTestSuite.java
@@ -17,8 +17,6 @@
package org.apache.ignite.ml.util;
-import org.apache.ignite.ml.util.genetic.GeneticAlgorithmTest;
-import org.apache.ignite.ml.util.genetic.PopulationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@@ -27,9 +25,7 @@ import org.junit.runners.Suite;
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
- LRUCacheTest.class,
- GeneticAlgorithmTest.class,
- PopulationTest.class
+ LRUCacheTest.class
})
public class UtilTestSuite {
}
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithmTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithmTest.java
deleted file mode 100644
index 75324e0..0000000
--- a/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/GeneticAlgorithmTest.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Random;
-import java.util.function.BiFunction;
-import java.util.function.Function;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Tests for {@link GeneticAlgorithm}.
- */
-public class GeneticAlgorithmTest {
- /** Amount of genes in chromosome. */
- public static final int AMOUNT_OF_GENES_IN_CHROMOSOME = 8;
-
- /** Precision. */
- private static final double PRECISION = 0.00000001;
-
- /** Fitness function. */
- Function<Chromosome, Double> fitnessFunction = (Chromosome ch) -> {
- double fitness = 0;
- for (int i = 0; i < ch.size(); i += 2) fitness += ch.getGene(i);
- return fitness;
- };
-
- /** Random. */
- Random rnd = new Random(1234L);
-
- /** Genetic algorithm instance. */
- private GeneticAlgorithm ga;
-
- /**
- *
- */
- @Before
- public void setUp() {
- List<Double[]> rawData = new ArrayList<>();
- for (int i = 0; i < 100; i++) {
- Double[] chromosomeData = new Double[AMOUNT_OF_GENES_IN_CHROMOSOME];
- for (int j = 0; j < AMOUNT_OF_GENES_IN_CHROMOSOME; j++)
- chromosomeData[j] = rnd.nextDouble();
- rawData.add(i, chromosomeData);
- }
-
-
- ga = new GeneticAlgorithm(rawData);
- BiFunction<Integer, Double, Double> mutator = (integer, aDouble) -> rnd.nextDouble() > 0.5 ? aDouble + (rnd.nextDouble() / 100) : aDouble - (rnd.nextDouble() / 100);
-
- ga.withFitnessFunction(fitnessFunction)
- .withMutationOperator(mutator)
- .withAmountOfEliteChromosomes(10)
- .withCrossingoverProbability(0.01)
- .withCrossoverStgy(CrossoverStrategy.ONE_POINT)
- .withAmountOfGenerations(100)
- .withSelectionStgy(SelectionStrategy.ROULETTE_WHEEL)
- .withMutationProbability(0.05);
- }
-
- /**
- *
- */
- @Test
- public void runGeneticAlgorithm() {
- ga.run();
- double[] expBestSolution = {0.9227093559081438, 0.8716316379636383, 0.9393034992555963, 0.9264946442527818,
- 0.8030164650964057, 0.41871505180713764, 1.0294056830181408, 0.5760945730781087};
- Assert.assertArrayEquals(ga.getTheBestSolution(), expBestSolution, PRECISION);
- }
-
- /**
- *
- */
- @Test
- public void runParallelGeneticAlgorithm() {
- ga.run();
- double[] expBestSolution = {0.9227093559081438, 0.8716316379636383, 0.9393034992555963, 0.9264946442527818,
- 0.8030164650964057, 0.41871505180713764, 1.0294056830181408, 0.5760945730781087};
- Assert.assertArrayEquals(ga.getTheBestSolution(), expBestSolution, PRECISION);
- }
-}
\ No newline at end of file
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/PopulationTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/PopulationTest.java
deleted file mode 100644
index d5e20ce..0000000
--- a/modules/ml/src/test/java/org/apache/ignite/ml/util/genetic/PopulationTest.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * 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.ml.util.genetic;
-
-import java.util.function.Function;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Tests for {@link Population}.
- */
-public class PopulationTest {
- /** Amount of the best chromosomes. */
- public static final int K = 10;
-
- /** Test population. */
- Population population;
-
- /** Precision. */
- private static final double PRECISION = 0.00000001;
-
- /** Fitness function. */
- Function<Chromosome, Double> fitnessFunction = (Chromosome ch) -> ch.getGene(0) + ch.getGene(1);
-
- /**
- *
- */
- @Before
- public void setUp() {
- population = new Population(100);
- Double[] chromosomeData = new Double[2];
- for (int i = 0; i < population.size(); i++) {
- chromosomeData[0] = (double) i;
- chromosomeData[1] = (double) i;
- population.setChromosome(i, new Chromosome(chromosomeData));
- }
- }
-
- /**
- *
- */
- @Test
- public void calculateFitnessForChromosome() {
- population.calculateFitnessForChromosome(0, fitnessFunction);
- Assert.assertEquals(population.getChromosome(0).getFitness(), 0, PRECISION);
- }
-
- /**
- *
- */
- @Test
- public void calculateFitnessForAll() {
- population.calculateFitnessForAll(fitnessFunction);
- Assert.assertEquals(population.getChromosome(0).getFitness(), 0, PRECISION);
- }
-
- /**
- *
- */
- @Test
- public void selectBestKChromosomeWithoutFitnessCalculation() {
- Assert.assertNull(population.selectBestKChromosome(K));
- }
-
- /**
- *
- */
- @Test
- public void selectBestKChromosomeWithPartiallyFitnessCalculation() {
- population.calculateFitnessForChromosome(0, fitnessFunction);
- population.calculateFitnessForChromosome(1, fitnessFunction);
- Assert.assertNull(population.selectBestKChromosome(K));
- }
-
- /**
- *
- */
- @Test
- public void selectBestKChromosome() {
- population.calculateFitnessForAll(fitnessFunction);
- Chromosome[] res = population.selectBestKChromosome(K);
- Assert.assertEquals(res[0].getFitness(), 180, PRECISION);
- }
-
- /**
- *
- */
- @Test
- public void getTotalFitness() {
- double res = population.getTotalFitness();
- Assert.assertEquals(res, Double.NaN, PRECISION);
-
- population.calculateFitnessForAll(fitnessFunction);
- res = population.getTotalFitness();
- Assert.assertEquals(res, 9900.0, PRECISION);
- }
-
- /**
- *
- */
- @Test
- public void getAverageFitness() {
- double res = population.getAverageFitness();
- Assert.assertEquals(res, Double.NaN, PRECISION);
-
- population.calculateFitnessForAll(fitnessFunction);
- res = population.getAverageFitness();
- Assert.assertEquals(res, 99.0, PRECISION);
- }
-}
\ No newline at end of file