Example usage for weka.classifiers.trees J48 setUnpruned

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

Introduction

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

Prototype

public void setUnpruned(boolean v) 

Source Link

Document

Set the value of unpruned.

Usage

From source file:au.edu.usyd.it.yangpy.snp.Ensemble.java

License:Open Source License

public void ensemble(String mode) throws Exception {

    numInstances = test.numInstances();/* ww  w. j  a va2s.  c  om*/
    numClasses = test.numClasses();
    givenValue = new double[numInstances];
    predictDistribution = new double[numClassifiers][numInstances][numClasses];
    predictValue = new double[numClassifiers][numInstances];
    voteValue = new double[numInstances][numClasses];

    // Setting the given class values of the test instances.
    for (int i = 0; i < numInstances; i++) {
        givenValue[i] = test.instance(i).classValue();
    }

    // Calculating the predicted class values using each classifier respectively.
    // J48 coverTree1NN KStar coverTree3NN coverTree5NN

    J48 tree = new J48();
    tree.setUnpruned(true);
    aucClassifiers[0] = classify(tree, 0);

    KStar kstar = new KStar();
    aucClassifiers[1] = classify(kstar, 1);

    IBk ctnn1 = new IBk(1);
    CoverTree search = new CoverTree();
    ctnn1.setNearestNeighbourSearchAlgorithm(search);
    aucClassifiers[2] = classify(ctnn1, 2);

    IBk ctnn3 = new IBk(3);
    ctnn3.setNearestNeighbourSearchAlgorithm(search);
    aucClassifiers[3] = classify(ctnn3, 3);

    IBk ctnn5 = new IBk(5);
    ctnn5.setNearestNeighbourSearchAlgorithm(search);
    aucClassifiers[4] = classify(ctnn5, 4);

    // Print the classification results if in print mode.
    if (mode.equals("v")) {
        System.out.println("J48   AUC: " + aucClassifiers[0]);
        System.out.println("KStar AUC: " + aucClassifiers[1]);
        System.out.println("CTNN1 AUC: " + aucClassifiers[2]);
        System.out.println("CTNN3 AUC: " + aucClassifiers[3]);
        System.out.println("CTNN5 AUC: " + aucClassifiers[4]);
        System.out.println("   -         -   ");
    }
}

From source file:com.edwardraff.WekaMNIST.java

License:Open Source License

public static void main(String[] args) throws IOException, Exception {
    String folder = args[0];/*w ww  .ja  v a2 s.c om*/
    String trainPath = folder + "MNISTtrain.arff";
    String testPath = folder + "MNISTtest.arff";

    System.out.println("Weka Timings");
    Instances mnistTrainWeka = new Instances(new BufferedReader(new FileReader(new File(trainPath))));
    mnistTrainWeka.setClassIndex(mnistTrainWeka.numAttributes() - 1);
    Instances mnistTestWeka = new Instances(new BufferedReader(new FileReader(new File(testPath))));
    mnistTestWeka.setClassIndex(mnistTestWeka.numAttributes() - 1);

    //normalize range like into [0, 1]
    Normalize normalizeFilter = new Normalize();
    normalizeFilter.setInputFormat(mnistTrainWeka);

    mnistTestWeka = Normalize.useFilter(mnistTestWeka, normalizeFilter);
    mnistTrainWeka = Normalize.useFilter(mnistTrainWeka, normalizeFilter);

    long start, end;

    System.out.println("RBF SVM (Full Cache)");
    SMO smo = new SMO();
    smo.setKernel(new RBFKernel(mnistTrainWeka, 0/*0 causes Weka to cache the whole matrix...*/, 0.015625));
    smo.setC(8.0);
    smo.setBuildLogisticModels(false);
    evalModel(smo, mnistTrainWeka, mnistTestWeka);

    System.out.println("RBF SVM (No Cache)");
    smo = new SMO();
    smo.setKernel(new RBFKernel(mnistTrainWeka, 1, 0.015625));
    smo.setC(8.0);
    smo.setBuildLogisticModels(false);
    evalModel(smo, mnistTrainWeka, mnistTestWeka);

    System.out.println("Decision Tree C45");
    J48 wekaC45 = new J48();
    wekaC45.setUseLaplace(false);
    wekaC45.setCollapseTree(false);
    wekaC45.setUnpruned(true);
    wekaC45.setMinNumObj(2);
    wekaC45.setUseMDLcorrection(true);

    evalModel(wekaC45, mnistTrainWeka, mnistTestWeka);

    System.out.println("Random Forest 50 trees");
    int featuresToUse = (int) Math.sqrt(28 * 28);//Weka uses different defaults, so lets make sure they both use the published way

    RandomForest wekaRF = new RandomForest();
    wekaRF.setNumExecutionSlots(1);
    wekaRF.setMaxDepth(0/*0 for unlimited*/);
    wekaRF.setNumFeatures(featuresToUse);
    wekaRF.setNumTrees(50);

    evalModel(wekaRF, mnistTrainWeka, mnistTestWeka);

    System.out.println("1-NN (brute)");
    IBk wekaNN = new IBk(1);
    wekaNN.setNearestNeighbourSearchAlgorithm(new LinearNNSearch());
    wekaNN.setCrossValidate(false);

    evalModel(wekaNN, mnistTrainWeka, mnistTestWeka);

    System.out.println("1-NN (Ball Tree)");
    wekaNN = new IBk(1);
    wekaNN.setNearestNeighbourSearchAlgorithm(new BallTree());
    wekaNN.setCrossValidate(false);

    evalModel(wekaNN, mnistTrainWeka, mnistTestWeka);

    System.out.println("1-NN (Cover Tree)");
    wekaNN = new IBk(1);
    wekaNN.setNearestNeighbourSearchAlgorithm(new CoverTree());
    wekaNN.setCrossValidate(false);

    evalModel(wekaNN, mnistTrainWeka, mnistTestWeka);

    System.out.println("Logistic Regression LBFGS lambda = 1e-4");
    Logistic logisticLBFGS = new Logistic();
    logisticLBFGS.setRidge(1e-4);
    logisticLBFGS.setMaxIts(500);

    evalModel(logisticLBFGS, mnistTrainWeka, mnistTestWeka);

    System.out.println("k-means (Loyd)");
    int origClassIndex = mnistTrainWeka.classIndex();
    mnistTrainWeka.setClassIndex(-1);
    mnistTrainWeka.deleteAttributeAt(origClassIndex);
    {
        long totalTime = 0;
        for (int i = 0; i < 10; i++) {
            SimpleKMeans wekaKMeans = new SimpleKMeans();
            wekaKMeans.setNumClusters(10);
            wekaKMeans.setNumExecutionSlots(1);
            wekaKMeans.setFastDistanceCalc(true);

            start = System.currentTimeMillis();
            wekaKMeans.buildClusterer(mnistTrainWeka);
            end = System.currentTimeMillis();
            totalTime += (end - start);
        }
        System.out.println("\tClustering took: " + (totalTime / 10.0) / 1000.0 + " on average");
    }
}

