weka MetaCost Stacking J48 - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

weka MetaCost Stacking J48

Demo Code

import java.io.FileWriter;
import java.util.ArrayList;

import weka.classifiers.Classifier;
import weka.classifiers.CostMatrix;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.evaluation.Prediction;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.classifiers.meta.MetaCost;
import weka.classifiers.meta.Stacking;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import au.com.bytecode.opencsv.CSVWriter;

public class Main {

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

        Instances train = DataSource
        read("./train1.arff");
        int cid1 = train.numAttributes() - 1;
        train.setClassIndex(cid1);//ww  w. j av  a2 s  . c  om

        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);

        NaiveBayes NB = new NaiveBayes();
        J48 j48_tree = new J48();
        MultilayerPerceptron mp = new MultilayerPerceptron();

        Stacking btree = new Stacking();
        Classifier[] classifiers = new Classifier[2];
        classifiers[0] = j48_tree;
        classifiers[1] = mp;
        btree.setClassifiers(classifiers);
        btree.setMetaClassifier(NB);
        //btree.buildClassifier(train);

        //MetaCost
        CostMatrix cm = new CostMatrix(2);
        cm.setElement(0, 1, 1);
        cm.setElement(1, 0, 9);
        cm.setElement(0, 0, 0);
        cm.setElement(1, 1, 0);

        MetaCost tree = new MetaCost();
        tree.setClassifier(btree);
        tree.setCostMatrix(cm);
        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();
    }

}

Related Tutorials