Search in sources :

Example 1 with GaussianNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer in project ignite by apache.

the class CompoundNaiveBayesExportImportExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Compound Naive Bayes classification model over partitioned dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        Path jsonMdlPath = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MIXED_DATASET);
            double[] priorProbabilities = new double[] { .5, .5 };
            double[][] thresholds = new double[][] { { .5 }, { .5 }, { .5 }, { .5 }, { .5 } };
            System.out.println("\n>>> Create new naive Bayes classification trainer object.");
            CompoundNaiveBayesTrainer trainer = new CompoundNaiveBayesTrainer().withPriorProbabilities(priorProbabilities).withGaussianNaiveBayesTrainer(new GaussianNaiveBayesTrainer()).withGaussianFeatureIdsToSkip(asList(3, 4, 5, 6, 7)).withDiscreteNaiveBayesTrainer(new DiscreteNaiveBayesTrainer().setBucketThresholds(thresholds)).withDiscreteFeatureIdsToSkip(asList(0, 1, 2));
            System.out.println("\n>>> Perform the training to get the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            CompoundNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println("\n>>> Exported Compound Naive Bayes model: " + mdl.toString(true));
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for exported Compound Naive Bayes model:" + accuracy);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            CompoundNaiveBayesModel modelImportedFromJSON = CompoundNaiveBayesModel.fromJSON(jsonMdlPath);
            System.out.println("\n>>> Imported Compound Naive Bayes model: " + modelImportedFromJSON.toString(true));
            accuracy = Evaluator.evaluate(dataCache, modelImportedFromJSON, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for imported Compound Naive Bayes model:" + accuracy);
            System.out.println("\n>>> Compound Naive Bayes model over partitioned 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) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) DiscreteNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer) CompoundNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesTrainer) CompoundNaiveBayesModel(org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesModel) GaussianNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 2 with GaussianNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer in project ignite by apache.

the class GaussianNaiveBayesExportImportExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Naive Bayes classification model over partitioned dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        Path jsonMdlPath = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
            System.out.println(">>> Create new Gaussian Naive Bayes classification trainer object.");
            GaussianNaiveBayesTrainer trainer = new GaussianNaiveBayesTrainer();
            System.out.println("\n>>> Perform the training to get the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            GaussianNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println("\n>>> Exported Gaussian Naive Bayes model: " + mdl.toString(true));
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for exported Gaussian Naive Bayes model:" + accuracy);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            GaussianNaiveBayesModel modelImportedFromJSON = GaussianNaiveBayesModel.fromJSON(jsonMdlPath);
            System.out.println("\n>>> Imported Gaussian Naive Bayes model: " + modelImportedFromJSON.toString(true));
            accuracy = Evaluator.evaluate(dataCache, modelImportedFromJSON, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for imported Gaussian Naive Bayes model:" + accuracy);
            System.out.println("\n>>> Gaussian Naive bayes model over partitioned 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) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) GaussianNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer) Ignite(org.apache.ignite.Ignite) GaussianNaiveBayesModel(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 3 with GaussianNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer in project ignite by apache.

the class CompoundNaiveBayesTest method testLearnsAndPredictCorrectly.

/**
 * Test.
 */
@Test
public void testLearnsAndPredictCorrectly() {
    CompoundNaiveBayesTrainer trainer = new CompoundNaiveBayesTrainer().withPriorProbabilities(classProbabilities).withGaussianNaiveBayesTrainer(new GaussianNaiveBayesTrainer()).withGaussianFeatureIdsToSkip(asList(3, 4, 5, 6, 7)).withDiscreteNaiveBayesTrainer(new DiscreteNaiveBayesTrainer().setBucketThresholds(binarizedDataThresholds)).withDiscreteFeatureIdsToSkip(asList(0, 1, 2));
    CompoundNaiveBayesModel mdl = trainer.fit(new LocalDatasetBuilder<>(data, 2), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    Vector observation1 = VectorUtils.of(5.92, 165, 10, 1, 1, 0, 0, 0);
    assertEquals(LABEL_1, mdl.predict(observation1), PRECISION);
    Vector observation2 = VectorUtils.of(6, 130, 8, 1, 0, 1, 1, 0);
    assertEquals(LABEL_2, mdl.predict(observation2), PRECISION);
}
Also used : DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GaussianNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer) DiscreteNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Example 4 with GaussianNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer in project ignite by apache.

the class CompoundNaiveBayesExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Compound Naive Bayes classification model over partitioned dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MIXED_DATASET);
        double[] priorProbabilities = new double[] { .5, .5 };
        double[][] thresholds = new double[][] { { .5 }, { .5 }, { .5 }, { .5 }, { .5 } };
        System.out.println(">>> Create new naive Bayes classification trainer object.");
        CompoundNaiveBayesTrainer trainer = new CompoundNaiveBayesTrainer().withPriorProbabilities(priorProbabilities).withGaussianNaiveBayesTrainer(new GaussianNaiveBayesTrainer()).withGaussianFeatureIdsToSkip(asList(3, 4, 5, 6, 7)).withDiscreteNaiveBayesTrainer(new DiscreteNaiveBayesTrainer().setBucketThresholds(thresholds)).withDiscreteFeatureIdsToSkip(asList(0, 1, 2));
        System.out.println(">>> Perform the training to get the model.");
        Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
        CompoundNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer);
        System.out.println(">>> Compound Naive Bayes model: " + mdl);
        double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
        System.out.println("\n>>> Accuracy " + accuracy);
        System.out.println(">>> Compound Naive bayes model over partitioned dataset usage example completed.");
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) DiscreteNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer) CompoundNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesTrainer) CompoundNaiveBayesModel(org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesModel) GaussianNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 5 with GaussianNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer in project ignite by apache.

the class GaussianNaiveBayesTrainerExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Naive Bayes classification model over partitioned dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
            System.out.println(">>> Create new naive Bayes classification trainer object.");
            GaussianNaiveBayesTrainer trainer = new GaussianNaiveBayesTrainer();
            System.out.println(">>> Perform the training to get the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            GaussianNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println(">>> Naive Bayes model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println(">>> Naive bayes model over partitioned dataset usage example completed.");
        } finally {
            dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) GaussianNaiveBayesTrainer(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer) Ignite(org.apache.ignite.Ignite) GaussianNaiveBayesModel(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Aggregations

Vector (org.apache.ignite.ml.math.primitives.vector.Vector)5 GaussianNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer)5 Ignite (org.apache.ignite.Ignite)4 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)4 DiscreteNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer)3 Path (java.nio.file.Path)2 CompoundNaiveBayesModel (org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesModel)2 CompoundNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.compound.CompoundNaiveBayesTrainer)2 GaussianNaiveBayesModel (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel)2 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)1 Test (org.junit.Test)1