naivebayes.TucilWeka.java Source code

Java tutorial

Introduction

Here is the source code for naivebayes.TucilWeka.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 weka.core.SerializationHelper;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.supervised.attribute.Discretize;
import weka.filters.unsupervised.attribute.NumericToNominal;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import java.util.Random;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;

/**
 *
 * @author ASUS
 */
public class TucilWeka {
    public static Instances readDataSet(String filepath) {
        //Membaca dataset

        Instances data = null;
        try {
            data = DataSource.read(filepath);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < data.numAttributes(); i++) {
            System.out.println(i + ". " + data.attribute(i));
        }
        Scanner scan = new Scanner(System.in);
        System.out.print("Class Index : ");
        int temp = scan.nextInt();
        data.setClassIndex(temp);
        if (temp == 26) {
            // data.deleteAttributeAt(27);
        } else
        //jika 27
        {
            //data.deleteAttributeAt(26);
        }
        for (int i = 0; i < data.numAttributes(); i++) {
            System.out.println(i + ". " + data.attribute(i));
        }
        System.out.println("Index kelasnya: " + data.classIndex());
        // data.setClassIndex(data.numAttributes()-1);
        Instances retval = filterDiscretize(data);
        return retval;
    }

    public static Instances filterDiscretize(Instances a) {
        Discretize filter = new Discretize();
        Instances b = null;
        try {
            String[] option = new String[4];
            option[0] = "-R";
            option[1] = "first-last";
            option[2] = "-precision";
            option[3] = "6";
            filter.setInputFormat(a);

            b = Filter.useFilter(a, filter);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }

    public static Evaluation crossValidation(Instances data) {
        //10-fold cross validation
        Evaluation eval = null;
        try {

            eval = new Evaluation(data);
            Classifier cls = new NBTubesAI();
            if (cls == null) {
                System.out.println("MODEL CANNOT BE USED");
            } else {
                System.out.println("MODEL IS USED");
            }
            cls.buildClassifier(data);
            //crossValidateModel:
            //param 1 = tipe classifier (disini J48)
            //param 2 = Instances data
            //param 3 = jumlah fold
            //param 4 = Randomizer (seed)

            eval.crossValidateModel(cls, data, 10, new Random(1));
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }

    public static Evaluation fullTraining(Instances data) {
        //10-fold cross validation
        Evaluation eval = null;
        Instances train = new Instances(data);
        Instances test = new Instances(data);
        try {
            Classifier classify = new NBTubesAI();
            //Membuat klasifier dari data training
            classify.buildClassifier(train);
            eval = new Evaluation(train);
            eval.evaluateModel(classify, test);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }

    public static Classifier getClassifier(Instances data) {
        Classifier classify = new NBTubesAI();
        try {
            classify.buildClassifier(data);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        return classify;
    }

    public static void saveModel(Instances data) {
        //Menyimpan model ke file eksternal
        String pathFile = null;
        System.out.println("Masukkan nama file save model: ");
        Scanner scan = new Scanner(System.in);
        pathFile = scan.next();
        Classifier cls = getClassifier(data);
        try {
            SerializationHelper.write(pathFile, cls);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Object readModel() {
        String pathFile = null;
        Object o = null;
        System.out.println("Masukkan nama file read model: ");
        Scanner scan = new Scanner(System.in);
        pathFile = scan.next();
        try {
            o = SerializationHelper.read(pathFile);
        } catch (Exception ex) {
            Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
        }
        return o;
    }
}