weka bayes classifier - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

weka bayes classifier

Demo Code


import weka.core.Instances;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Random;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.misc.VFI;
import weka.classifiers.trees.J48;

import weka.core.SparseInstance;

public class bayesclassifier {
  public static void main(String[] args) throws Exception {

    BufferedReader reader = new BufferedReader(new FileReader("cmc.arff"));
    Instances data = new Instances(reader);
    reader.close();/*from   ww  w .  j  a  v a  2  s. c o m*/
    data.setClassIndex(data.numAttributes() - 1);
    int i = data.numInstances();
    int j = data.numAttributes() - 1;
    File file = new File("tablecmc.csv");
    Writer output = null;
    output = new BufferedWriter(new FileWriter(file));
    output.write("probability,auc,correct,fmeasure\n");

    Random randomGenerator = new Random();
    int numBlock = data.numInstances() * (data.numAttributes() - 1);
    for (double prob = 0; prob <= 1.0; prob = prob + 0.02) {
      Instances mdata = new Instances(data);
      for (int k = 0; k < i; k++) {
        if (data.instance(k).stringValue(1).equals("1") || data.instance(k).stringValue(1).equals("2")) {
          float p = randomGenerator.nextFloat();
          if (p <= prob)
            mdata.instance(k).setMissing(1);
        }
      }
      Classifier cModel = (Classifier) new J48();
      Evaluation eTest = new Evaluation(mdata);
      eTest.crossValidateModel(cModel, mdata, 10, mdata.getRandomNumberGenerator(1));
      double y1 = eTest.areaUnderROC(0);
      double y2 = eTest.correct();
      double y3 = eTest.fMeasure(0);
      output.write(prob + "," + y1 + "," + y2 + "," + y3 + "\n");
    }
    output.close();
  }
}

Related Tutorials