use weka do Classification - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

use weka do Classification

Demo Code

/*/*from  w  w w  .j  a v a2s  .  c  o m*/
 *  How to use WEKA API in Java 
 *  Copyright (C) 2014 
 *  @author Dr Noureddin M. Sadawi (noureddin.sadawi@gmail.com)
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it as you wish ... 
 *  I ask you only, as a professional courtesy, to cite my name, web page 
 *  and my YouTube Channel!
 *  
 */

package weka.api;

//import required classes
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.trees.J48;
import weka.classifiers.functions.SMO;

public class Classification {
    public static void main(String args[]) throws Exception {
        //load dataset
        DataSource source = new DataSource(
                "/home/likewise-open/ACADEMIC/csstnns/Desktop/iris.arff");
        Instances dataset = source.getDataSet();
        //set class index to the last attribute
        dataset.setClassIndex(dataset.numAttributes() - 1);
        //create and build the classifier!
        NaiveBayes nb = new NaiveBayes();
        nb.buildClassifier(dataset);
        //print out capabilities
        System.out.println(nb.getCapabilities().toString());

        SMO svm = new SMO();
        svm.buildClassifier(dataset);
        System.out.println(svm.getCapabilities().toString());

        String[] options = new String[4];
        options[0] = "-C";
        options[1] = "0.11";
        options[2] = "-M";
        options[3] = "3";
        J48 tree = new J48();
        tree.setOptions(options);
        tree.buildClassifier(dataset);
        System.out.println(tree.getCapabilities().toString());
        System.out.println(tree.graph());

    }
}

Related Tutorials