Java examples for Machine Learning AI:weka
make prediction by weka naive bayes classifier
import weka.classifiers.bayes.NaiveBayes; import weka.core.Instance; import weka.core.Instances; import weka.core.converters.ConverterUtils.DataSource; public class CopyOfClassificationPrediction { public static void main(String[] args) throws Exception { DataSource source = new DataSource("iris.arff"); Instances traindata = source.getDataSet(); traindata.setClassIndex(traindata.numAttributes() - 1); int numClasses = traindata.numClasses(); for (int i = 0; i < numClasses; i++) { String classValue = traindata.classAttribute().value(i); System.out.println("the " + i + "th class value:" + classValue); }/*w w w .j av a 2s . c o m*/ /** * naive bayes classifier */ NaiveBayes nb = new NaiveBayes(); nb.buildClassifier(traindata); /** * load test data */ DataSource source2 = new DataSource("iris-unknown.arff"); Instances testdata = source2.getDataSet(); testdata.setClassIndex(testdata.numAttributes() - 1); /** * make prediction by naive bayes classifier */ for (int j = 0; j < testdata.numInstances(); j++) { double actualClass = testdata.instance(j).classValue(); String actual = testdata.classAttribute().value( (int) actualClass); Instance newInst = testdata.instance(j); double preNB = nb.classifyInstance(newInst); String predString = testdata.classAttribute() .value((int) preNB); System.out.println(actual + "," + predString); } } }