naivebayes.NBRun.java Source code

Java tutorial

Introduction

Here is the source code for naivebayes.NBRun.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package naivebayes;

import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Normalize;

/**
 *
 * @author ASUS
 */
public class NBRun {
    public static void main(String[] args) throws Exception {
        System.out.println("Naive Bayes Classifier");
        Instances data = TucilWeka.readDataSet("C:\\Program Files\\Weka-3-8\\data\\mush_test.arff");
        Scanner scan = new Scanner(System.in);
        Classifier cls;
        Instances train = TucilWeka.readDataSet("C:\\Program Files\\Weka-3-8\\data\\mush.arff");

        System.out.println("Read or create model? r/c");
        if (scan.next().equals("c")) {
            cls = new NBTubesAI();
            cls.buildClassifier(train);
            TucilWeka.saveModel(train);
        } else {
            cls = (NBTubesAI) TucilWeka.readModel();
        }
        Evaluation eval = new Evaluation(data);
        System.out.println("10 fold cross validate or Full train? c/f");
        if (scan.next().equals("c")) {
            int fold = 10;
            for (int i = 0; i < data.numAttributes(); i++) {
                System.out.println(i + ". " + data.attribute(i));
            }
            eval.crossValidateModel(cls, data, fold, new Random(1));
        } else {
            for (int i = 0; i < data.numAttributes(); i++) {
                System.out.println(i + ". " + data.attribute(i));
            }
            data.deleteWithMissingClass();
            try {

                eval.evaluateModel(cls, data);
            } catch (java.lang.Exception ex) {
                eval.crossValidateModel(cls, data, 11, new Random(1));
            }
        }
        //        Classifier cls=new NBTubesAI();
        //        cls.buildClassifier(data);

        System.out.println("Hasil evaluasi: ");
        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
        System.out.println(eval.toClassDetailsString());
    }
}