Example usage for weka.classifiers.trees J48 buildClassifier

List of usage examples for weka.classifiers.trees J48 buildClassifier

Introduction

In this page you can find the example usage for weka.classifiers.trees J48 buildClassifier.

Prototype

@Override
public void buildClassifier(Instances instances) throws Exception 

Source Link

Document

Generates the classifier.

Usage

From source file:tubes.ml.pkg1.TubesML1.java

public void akses() throws Exception {
    Discretize filter;/*from   w  w  w  .jav  a 2  s  .  c om*/
    int fold = 10;
    int fold3 = 3;
    int trainNum, testNum;
    PrintWriter file = new PrintWriter("model.txt");

    /***dataset 1***/
    file.println("***DATASET 1***");
    fileReader tets = new fileReader("./src/data/iris.arff");
    try {
        tets.read();
    } catch (IOException ex) {
        Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
    }
    Instances data = tets.getData();
    filter = new Discretize();
    try {
        filter.setInputFormat(data);
    } catch (Exception ex) {
        Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
    }

    /*ID3*/
    Instances discreteData;
    discreteData = Filter.useFilter(data, filter);
    trainNum = discreteData.numInstances() * 3 / 4;
    testNum = discreteData.numInstances() / 4;

    for (int i = 0; i < fold; i++) {
        try {

            Instances train = discreteData.trainCV(fold, i);
            Instances test = discreteData.testCV(fold, i);

            Id3 iTiga = new Id3();
            Evaluation validation = new Evaluation(train);
            try {
                iTiga.buildClassifier(train);
                System.out.println(iTiga.toString());
                file.println(iTiga.toString());
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(iTiga, test);
            System.out.println(validation.toSummaryString());
            file.println("Validation " + (i + 1));
            file.println(validation.toSummaryString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*J48*/
    trainNum = data.numInstances() * 3 / 4;
    testNum = data.numInstances() / 4;
    J48 jKT = new J48();
    for (int i = 0; i < fold; i++) {
        Instances train = data.trainCV(fold, i);
        Instances test = data.testCV(fold, i);
        try {
            Evaluation validation = new Evaluation(train);
            try {
                jKT.buildClassifier(data);
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(jKT, test);
            System.out.println(validation.toSummaryString());
            file.println("Validation " + (i + 1));
            file.println(validation.toSummaryString());
            // System.out.println(jKT.toString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*dataset 2*/
    file.println("***DATASET 2***");
    tets.setFilepath("./src/data/weather.arff");
    try {
        tets.read();
    } catch (IOException ex) {
        Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
    }
    data = new Instances(tets.getData());

    /*ID3*/
    discreteData = Filter.useFilter(data, filter);
    trainNum = discreteData.numInstances() * 3 / 4;
    testNum = discreteData.numInstances() / 4;

    for (int i = 0; i < fold3; i++) {
        try {
            Instances train = discreteData.trainCV(trainNum, i);
            Instances test = discreteData.testCV(testNum, i);

            Id3 iTiga = new Id3();
            Evaluation validation = new Evaluation(train);
            try {
                iTiga.buildClassifier(train);
                System.out.println(iTiga.toString());
                //file.println(iTiga.toString());
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(iTiga, test);
            System.out.println(validation.toSummaryString());
            file.println("Validation " + (i + 1));
            file.println(validation.toSummaryString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    System.out.println(testNum);
    file.println("Test Number");
    file.println(testNum);

    /*J48*/
    trainNum = data.numInstances() * 3 / 4;
    testNum = data.numInstances() / 4;

    for (int i = 0; i < fold; i++) {
        Instances train = data.trainCV(fold, i);
        Instances test = data.testCV(fold, i);
        try {
            Evaluation validation = new Evaluation(train);
            try {
                jKT.buildClassifier(data);
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(jKT, test);
            System.out.println(validation.toSummaryString());
            file.println(validation.toSummaryString());
            System.out.println(jKT.toString());
            file.println(jKT.toString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*dataset 3*/
    file.println("***DATASET 3***");
    tets.setFilepath("./src/data/weather.nominal.arff");
    try {
        tets.read();
    } catch (IOException ex) {
        Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
    }
    data = new Instances(tets.getData());

    /*ID3*/
    discreteData = Filter.useFilter(data, filter);
    trainNum = discreteData.numInstances() * 3 / 4;
    testNum = discreteData.numInstances() / 4;

    for (int i = 0; i < fold3; i++) {
        try {
            Instances train = discreteData.trainCV(fold, i);
            Instances test = discreteData.testCV(fold, i);

            Id3 iTiga = new Id3();
            Evaluation validation = new Evaluation(train);
            try {
                iTiga.buildClassifier(train);
                System.out.println(iTiga.toString());
                file.println(iTiga.toString());
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(iTiga, test);
            System.out.println(validation.toSummaryString());
            file.println(validation.toSummaryString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    System.out.println(testNum);
    file.println("Test Number");
    file.println(testNum);

    /*J48*/
    trainNum = data.numInstances() * 3 / 4;
    testNum = data.numInstances() / 4;

    for (int i = 0; i < fold; i++) {
        Instances train = data.trainCV(fold, i);
        Instances test = data.testCV(fold, i);
        try {
            Evaluation validation = new Evaluation(train);
            try {
                jKT.buildClassifier(data);
            } catch (Exception ex) {
                Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
            }
            validation.evaluateModel(jKT, test);
            System.out.println(validation.toSummaryString());
            file.println(validation.toSummaryString());
            System.out.println(jKT.toString());
            file.println(jKT.toString());
        } catch (Exception ex) {
            Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*RESULTT*/
    System.out.println(jKT.toString());
    file.println("RESULT");
    file.println(jKT.toString());
    file.close();
}

From source file:uv.datamining.tp2.WekaModeler.java

public static void generarArbol(File file, float cm) throws Exception {
    ArffLoader loader = new ArffLoader();
    loader.setFile(file);/*  w w  w  .  ja  va 2 s .  com*/
    Instances data = loader.getDataSet();
    data.setClassIndex(data.numAttributes() - 1); //columna con el atributo clase
    J48 tree = new J48();
    tree.setConfidenceFactor(cm);
    tree.buildClassifier(data);
    Evaluation eval = new Evaluation(data);
    eval.evaluateModel(tree, data);
    System.out.println(eval.toSummaryString());

    weka.core.SerializationHelper.write(
            file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".model", tree);

}

From source file:wtute.engine.AnalysisEngine.java

public void train() throws Exception {

    Instances trainingInstances = createInstances("TRAINING INS");
    for (int i = 0; i < data.numInstances(); i++) {
        Instance instance = convertInstance(data.instance(i));

        instance.setDataset(trainingInstances);
        trainingInstances.add(instance);
    }// w w w.  j  a  va2s  . c  o  m

    System.out.println(data);
    J48 classifier = new J48();

    try {
        //classifier training code
        classifier.buildClassifier(trainingInstances);

        //storing the trained classifier to a file for future use
        weka.core.SerializationHelper.write("J48.model", classifier);
    } catch (Exception ex) {
        System.out.println("Exception in training the classifier.");
    }
}