Java examples for Machine Learning AI:weka
Use weka Vote
import weka.classifiers.Evaluation; import weka.classifiers.meta.GridSearch; import weka.classifiers.meta.Vote; import weka.core.Instances; import weka.core.Utils; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; public class Voting { public static void main(String[] args) throws Exception { Instances train = new Instances(new BufferedReader(new FileReader( "spambase_train.arff"))); Instances test = new Instances(new BufferedReader(new FileReader( "spambase_test.arff"))); train.setClassIndex(train.numAttributes() - 1); test.setClassIndex(test.numAttributes() - 1); Vote vs = new Vote(); GridSearch ps = new GridSearch(); vs.setOptions(weka.core.Utils/* w ww. j a v a2 s. c om*/ .splitOptions("-B \"weka.classifiers.functions.SMO -C 1 -L 0.01 -P 1E-10\" -B \"weka.classifiers.trees.NBTree\" -B \"weka.classifiers.trees.RandomForest -I 10 -K 20 -depth 5\" -R MAJ")); System.out.println(Utils.joinOptions(vs.getOptions())); vs.buildClassifier(train); PrintWriter pw = new PrintWriter(new FileWriter( "spambase-L5.txt")); for (int i = 0; i < test.numInstances(); i++) { double pred = vs.classifyInstance(test.instance(i)); pw.println(pred); } pw.close(); Evaluation eval = new Evaluation(train); eval.evaluateModel(vs, test); Double error_c = eval.errorRate(); System.out.println(error_c); } }