Weka Filtered Classifier - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

Weka Filtered Classifier

Demo Code



import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.unsupervised.attribute.Remove;


public class WekaFilteredClassifier {
    public static void main(String[] args) throws Exception {
        DataSource source = new DataSource(
                "bank-train.arff");
        Instances instancesTrain = source.getDataSet(); 
                "bank-test.arff");
        Instances instancesTest = source.getDataSet(); 
        instancesTest.setClassIndex(instancesTest.numAttributes() - 1);

        Remove rm = new Remove();
        rm.setAttributeIndices("1"); 

        J48 j48 = new J48();
        j48.setUnpruned(true); /*  w w  w. j a  v a2s  .  com*/

        FilteredClassifier fc = new FilteredClassifier();
        fc.setFilter(rm);
        fc.setClassifier(j48);

        // train and make predictions
        fc.buildClassifier(instancesTrain);

        for (int i = 0; i < instancesTest.numInstances(); i++) {
            double pred = fc.classifyInstance(instancesTest.instance(i));
            System.out.print("ID: " + instancesTest.instance(i).value(0));// ??
            System.out.print(", actual: "
                    + instancesTest.classAttribute().value(
                            (int) instancesTest.instance(i).classValue()));
            System.out.println(", predicted: "
                    + instancesTest.classAttribute().value((int) pred));
        }
    }
}

Related Tutorials