Search in sources :

Example 1 with GaussianNaiveBayesModel

use of org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel 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 2 with GaussianNaiveBayesModel

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

the class CompoundNaiveBayesTrainer method updateModel.

/**
 * {@inheritDoc}
 */
@Override
protected <K, V> CompoundNaiveBayesModel updateModel(CompoundNaiveBayesModel mdl, DatasetBuilder<K, V> datasetBuilder, Preprocessor<K, V> extractor) {
    CompoundNaiveBayesModel compoundModel = new CompoundNaiveBayesModel().withPriorProbabilities(priorProbabilities);
    if (gaussianNaiveBayesTrainer != null) {
        if (priorProbabilities != null)
            gaussianNaiveBayesTrainer.setPriorProbabilities(priorProbabilities);
        GaussianNaiveBayesModel model = (mdl == null) ? gaussianNaiveBayesTrainer.fit(datasetBuilder, extractor.map(skipFeatures(gaussianFeatureIdsToSkip))) : gaussianNaiveBayesTrainer.update(mdl.getGaussianModel(), datasetBuilder, extractor.map(skipFeatures(gaussianFeatureIdsToSkip)));
        compoundModel.withGaussianModel(model).withGaussianFeatureIdsToSkip(gaussianFeatureIdsToSkip).withLabels(model.getLabels()).withPriorProbabilities(priorProbabilities);
    }
    if (discreteNaiveBayesTrainer != null) {
        if (priorProbabilities != null)
            discreteNaiveBayesTrainer.setPriorProbabilities(priorProbabilities);
        DiscreteNaiveBayesModel model = (mdl == null) ? discreteNaiveBayesTrainer.fit(datasetBuilder, extractor.map(skipFeatures(discreteFeatureIdsToSkip))) : discreteNaiveBayesTrainer.update(mdl.getDiscreteModel(), datasetBuilder, extractor.map(skipFeatures(discreteFeatureIdsToSkip)));
        compoundModel.withDiscreteModel(model).withDiscreteFeatureIdsToSkip(discreteFeatureIdsToSkip).withLabels(model.getLabels()).withPriorProbabilities(priorProbabilities);
    }
    return compoundModel;
}
Also used : GaussianNaiveBayesModel(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel) DiscreteNaiveBayesModel(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel)

Example 3 with GaussianNaiveBayesModel

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

the class CompoundNaiveBayesModelTest method testPredictOnlyGauss.

/**
 * Test.
 */
@Test
public void testPredictOnlyGauss() {
    GaussianNaiveBayesModel gaussianModel = new GaussianNaiveBayesModel(means, variances, classProbabilities, labels, null);
    Vector observation = VectorUtils.of(6, 130, 8);
    CompoundNaiveBayesModel model = new CompoundNaiveBayesModel().withPriorProbabilities(classProbabilities).withLabels(labels).withGaussianModel(gaussianModel);
    assertEquals(LABEL_2, model.predict(observation), PRECISION);
}
Also used : GaussianNaiveBayesModel(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Example 4 with GaussianNaiveBayesModel

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

the class CompoundNaiveBayesModelTest method testPredictGaussAndDiscrete.

/**
 * Test.
 */
@Test
public void testPredictGaussAndDiscrete() {
    DiscreteNaiveBayesModel discreteMdl = new DiscreteNaiveBayesModel(probabilities, classProbabilities, labels, binarizedDataThresholds, null);
    GaussianNaiveBayesModel gaussianMdl = new GaussianNaiveBayesModel(means, variances, classProbabilities, labels, null);
    CompoundNaiveBayesModel mdl = new CompoundNaiveBayesModel().withPriorProbabilities(classProbabilities).withLabels(labels).withGaussianModel(gaussianMdl).withGaussianFeatureIdsToSkip(asList(3, 4, 5, 6, 7)).withDiscreteModel(discreteMdl).withDiscreteFeatureIdsToSkip(asList(0, 1, 2));
    Vector observation = VectorUtils.of(6, 130, 8, 1, 0, 1, 1, 0);
    assertEquals(LABEL_2, mdl.predict(observation), PRECISION);
}
Also used : GaussianNaiveBayesModel(org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DiscreteNaiveBayesModel(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel) Test(org.junit.Test)

Example 5 with GaussianNaiveBayesModel

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

GaussianNaiveBayesModel (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel)5 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)4 Ignite (org.apache.ignite.Ignite)2 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)2 DiscreteNaiveBayesModel (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel)2 GaussianNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer)2 Test (org.junit.Test)2 Path (java.nio.file.Path)1