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();
}
}
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);
}
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.");
}
}
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();
}
}
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();
}
}
Aggregations