Java examples for Machine Learning AI:weka
weka Resampling Bagging
import java.io.FileWriter; import java.util.ArrayList; import weka.classifiers.Evaluation; import weka.classifiers.evaluation.Prediction; import weka.classifiers.meta.Bagging; import weka.classifiers.trees.RandomForest; import weka.core.Instances; import weka.core.converters.ConverterUtils.DataSource; import weka.filters.Filter; import weka.filters.supervised.instance.Resample; import au.com.bytecode.opencsv.CSVWriter; public class Main { public static void main(String[] args) throws Exception { /*from w w w .j a v a 2 s . c o m*/ Instances train = DataSource read("./train1.arff"); int cid1 = train.numAttributes() - 1; train.setClassIndex(cid1); Instances validation = DataSource read("./validation1.arff"); int cid2 = validation.numAttributes() - 1; validation.setClassIndex(cid2); Instances test = DataSource read("./test1.arff"); int cid3 = test.numAttributes() - 1; test.setClassIndex(cid3); //Resampling 17:83 Resample rs = new Resample(); rs.setBiasToUniformClass(0.3); rs.setInputFormat(train); rs.setSampleSizePercent(100); train = Filter.useFilter(train, rs); System.out.println(train.numInstances()); //BAGGING RF RandomForest rf = new RandomForest(); Bagging tree = new Bagging(); tree.setClassifier(rf); tree.buildClassifier(train); Evaluation eval = new Evaluation(train); eval.evaluateModel(tree, validation); System.out.println(eval.toSummaryString("\nResults_RF\n\n", false)); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); ArrayList<Prediction> al = eval.predictions(); ArrayList<String[]> as = new ArrayList<String[]>(al.size()); for (int i = 0; i < al.size(); i++) { String[] s = new String[1]; s[0] = al.get(i).toString(); s[0] = s[0].substring(9, 11); as.add(s); } ArrayList<String[]> li = new ArrayList<String[]>(al.size()); li.addAll(as); String csv = "./output.csv"; CSVWriter writer = new CSVWriter(new FileWriter(csv)); writer.writeAll(li); writer.close(); } }