do Weka Statistics - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

do Weka Statistics

Demo Code



import weka.classifiers.evaluation.Evaluation;
import weka.classifiers.evaluation.Prediction;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;

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

        String[] options = Utils.splitOptions("-C 0.25 -M 2");
        J48 classifier = new J48();
        classifier.setOptions(options);//from  w w  w . j  a v a 2 s  .c  om
        classifier.buildClassifier(instancesTrain);

        Evaluation eval = new Evaluation(instancesTrain);
        double[] d = eval.evaluateModel(classifier, instancesTest);
            System.out.println(pre);
        }
        System.out.println(eval.errorRate());
        for (Prediction p : eval.predictions()) {
            System.out.println(p.actual() + "---" + p.predicted() + "---"
                    + p.weight());
        }

        options = Utils
                .splitOptions("-t bank-train.arff -T bank-test.arff");
        System.out.println(Evaluation.evaluateModel(classifier, options));
    }
}

Related Tutorials