Weka Updatable Classifier - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

Weka Updatable Classifier

Demo Code



import java.io.File;

import weka.classifiers.bayes.NaiveBayesUpdateable;
import weka.classifiers.evaluation.Evaluation;
import weka.core.Debug.Random;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;


public class WekaUpdateableClassifier {
    public static void main(String[] args) throws Exception {
        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(
                "bank-train.arff"));
        Instances structure = loader.getStructure();
        structure.setClassIndex(structure.numAttributes() - 1);

        // train NaiveBayes
        NaiveBayesUpdateable nb = new NaiveBayesUpdateable();
        nb.buildClassifier(structure);//from  ww w .  ja v a2  s . c o  m

        Instance current;
        while ((current = loader.getNextInstance(structure)) != null)
            nb.updateClassifier(current);

        loader = new ArffLoader();
        loader.setFile(new File(
                "bank-train.arff"));
        Instances instancesTrain = loader.getDataSet();
        instancesTrain.setClassIndex(instancesTrain.numAttributes() - 1);

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

Related Tutorials