From source file:meddle.TrainModelByDomainOS.java

License:Open Source License

/**
 * Given the classifierName, return a classifier
 *
 * @param classifierName//  w  w  w.j a  va2s.com
 *            e.g. J48, Bagging etc.
 */
public static Classifier getClassifier(String classifierName) {
    Classifier classifier = null;
    if (classifierName.equals("J48")) {
        J48 j48 = new J48();
        j48.setUnpruned(true);
        classifier = j48;
    } else if (classifierName.equals("AdaBoostM1")) {
        AdaBoostM1 adm = new AdaBoostM1();
        adm.setNumIterations(10);
        J48 j48 = new J48();
        adm.setClassifier(j48);
        classifier = adm;
    } else if (classifierName.equals("Bagging")) {
        Bagging bagging = new Bagging();
        bagging.setNumIterations(10);
        J48 j48 = new J48();
        bagging.setClassifier(j48);
        classifier = bagging;
    } else if (classifierName.equals("Stacking")) {
        Stacking stacking = new Stacking();
        stacking.setMetaClassifier(new Logistic());
        Classifier cc[] = new Classifier[2];
        cc[0] = new J48();
        cc[1] = new IBk();
        stacking.setClassifiers(cc);
        classifier = stacking;
    } else if (classifierName.equals("AdditiveRegression")) {
        AdditiveRegression ar = new AdditiveRegression();
        ar.setClassifier(new J48());
        classifier = ar;
    } else if (classifierName.equals("LogitBoost")) {
        LogitBoost lb = new LogitBoost();
        lb.setClassifier(new J48());
        classifier = lb;
    }
    return classifier;
}

From source file:statechum.analysis.learning.experiments.PaperUAS.java

License:Open Source License

/** Used to training a few different classifiers from a full PTA by comparing metrics on pairs considered by QSM and checking them against the reference solution. */
protected Classifier[] loadClassifierFromArff(String arffWithTrainingData) {
    weka.classifiers.trees.REPTree tree = new weka.classifiers.trees.REPTree();
    tree.setMaxDepth(3);//from  w  w w  .  j a v a  2  s  .  c  o m
    tree.setNoPruning(true);// since we only use the tree as a classifier (as a conservative extension of what is currently done) and do not actually look at it, elimination of pruning is not a problem. 
    // As part of learning, we also prune some of the nodes where the ratio of correctly-classified pairs to those incorrectly classified is comparable.
    // The significant advantage of not pruning is that the result is no longer sensitive to the order of elements in the tree and hence does not depend on the order in which elements have been obtained by concurrent threads.
    weka.classifiers.trees.J48 tree48 = new weka.classifiers.trees.J48();
    tree48.setUnpruned(true);// since we only use the tree as a classifier (as a conservative extension of what is currently done) and do not actually look at it, elimination of pruning is not a problem. 
    // As part of learning, we also prune some of the nodes where the ratio of correctly-classified pairs to those incorrectly classified is comparable.
    // The significant advantage of not pruning is that the result is no longer sensitive to the order of elements in the tree and hence does not depend on the order in which elements have been obtained by concurrent threads.
    weka.classifiers.lazy.IBk ibk = new weka.classifiers.lazy.IBk(1);
    weka.classifiers.lazy.IB1 ib1 = new weka.classifiers.lazy.IB1();
    weka.classifiers.functions.MultilayerPerceptron perceptron = new weka.classifiers.functions.MultilayerPerceptron();
    Classifier[] outcome = new Classifier[] { ib1 };//tree};//,tree48,ibk};//,perceptron};
    for (Classifier c : outcome)
        trainClassifierFromArff(c, arffWithTrainingData);
    return outcome;
}