Search in sources :

Example 1 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class GDBOnTreesClassificationExportImportExample method main.

/**
 * Run example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> GDB classification trainer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println("\n>>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
        IgniteCache<Integer, double[]> trainingSet = null;
        Path jsonMdlPath = null;
        try {
            trainingSet = fillTrainingData(ignite, trainingSetCfg);
            // Create classification trainer.
            GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
            // Train decision tree model.
            GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
            System.out.println("\n>>> Exported GDB classification model: " + mdl.toString(true));
            predictOnGeneratedData(mdl);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            IgniteFunction<Double, Double> lbMapper = lb -> lb > 0.5 ? 1.0 : 0.0;
            GDBModel modelImportedFromJSON = GDBModel.fromJSON(jsonMdlPath).withLblMapping(lbMapper);
            System.out.println("\n>>> Imported GDB classification model: " + modelImportedFromJSON.toString(true));
            predictOnGeneratedData(modelImportedFromJSON);
            System.out.println(">>> GDB classification trainer example completed.");
        } finally {
            if (trainingSet != null)
                trainingSet.destroy();
            if (jsonMdlPath != null)
                Files.deleteIfExists(jsonMdlPath);
        }
    } finally {
        System.out.flush();
    }
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) GDBBinaryClassifierOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) NotNull(org.jetbrains.annotations.NotNull) Path(java.nio.file.Path) Vectorizer(org.apache.ignite.ml.dataset.feature.extractor.Vectorizer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) GDBBinaryClassifierOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) Ignite(org.apache.ignite.Ignite)

Example 2 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class ANNClassificationExportImportExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, double[]> dataCache = null;
        Path jsonMdlPath = null;
        try {
            dataCache = getTestCache(ignite);
            ANNClassificationTrainer trainer = new ANNClassificationTrainer().withDistance(new ManhattanDistance()).withK(50).withMaxIterations(1000).withEpsilon(1e-2);
            ANNClassificationModel mdl = (ANNClassificationModel) trainer.fit(ignite, dataCache, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST)).withK(5).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
            System.out.println("\n>>> Exported ANN model: " + mdl.toString(true));
            double accuracy = evaluateModel(dataCache, mdl);
            System.out.println("\n>>> Accuracy for exported ANN model:" + accuracy);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            ANNClassificationModel modelImportedFromJSON = ANNClassificationModel.fromJSON(jsonMdlPath);
            System.out.println("\n>>> Imported ANN model: " + modelImportedFromJSON.toString(true));
            accuracy = evaluateModel(dataCache, modelImportedFromJSON);
            System.out.println("\n>>> Accuracy for imported ANN model:" + accuracy);
            System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
            if (jsonMdlPath != null)
                Files.deleteIfExists(jsonMdlPath);
        }
    } finally {
        System.out.flush();
    }
}
Also used : Path(java.nio.file.Path) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) ANNClassificationModel(org.apache.ignite.ml.knn.ann.ANNClassificationModel) Ignite(org.apache.ignite.Ignite) ANNClassificationTrainer(org.apache.ignite.ml.knn.ann.ANNClassificationTrainer) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance)

Example 3 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class GDBOnTreesRegressionTrainerExample method main.

/**
 * Run example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String... args) {
    System.out.println();
    System.out.println(">>> GDB regression trainer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
        IgniteCache<Integer, double[]> trainingSet = null;
        try {
            trainingSet = fillTrainingData(ignite, trainingSetCfg);
            // Create regression trainer.
            GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001));
            // Train decision tree model.
            GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Valid answer \t|");
            System.out.println(">>> ---------------------------------");
            // Calculate score.
            for (int x = -5; x < 5; x++) {
                double predicted = mdl.predict(VectorUtils.of(x));
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.pow(x, 2));
            }
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> GDB regression trainer example completed.");
        } finally {
            trainingSet.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) Ignite(org.apache.ignite.Ignite)

Example 4 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class GDBOnTreesClassificationTrainerExample method main.

/**
 * Run example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String... args) {
    System.out.println();
    System.out.println(">>> GDB classification trainer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
        IgniteCache<Integer, double[]> trainingSet = null;
        try {
            trainingSet = fillTrainingData(ignite, trainingSetCfg);
            // Create classification trainer.
            GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
            // Train decision tree model.
            GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Valid answer\t|");
            System.out.println(">>> ---------------------------------");
            // Calculate score.
            for (int x = -5; x < 5; x++) {
                double predicted = mdl.predict(VectorUtils.of(x));
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x) < 0 ? 0.0 : 1.0);
            }
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> Count of trees = " + mdl.getModels().size());
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> GDB classification trainer example completed.");
        } finally {
            trainingSet.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) Ignite(org.apache.ignite.Ignite) GDBBinaryClassifierOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer)

Example 5 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class LocalModelsTest method getClusterModel.

/**
 */
private KMeansModel getClusterModel() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 1.0, 1959, 325100 });
    data.put(1, new double[] { 1.0, 1960, 373200 });
    KMeansTrainer trainer = new KMeansTrainer().withAmountOfClusters(1);
    return trainer.fit(new LocalDatasetBuilder<>(data, 2), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
}
Also used : DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KMeansTrainer(org.apache.ignite.ml.clustering.kmeans.KMeansTrainer)

Aggregations

DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)30 Test (org.junit.Test)23 HashMap (java.util.HashMap)17 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)14 TrainerTest (org.apache.ignite.ml.common.TrainerTest)11 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)10 Ignite (org.apache.ignite.Ignite)5 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 MeanAbsValueConvergenceCheckerFactory (org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory)5 KNNClassificationModel (org.apache.ignite.ml.knn.classification.KNNClassificationModel)5 KNNClassificationTrainer (org.apache.ignite.ml.knn.classification.KNNClassificationTrainer)5 GDBModel (org.apache.ignite.ml.composition.boosting.GDBModel)4 GDBTrainer (org.apache.ignite.ml.composition.boosting.GDBTrainer)4 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)4 SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)4 Path (java.nio.file.Path)3 Random (java.util.Random)3 KNNRegressionModel (org.apache.ignite.ml.knn.regression.KNNRegressionModel)3 KNNRegressionTrainer (org.apache.ignite.ml.knn.regression.KNNRegressionTrainer)3