Example usage for weka.classifiers.trees J48 J48

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

Introduction

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

Prototype

J48

Source Link

Usage

From source file:development.GoodHonoursPrediction.java

public static void main(String[] args) {
    Instances data = ClassifierTools.loadData("C:\\Admin\\Perfomance Analysis\\GoodHonsClassification");
    RandomForest rf = new RandomForest();
    double[][] a = ClassifierTools.crossValidationWithStats(rf, data, data.numInstances());
    System.out.println(" Random forest LOOCV accuracy =" + a[0][0]);
    J48 tree = new J48();
    a = ClassifierTools.crossValidationWithStats(tree, data, data.numInstances());
    System.out.println(" C4.5 LOOCV accuracy =" + a[0][0]);
    IBk knn = new IBk(11);
    knn.setCrossValidate(true);/*from w  w w  . ja va2 s  . c  om*/
    a = ClassifierTools.crossValidationWithStats(knn, data, data.numInstances());
    System.out.println(" KNN LOOCV accuracy =" + a[0][0]);
    NaiveBayes nb = new NaiveBayes();
    a = ClassifierTools.crossValidationWithStats(nb, data, data.numInstances());
    System.out.println(" Naive Bayes LOOCV accuracy =" + a[0][0]);

    /*       try {
    tree.buildClassifier(data);
           System.out.println(" Tree ="+tree);
           Classifier cls = new J48();
            Evaluation eval = new Evaluation(data);
            Random rand = new Random(1);  // using seed = 1
            int folds = data.numInstances();
            eval.crossValidateModel(cls, data, folds, rand);
            System.out.println(eval.toSummaryString());        
                   
           tree.getTechnicalInformation();
           } catch (Exception ex) {
    Logger.getLogger(GoodHonoursPrediction.class.getName()).log(Level.SEVERE, null, ex);
           }
           */

}

From source file:DiversifyQuery.DivTopK.java

public static void table4_5() throws Exception {

    // Initialise classifiers required for this experiment
    classifiers = new Classifier[8];
    classifiers[0] = new ShapeletTreeClassifier("infoTree.txt");
    classifiers[1] = new J48();
    classifiers[2] = new IB1();
    classifiers[3] = new NaiveBayes();
    classifiers[4] = new BayesNet();
    classifiers[5] = new RandomForest();
    classifiers[6] = new RotationForest();
    classifiers[7] = new SMO();

    // Set up names for the classifiers - only used for output
    classifierNames = new String[8];
    classifierNames[0] = "ShapeletTree";
    classifierNames[1] = "C4.5";
    classifierNames[2] = "1NN";
    classifierNames[3] = "Naive Bayes";
    classifierNames[4] = "Bayesian Network";
    classifierNames[5] = "Random Forest";
    classifierNames[6] = "Rotation Forest";
    classifierNames[7] = "SVM (linear)";

    //        if ((classifierToProcessIndex < 1 || classifierToProcessIndex > classifiers.length) && classifierToProcessIndex != -1) {
    //            throw new IOException("Invalid classifier identifier.");
    //        } else {
    //            if (classifierToProcessIndex != -1) {
    //                classifierToProcessIndex--;
    //            }
    //        }//from   w  w w.j  a v  a2 s.  com

    // Compute classifier accuracies for each classifier
    double accuracies[] = new double[classifiers.length];

    for (int i = 1; i < classifiers.length; i++) {

        //if (i == classifierToProcessIndex || classifierToProcessIndex == -1) {
        accuracies[i] = classifierAccuracy(i, true, true);

    }

    // Write experiment output to file 
    writeFileContent(accuracies);
}

From source file:edu.brandeis.wisedb.scheduler.training.decisiontree.DTSearcher.java

License:Open Source License

private void init(Instances is, QueryTimePredictor qtp, ModelSLA sla) throws Exception {
    wekaDataSet = is;/*from  w w w .j a  v  a2s  . c  om*/

    this.qtp = qtp;
    this.sla = sla;

    // the last attribute is the action / class label
    wekaDataSet.setClassIndex(wekaDataSet.numAttributes() - 1);

    // build the tree with a pruning level of 0.25
    // and a minimum of 2 instances per leaf node
    String[] options = new String[] { "-C", "0.05", "-M", "2" };

    tree = new J48();
    tree.setOptions(options);

    // do 10 fold cross validation

    // do NOT train the classifier before evaluation!
    /*
    Evaluation eval = new Evaluation(wekaDataSet);
    eval.crossValidateModel(tree, wekaDataSet, 10, new Random());
    log.info("Model CV: correct/incorrect: " + eval.correct() + "/" + eval.incorrect());
     */

    // build the tree
    tree.buildClassifier(wekaDataSet);

    attributes = new Attribute[wekaDataSet.numAttributes()];
    for (int i = 0; i < wekaDataSet.numAttributes(); i++) {
        attributes[i] = wekaDataSet.attribute(i);
    }

    log.finer("Got attributes: " + Arrays.toString(attributes));
}

