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