Java tutorial
/* * Copyright (C) 2014 roelen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package nl.bioinf.roelen.thema11.classifier_tools; import java.util.logging.Level; import java.util.logging.Logger; import weka.classifiers.Classifier; import weka.classifiers.rules.OneR; import weka.classifiers.rules.ZeroR; import weka.classifiers.trees.J48; import weka.core.Instances; import weka.core.converters.ConverterUtils; /** * class to write classifiers * @author roelen */ public class BoundaryClassifier { /** * method to build a classifier * @param fileLocation the arrf file our attributes are in * @param method the method to use for building our classifier * @return the classifier object that was built */ public static Classifier build(String fileLocation, String method) { //init classifier object Classifier classifier; classifier = null; try { //get data ConverterUtils.DataSource source = new ConverterUtils.DataSource(fileLocation); //SET DATA AND OPTIONS Instances data = source.getDataSet(); //remove the name and position entries, these are not important for classifying data.deleteAttributeAt(data.numAttributes() - 2); data.deleteAttributeAt(data.numAttributes() - 2); data.setClassIndex(data.numAttributes() - 1); //prepare data for classifying String[] options = new String[1]; //unpruned options[0] = "-U"; // unpruned tree //see what method was given switch (method.toUpperCase()) { case "J48": //Build J48 classifier classifier = new J48(); // new instance of tree break; case "OneR": //Build OneR classifier classifier = new OneR(); break; case "ZeroR": //build (useless) ZeroR classifier classifier = new ZeroR(); break; default: //default is building OneR classifier = new OneR(); break; } //set the options and build that thing classifier.setOptions(options); // set the options classifier.buildClassifier(data); // build classifier } catch (Exception ex) { Logger.getLogger(BoundaryClassifier.class.getName()).log(Level.SEVERE, null, ex); } return classifier; } }