Search in sources :

Example 1 with DiscreteNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer 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 DiscreteNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer 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 3 with DiscreteNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer 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 4 with DiscreteNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer in project ignite by apache.

the class DiscreteNaiveBayesTrainerExample method main.

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

Example 5 with DiscreteNaiveBayesTrainer

use of org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer in project ignite by apache.

the class DiscreteNaiveBayesExportImportExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    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")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        Path jsonMdlPath = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.ENGLISH_VS_SCOTTISH);
            double[][] thresholds = new double[][] { { .5 }, { .5 }, { .5 }, { .5 }, { .5 } };
            System.out.println(">>> Create new Discrete naive Bayes classification trainer object.");
            DiscreteNaiveBayesTrainer trainer = new DiscreteNaiveBayesTrainer().setBucketThresholds(thresholds);
            System.out.println("\n>>> Perform the training to get the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            DiscreteNaiveBayesModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println("\n>>> Exported Discrete Naive Bayes model: " + mdl.toString(true));
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for exported Discrete Naive Bayes model:" + accuracy);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            DiscreteNaiveBayesModel modelImportedFromJSON = DiscreteNaiveBayesModel.fromJSON(jsonMdlPath);
            System.out.println("\n>>> Imported Discrete Naive Bayes model: " + modelImportedFromJSON.toString(true));
            accuracy = Evaluator.evaluate(dataCache, modelImportedFromJSON, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy for imported Discrete Naive Bayes model:" + accuracy);
            System.out.println("\n>>> Discrete 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) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DiscreteNaiveBayesModel(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel)

Aggregations

Vector (org.apache.ignite.ml.math.primitives.vector.Vector)5 DiscreteNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer)5 Ignite (org.apache.ignite.Ignite)4 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)4 GaussianNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer)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 DiscreteNaiveBayesModel (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel)2 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)1 Test (org.junit.Test)1