use weka classifiers LibSVM - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

use weka classifiers LibSVM

Demo Code



import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.evaluation.Evaluation;
import weka.classifiers.functions.LibSVM;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class WekaEvaluation {
    public static void main(String[] args) {
        try {/*  w w w  .  j a  v a 2 s.  co m*/
            File inputFile = new File(
                    "bank-train.arff");
            atf.setFile(inputFile);
            Instances instancesTrain = atf.getDataSet(); 
                    "bank-test.arff");
            Instances instancesTest = atf.getDataSet(); 
                    .setClassIndex(instancesTrain.numAttributes() - 1);
            instancesTest.setClassIndex(instancesTest.numAttributes() - 1);

            Classifier classifier1 = (Classifier) Class.forName(
                    "weka.classifiers.bayes.NaiveBayes").newInstance();
                    "weka.classifiers.trees.J48").newInstance();
            Classifier classifier3 = (Classifier) Class.forName(
                    "weka.classifiers.rules.ZeroR").newInstance();

            LibSVM classifier4 = new LibSVM();

            classifier1.buildClassifier(instancesTrain);
            classifier2.buildClassifier(instancesTrain);
            classifier3.buildClassifier(instancesTrain);
            classifier4.buildClassifier(instancesTrain);

            Evaluation eval = new Evaluation(instancesTrain);

            eval.evaluateModel(classifier1, instancesTrain);
            System.out.println(eval.errorRate());
            eval.evaluateModel(classifier2, instancesTest);
            System.out.println(eval.errorRate());
            eval.evaluateModel(classifier3, instancesTest);
            System.out.println(eval.errorRate());
            eval.evaluateModel(classifier4, instancesTest);
            System.out.println(eval.errorRate());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials