Search in sources :

Example 1 with DiscreteNaiveBayesModel

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

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

the class CompoundNaiveBayesModelTest method testPredictOnlyDiscrete.

/**
 * Test.
 */
@Test
public void testPredictOnlyDiscrete() {
    DiscreteNaiveBayesModel discreteModel = new DiscreteNaiveBayesModel(probabilities, classProbabilities, labels, binarizedDataThresholds, null);
    Vector observation = VectorUtils.of(1, 0, 1, 1, 0);
    CompoundNaiveBayesModel model = new CompoundNaiveBayesModel().withPriorProbabilities(classProbabilities).withLabels(labels).withDiscreteModel(discreteModel);
    assertEquals(LABEL_2, model.predict(observation), PRECISION);
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DiscreteNaiveBayesModel(org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel) Test(org.junit.Test)

Example 3 with DiscreteNaiveBayesModel

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

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

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

DiscreteNaiveBayesModel (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesModel)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 DiscreteNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer)2 GaussianNaiveBayesModel (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesModel)2 Test (org.junit.Test)2 Path (java.nio.file.Path)1