use weka classifiers trees LADTree with Filter - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

use weka classifiers trees LADTree with Filter

Demo Code

import weka.classifiers.functions.SMO;
import weka.classifiers.trees.LADTree;
import weka.core.Instances;
import weka.core.Utils;
import weka.filters.Filter;
import weka.filters.supervised.instance.Resample;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class LADTreewithFilter {

    public static void main(String[] args) throws Exception {

        // load data sets
        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);
        Resample filter = new Resample();
        filter.setInputFormat(train);//ww  w  . ja  v a  2s  .  com
        Instances newtrain = Filter.useFilter(train, filter);
        Instances newtest = Filter.useFilter(test, filter);
        newtrain.setClassIndex(newtrain.numAttributes() - 1);
        newtest.setClassIndex(newtest.numAttributes() - 1);
        //Classifier [] ClassifierArray=new Classifier[3];
        //ClassifierArray[1]=new J48();
        //ClassifierArray[0]=new NaiveBayes();
        //ClassifierArray[2]=new NBTree();
        LADTree vs = new LADTree();

        //find optimal parameter

        vs.setOptions(weka.core.Utils.splitOptions("-B 50"));
        //String[] options=new String[3];
        //options[2]="-R MAJ";
        //options[1]="-B weka.classifiers.functions.SMO -B weka.classifiers.bayes.NaiveBayes";
        //options[0]="-S <2>";
        //vs.setOptions(options);

        //vs.setClassifiers(ClassifierArray);

        vs.buildClassifier(newtrain);
        System.out.println(Utils.joinOptions(vs.getOptions()));
        //find optimal parameter
        //ps.addCVParameter("F 1 5 5");
        //ps.addCVParameter("S 1 10 10");

        //Dagging cls = new Dagging();
        //change the base classifier
        //cls.setClassifier(new NBTree());
        //change the parameter for dagging
        //cls.setNumFolds(1);
        //cls.setSeed(7);
        //cls.buildClassifier(train);
        //System.out.println(vs.getCombinationRule());
        //System.out.println(vs.getOptions());
        PrintWriter pw = new PrintWriter(new FileWriter(
                "spambase-L5.txt"));

        //System.out.println(Utils.joinOptions(ps.getBestClassifierOptions()));
        for (int i = 0; i < newtest.numInstances(); i++) {
            double pred = vs.classifyInstance(newtest.instance(i));
            pw.println(pred);
        }
        pw.close();
        //weka.core.SerializationHelper.write("/ionosphere.model", vs);

    }
}

Related Tutorials