Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Learning; import java.util.Random; import weka.classifiers.Evaluation; import weka.classifiers.bayes.BayesNet; import weka.classifiers.meta.CVParameterSelection; import weka.classifiers.trees.J48; import weka.core.Instances; import weka.core.converters.ConverterUtils; import weka.filters.Filter; import weka.filters.unsupervised.attribute.NumericToNominal; /** * * @author jxy */ public class WekaWrapper { J48 tree; Evaluation eval; public J48 getTree() { return tree; } public Evaluation getEval() { return eval; } public double[] evaluate(String fn) throws Exception { ConverterUtils.DataSource source = new ConverterUtils.DataSource(fn); Instances data = source.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } NumericToNominal nmf = new NumericToNominal(); nmf.setInputFormat(data); data = Filter.useFilter(data, nmf); tree = new J48(); // new instance of tree String[] options = new String[1]; options[0] = "-C 0.25 -M 2"; tree.setOptions(options); tree.buildClassifier(data); // build classifier // eval eval = new Evaluation(data); eval.crossValidateModel(tree, data, 5, new Random(1)); // System.out.println("corr: " + eval.pctCorrect()); // System.out.println("inco: " + eval.pctIncorrect()); // System.out.println(eval.toSummaryString()); // System.out.println(eval.toMatrixString()); // System.out.println(eval.toClassDetailsString()); double[] results = new double[2]; results[0] = eval.pctCorrect(); results[1] = eval.pctIncorrect(); return results; } }