From source file:edu.cmu.cs.in.hoop.hoops.analyze.HoopWekaML.java

License:Open Source License

/**
 *
 *///from w  w w . java 2  s .  c o  m
public HoopWekaML() {
    setClassName("HoopWekaML");
    debug("HoopWekaML ()");

    removeOutPort("KV");

    setHoopDescription("Run Weka Machine Learning");

    String[] options = new String[1];
    options[0] = "-U"; // unpruned tree
    J48 tree = new J48(); // new instance of tree

    try {
        tree.setOptions(options);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Declare a nominal attribute along with its values
    FastVector fvNominalVal = new FastVector(3);
    fvNominalVal.addElement("blue");
    fvNominalVal.addElement("gray");
    fvNominalVal.addElement("black");

    // Declare the class attribute along with its values
    FastVector fvClassVal = new FastVector(2);
    fvClassVal.addElement("positive");
    fvClassVal.addElement("negative");
    Attribute ClassAttribute = new Attribute("theClass", fvClassVal);

    // Declare two numeric attributes
    Attribute Attribute1 = new Attribute("firstNumeric");
    Attribute Attribute2 = new Attribute("secondNumeric");
    Attribute Attribute3 = new Attribute("aNominal", fvNominalVal);

    // Declare the feature vector
    FastVector fvWekaAttributes = new FastVector(4);
    fvWekaAttributes.addElement(Attribute1);
    fvWekaAttributes.addElement(Attribute2);
    fvWekaAttributes.addElement(Attribute3);
    fvWekaAttributes.addElement(ClassAttribute);

    // Create an empty training set
    Instances isTrainingSet = new Instances("Rel", fvWekaAttributes, 10);

    // Set class index
    isTrainingSet.setClassIndex(3);

    try {
        tree.buildClassifier(isTrainingSet);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:edu.cuny.qc.speech.AuToBI.featureextractor.XValSpectrumPADFeatureExtractor.java

License:Open Source License

/**
 * Generates cross validated pitch accent detection hypotheses using energy information drawn from the assigned
 * spectral region.//from  ww  w.  j  a v a 2 s .c  o  m
 *
 * @param regions The regions to extract features from.
 * @throws FeatureExtractorException if something goes wrong.
 */
public void extractFeatures(List regions) throws FeatureExtractorException {
    // Construct a feature set.
    FeatureSet feature_set = fs.newInstance();

    // Extract spectrum features.
    feature_set.setDataPoints((List<Word>) regions);
    try {
        PartitionUtils.assignStratifiedFoldNum((List<Word>) regions, FOLD_ASSIGNMENT_FEATURE, num_folds,
                feature_set.getClassAttribute());
    } catch (AuToBIException e) {
        throw new FeatureExtractorException(e.getMessage());
    }

    // Train n-fold cross validated prediction features.
    for (int fold_num = 0; fold_num < num_folds; ++fold_num) {
        AuToBIClassifier classifier = new WekaClassifier(new J48());

        FeatureSet training_fs = fs.newInstance();

        List<Word> training_regions = new ArrayList<Word>();
        List<Word> testing_regions = new ArrayList<Word>();
        try {
            PartitionUtils.splitData((List<Word>) regions, training_regions, testing_regions, fold_num,
                    FOLD_ASSIGNMENT_FEATURE);
        } catch (AuToBIException e) {
            // This should never happen.  If there is a problem with splitData it would have thrown
            // an exception during the fold assignment method assignStratifiedFoldNum
            throw new FeatureExtractorException(e.getMessage());
        }
        training_fs.setDataPoints(training_regions);
        training_fs.constructFeatures();
        try {
            classifier.train(training_fs);
        } catch (Exception e) {
            throw new FeatureExtractorException(e.getMessage());
        }

        for (Word w : testing_regions) {
            try {
                Distribution result = classifier.distributionForInstance(w);

                w.setAttribute("nominal_bark_" + low + "_" + high + "__prediction",
                        result.getKeyWithMaximumValue());
                w.setAttribute("bark_" + low + "_" + high + "__prediction_confidence",
                        result.get(result.getKeyWithMaximumValue()));
                w.setAttribute("bark_" + low + "_" + high + "__prediction_confidence_accented",
                        result.get(ACCENTED_VALUE));
            } catch (Exception e) {
                throw new FeatureExtractorException(e.getMessage());
            }
        }
    }
}

From source file:epsi.i5.datamining.Weka.java

public void excutionAlgo() throws FileNotFoundException, IOException, Exception {
    BufferedReader reader = new BufferedReader(new FileReader("src/epsi/i5/data/" + fileOne + ".arff"));
    Instances data = new Instances(reader);
    reader.close();/* w  w  w .j a va2  s.c o m*/
    //System.out.println(data.attribute(0));
    data.setClass(data.attribute(0));
    NaiveBayes NB = new NaiveBayes();
    NB.buildClassifier(data);
    Evaluation naiveBayes = new Evaluation(data);
    naiveBayes.crossValidateModel(NB, data, 10, new Random(1));
    naiveBayes.evaluateModel(NB, data);
    //System.out.println(test.confusionMatrix() + "1");
    //System.out.println(test.correct() + "2");
    System.out.println("*****************************");
    System.out.println("******** Naive Bayes ********");
    System.out.println(naiveBayes.toMatrixString());
    System.out.println("*****************************");
    System.out.println("**** Pourcentage Correct ****");
    System.out.println(naiveBayes.pctCorrect());
    System.out.println("");
    J48 j = new J48();
    j.buildClassifier(data);
    Evaluation jeval = new Evaluation(data);
    jeval.crossValidateModel(j, data, 10, new Random(1));
    jeval.evaluateModel(j, data);
    System.out.println("*****************************");
    System.out.println("************ J48 ************");
    System.out.println(jeval.toMatrixString());
    System.out.println("*****************************");
    System.out.println("**** Pourcentage Correct ****");
    System.out.println(jeval.pctCorrect());
    System.out.println("");
    DecisionTable DT = new DecisionTable();
    DT.buildClassifier(data);
    Evaluation decisionTable = new Evaluation(data);
    decisionTable.crossValidateModel(DT, data, 10, new Random(1));
    decisionTable.evaluateModel(DT, data);
    System.out.println("*****************************");
    System.out.println("******* DecisionTable *******");
    System.out.println(decisionTable.toMatrixString());
    System.out.println("*****************************");
    System.out.println("**** Pourcentage Correct ****");
    System.out.println(decisionTable.pctCorrect());
    System.out.println("");
    OneR OR = new OneR();
    OR.buildClassifier(data);
    Evaluation oneR = new Evaluation(data);
    oneR.crossValidateModel(OR, data, 10, new Random(1));
    oneR.evaluateModel(OR, data);
    System.out.println("*****************************");
    System.out.println("************ OneR ***********");
    System.out.println(oneR.toMatrixString());
    System.out.println("*****************************");
    System.out.println("**** Pourcentage Correct ****");
    System.out.println(oneR.pctCorrect());

    //Polarit
    data.setClass(data.attribute(1));
    System.out.println("");
    M5Rules MR = new M5Rules();
    MR.buildClassifier(data);
    Evaluation m5rules = new Evaluation(data);
    m5rules.crossValidateModel(MR, data, 10, new Random(1));
    m5rules.evaluateModel(MR, data);
    System.out.println("*****************************");
    System.out.println("********** M5Rules **********");
    System.out.println(m5rules.correlationCoefficient());

    System.out.println("");
    LinearRegression LR = new LinearRegression();
    LR.buildClassifier(data);
    Evaluation linearR = new Evaluation(data);
    linearR.crossValidateModel(LR, data, 10, new Random(1));
    linearR.evaluateModel(LR, data);
    System.out.println("*****************************");
    System.out.println("********** linearR **********");
    System.out.println(linearR.correlationCoefficient());
}

From source file:ergasia2pkg.ergasia2_main.java

public static void ML_RUS_exec() throws InvalidDataFormatException, Exception {

    ML_RUS transformer_under = new ML_RUS(0.1f);

    MultiLabelInstances mli = new MultiLabelInstances("enron.arff", "enron.xml");
    MultiLabelInstances mliCloneUnder = transformer_under.transformInstances(mli);

    RAkEL rak = new RAkEL();
    HOMER hom = new HOMER();
    CalibratedLabelRanking cal = new CalibratedLabelRanking(new J48());
    IBLR_ML ibl = new IBLR_ML();
    Evaluator eval = new Evaluator();
    MultipleEvaluation evaluationsUnder_rak = eval.crossValidate(rak, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_hom = eval.crossValidate(hom, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_cal = eval.crossValidate(cal, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_ibl = eval.crossValidate(ibl, mliCloneUnder, 10);
    System.out.println("========== ML-Undersampling Evaluations (RAkEL) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_rak.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (HOMER) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_hom.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (CalibratedLabelRanking) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_cal.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (IBLR_ML) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_ibl.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Macro-averaged F-Measure"));
}

From source file:ergasia2pkg.ergasia2_main.java

public static void ML_ROS_exec() throws InvalidDataFormatException, Exception {

    ML_ROS transformer_over = new ML_ROS(0.1f);

    MultiLabelInstances mli = new MultiLabelInstances("CAL500.arff", "CAL500.xml");
    MultiLabelInstances mliCloneOver = transformer_over.transformInstances(mli);

    RAkEL rak = new RAkEL();
    HOMER hom = new HOMER();
    CalibratedLabelRanking cal = new CalibratedLabelRanking(new J48());
    IBLR_ML ibl = new IBLR_ML();
    Evaluator eval = new Evaluator();
    MultipleEvaluation evaluationsUnder_rak = eval.crossValidate(rak, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_hom = eval.crossValidate(hom, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_cal = eval.crossValidate(cal, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_ibl = eval.crossValidate(ibl, mliCloneOver, 10);
    System.out.println("========== ML-Undersampling Evaluations (RAkEL) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_rak.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (HOMER) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_hom.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (CalibratedLabelRanking) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_cal.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (IBLR_ML) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_ibl.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Macro-averaged F-Measure"));
}

From source file:ergasia2pkg.ergasia2_main.java

public static void LP_ROS_exec() throws InvalidDataFormatException, Exception {
    LP_ROS transformer_over = new LP_ROS(0.1f);

    MultiLabelInstances mli = new MultiLabelInstances("CAL500.arff", "CAL500.xml");
    MultiLabelInstances mliCloneOver = transformer_over.transformInstances(mli);

    RAkEL rak = new RAkEL();
    HOMER hom = new HOMER();
    CalibratedLabelRanking cal = new CalibratedLabelRanking(new J48());
    IBLR_ML ibl = new IBLR_ML();
    Evaluator eval = new Evaluator();
    MultipleEvaluation evaluationsUnder_rak = eval.crossValidate(rak, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_hom = eval.crossValidate(hom, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_cal = eval.crossValidate(cal, mliCloneOver, 10);
    MultipleEvaluation evaluationsUnder_ibl = eval.crossValidate(ibl, mliCloneOver, 10);
    System.out.println("========== ML-Undersampling Evaluations (RAkEL) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_rak.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (HOMER) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_hom.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (CalibratedLabelRanking) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_cal.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (IBLR_ML) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_ibl.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Macro-averaged F-Measure"));
}

From source file:ergasia2pkg.ergasia2_main.java

public static void LP_RUS_exec() throws InvalidDataFormatException, Exception {
    LP_RUS transformer_under = new LP_RUS(0.1f);

    MultiLabelInstances mli = new MultiLabelInstances("CAL500.arff", "CAL500.xml");
    MultiLabelInstances mliCloneUnder = transformer_under.transformInstances(mli);

    RAkEL rak = new RAkEL();
    HOMER hom = new HOMER();
    CalibratedLabelRanking cal = new CalibratedLabelRanking(new J48());
    IBLR_ML ibl = new IBLR_ML();
    Evaluator eval = new Evaluator();
    MultipleEvaluation evaluationsUnder_rak = eval.crossValidate(rak, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_hom = eval.crossValidate(hom, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_cal = eval.crossValidate(cal, mliCloneUnder, 10);
    MultipleEvaluation evaluationsUnder_ibl = eval.crossValidate(ibl, mliCloneUnder, 10);
    System.out.println("========== ML-Undersampling Evaluations (RAkEL) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_rak.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_rak.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (HOMER) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_hom.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_hom.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (CalibratedLabelRanking) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_cal.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_cal.getMean("Macro-averaged F-Measure"));
    System.out.println("========== ML-Undersampling Evaluations (IBLR_ML) =========");
    System.out.println("Example-Based Accuracy: " + evaluationsUnder_ibl.getMean("Example-Based Accuracy"));
    System.out.println("Micro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Micro-averaged F-Measure"));
    System.out.println("Macro-averaged F-Measure: " + evaluationsUnder_ibl.getMean("Macro-averaged F-Measure"));
}