Weka Filter Setting - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

Weka Filter Setting

Demo Code



import weka.classifiers.evaluation.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Debug.Random;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;

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

        Filter remove = new Remove();
        String[] options = Utils.splitOptions("-R 1");
        remove.setOptions(options);//from w  w w  . java2  s . c o m
        

        instancesTrain = Filter.useFilter(instancesTrain, remove);
        instancesTest = Filter.useFilter(instancesTest, remove);


        options = Utils.splitOptions("-C 0.25 -M 2");
        classifier.setOptions(options);

        classifier.buildClassifier(instancesTrain);

        Evaluation eval = new Evaluation(instancesTrain);
        eval.evaluateModel(classifier, instancesTest);
        System.out.println(eval.errorRate());

        eval = new Evaluation(instancesTrain);
        eval.crossValidateModel(classifier, instancesTrain, 10, new Random(
                1));
        System.out.println(eval.errorRate());
    }
}

Related Tutorials