Example usage for weka.classifiers.bayes NaiveBayes buildClassifier

List of usage examples for weka.classifiers.bayes NaiveBayes buildClassifier

Introduction

In this page you can find the example usage for weka.classifiers.bayes NaiveBayes buildClassifier.

Prototype

@Override
public void buildClassifier(Instances instances) throws Exception 

Source Link

Document

Generates the classifier.

Usage

From source file:lector.Analizador.java

public static void clasificador() {

    BufferedReader reader1;// w  ww.  j av  a2  s  .c  o m
    BufferedReader reader2;
    try {
        reader1 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                + "proyecto/compartida/DataSetAnalisisSentimientos.arff"));

        reader2 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                + "proyecto/compartida/DataSetAnalisisSentimientos_inc.arff"));
        Instances train = new Instances(reader1);
        train.setClassIndex(train.numAttributes() - 1);
        System.out.println(train.classIndex() + " " + train.numAttributes());

        Instances test = new Instances(reader2);
        test.setClassIndex(train.numAttributes() - 1);
        System.out.println(test.classIndex() + " " + test.numAttributes());

        NaiveBayes model = new NaiveBayes();
        model.buildClassifier(train);

        //classify
        Instances labeled = new Instances(test);

        for (int i = 0; i < test.numInstances(); i++) {
            double clsLabel = model.classifyInstance(test.instance(i));
            labeled.instance(i).setClassValue(clsLabel);
        }

        // https://youtu.be/JY_x5zKTfyo?list=PLJbE6j2EG1pZnBhOg3_Rb63WLCprtyJag
        Evaluation eval_train = new Evaluation(test);
        eval_train.evaluateModel(model, test);

        reader1.close();
        reader2.close();

        //System.out.println(eval_train.toSummaryString("\nResults\n======\n", false));
        String[] options = new String[4];
        options[0] = "-t"; //name of training file
        options[1] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                + "compartida/DataSetAnalisisSentimientos.arff";
        options[2] = "-T";
        options[3] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                + "compartida/DataSetAnalisisSentimientos_inc.arff";
        System.out.println(Evaluation.evaluateModel(model, options));

        try ( // print classification results to file
                BufferedWriter writer = new BufferedWriter(
                        new FileWriter("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                                + "proyecto/compartida/DataSetAnalisisSentimientos_labeled.arff"))) {
            writer.write(labeled.toString());
        }

    } catch (Exception e) {
    }
}

From source file:model.clasification.klasifikacijaIstanca.java

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

    // load data//from  w  w  w  .j  a  v a 2  s.c  o  m
    DataSource loader = new DataSource(fileName);
    Instances data = loader.getDataSet();
    data.setClassIndex(data.numAttributes() - 1);

    // Create the Naive Bayes Classifier
    NaiveBayes bayesClsf = new NaiveBayes();
    bayesClsf.buildClassifier(data);

    // output generated model
    //      System.out.println(bayesClsf);

    // Test the model with the original set
    Evaluation eval = new Evaluation(data);
    eval.evaluateModel(bayesClsf, data);

    // Print the result as in Weka explorer
    String strSummary = eval.toSummaryString();

    //      System.out.println("=== Evaluation on training set ===");
    //      System.out.println("=== Summary ===");
    //      System.out.println(strSummary);

    // Get the confusion matrix
    System.out.println(eval.toMatrixString());

}

From source file:PEBL.TwoStep.java

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

    ConverterUtils.DataSource source = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\mixed.csv");

    Instances data = source.getDataSet();

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }/*from  w  w  w.  jav a2 s  .  c o  m*/

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    // options[0] = "-C 0.25 -M 2";            // unpruned tree
    options[0] = "-K";
    NaiveBayes c = new NaiveBayes(); // new instance of tree
    c.setOptions(options); // set the options
    c.buildClassifier(data); // build classifier

    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(c, data, 10, new Random(1));
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());
    System.out.println("--- model learned on mixed set ---");

    // load unlabeled data
    ConverterUtils.DataSource s = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\unlabelled.csv");
    Instances unlabeled = s.getDataSet();
    // set class attribute
    unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

    nmf = new NumericToNominal();
    nmf.setInputFormat(unlabeled);
    unlabeled = Filter.useFilter(unlabeled, nmf);

    // label instances
    for (int i = 0; i < unlabeled.numInstances(); i++) {
        double classZero = c.distributionForInstance(unlabeled.instance(i))[0];
        double classOne = c.distributionForInstance(unlabeled.instance(i))[1];
        System.out.print(
                "classifying: " + unlabeled.instance(i) + " : " + classZero + " - " + classOne + " == class: ");
        if (classZero > classOne) {
            System.out.print("0");
            unlabeled.instance(i).setClassValue("0");
        } else {
            System.out.print("1");
            unlabeled.instance(i).setClassValue("1");
        }
        System.out.println("");
    }

    // save labeled data
    // BufferedWriter writer = new BufferedWriter(
    //         new FileWriter("Z:\\\\shared from vm\\\\thirdset\\\\relabelled.arff"));
    // writer.write(labeled.toString());
    // writer.newLine();
    // writer.flush();
    // writer.close();
    ArffSaver saver = new ArffSaver();
    saver.setInstances(unlabeled);
    saver.setFile(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));
    //        saver.setDestination(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));   // **not** necessary in 3.5.4 and later
    saver.writeBatch();

}