Example usage for weka.classifiers.trees RandomForest buildClassifier

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

Introduction

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

Prototype

@Override
public void buildClassifier(Instances data) throws Exception 

Source Link

Document

Bagging method.

Usage

From source file:KFST.featureSelection.embedded.TreeBasedMethods.RandomForestMethod.java

License:Open Source License

/**
 * {@inheritDoc }//w  w w .  j a  va  2  s . co  m
 */
@Override
protected String buildClassifier(Instances dataTrain) {
    try {
        RandomForest decisionTreeRandomForest = new RandomForest();
        decisionTreeRandomForest.setNumFeatures(randomForestNumFeatures);
        decisionTreeRandomForest.setMaxDepth(randomForestMaxDepth);
        decisionTreeRandomForest.setNumIterations(randomForestNumIterations);
        decisionTreeRandomForest.setComputeAttributeImportance(true);
        decisionTreeRandomForest.buildClassifier(dataTrain);

        /**
         * Creating an array of indices of the features based on descending
         * order of features' importance
         */
        double[] nodeCounts = new double[numFeatures + 1];
        double[] impurityScores = decisionTreeRandomForest
                .computeAverageImpurityDecreasePerAttribute(nodeCounts);
        int[] sortedIndices = Utils.sort(impurityScores);
        String sortedIndicesToString = "";
        for (int i = sortedIndices.length - 1; i >= 0; i--) {
            if (sortedIndices[i] != numFeatures) {
                sortedIndicesToString += String.valueOf(sortedIndices[i]) + " ";
            }
        }

        return sortedIndicesToString.trim();
        //return decisionTreeRandomForest.toString();
    } catch (Exception ex) {
        Logger.getLogger(RandomForestMethod.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:my.randomforestui.RandomForestUI.java

public static double doRandomForest(Instances training, Instances testing) throws Exception {
    double accuracy;

    //inisialisasi random forest
    String[] options = new String[1];
    // set tree random forest unpruned tree
    options[0] = "-U";
    // new instance of tree
    RandomForest tree = new RandomForest();
    // set the options
    tree.setOptions(options);//  w w w .j ava2 s .c  o m
    // build classifier using training data
    tree.buildClassifier(training);

    Evaluation eval = new Evaluation(testing);
    eval.evaluateModel(tree, testing);
    //System.out.println((eval.correct()/56)*100);

    accuracy = (eval.correct() / 56) * 100;

    return accuracy;
}

From source file:predictors.HelixIndexer.java

License:Open Source License

/**
 * Trains the Weka Classifer./*from   w  w  w .  j  a  v a2  s  .  c o  m*/
 */
public void trainClassifier() {
    try {
        RandomForest classifier = new weka.classifiers.trees.RandomForest();
        Instances data = this.dataset;

        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        data.randomize(new Random(data.size()));

        String[] optClassifier = weka.core.Utils.splitOptions("-I 100 -K 9 -S 1 -num-slots 3");

        classifier.setOptions(optClassifier);
        classifier.setSeed(data.size());

        classifier.buildClassifier(data);

        this.classifier = classifier;
        this.isTrained = true;
    } catch (Exception e) {
        ErrorUtils.printError(HelixIndexer.class, "Training failed", e);
    }
}

From source file:predictors.TopologyPredictor.java

License:Open Source License

/**
 * Trains the Weka Classifer.//from  w w w .ja v  a2  s  . c o  m
 */
public void trainClassifier() {
    try {
        RandomForest classifier = new weka.classifiers.trees.RandomForest();
        Instances data = this.dataset;

        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        data.randomize(new Random(data.size()));

        String[] optClassifier = weka.core.Utils.splitOptions("-I 100 -K 7 -S 1 -num-slots 1");

        classifier.setOptions(optClassifier);
        classifier.setSeed(data.size());

        classifier.buildClassifier(data);

        this.classifier = classifier;
        this.isTrained = true;
    } catch (Exception e) {
        ErrorUtils.printError(TopologyPredictor.class, "Training failed", e);
    }
}

From source file:recsys.BuildModel.java

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

    //Opening the training file
    int own_training = StaticVariables.own_training;
    DataSource sourceTrain;/*ww  w .j a  v  a  2 s.c o m*/
    if (own_training == 1)
        sourceTrain = new DataSource("D://own_training//item//feature data//train_feature.arff");
    else
        sourceTrain = new DataSource("E://recsys//item//feature data//train_feature.arff");

    Instances train = sourceTrain.getDataSet();

    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = "1,2,4"; // first attribute
    //options[2] = "2";                                     // first attribute
    //options[3] = "4";         
    //options[2] = "9";                                     // first attribute
    //options[3] = "3";                                     // first attribute
    //options[4] = "4";                                     // first attribute

    Remove remove = new Remove(); // new instance of filter
    remove.setOptions(options); // set options
    remove.setInputFormat(train); // inform filter about dataset **AFTER** setting options
    Instances newData = Filter.useFilter(train, remove); // apply filter
    System.out.println("number of attributes " + newData.numAttributes());

    System.out.println(newData.firstInstance());

    if (newData.classIndex() == -1) {
        newData.setClassIndex(newData.numAttributes() - 1);
    }

    Resample sampler = new Resample();
    String Fliteroptions = "-B 1.0";
    sampler.setOptions(weka.core.Utils.splitOptions(Fliteroptions));
    sampler.setRandomSeed((int) System.currentTimeMillis());
    sampler.setInputFormat(newData);
    newData = Resample.useFilter(newData, sampler);

    //Normalize normalize = new Normalize();
    //normalize.toSource(Fliteroptions, newData);
    //Remove remove = new Remove();                         // new instance of filter
    //remove.setOptions(options);                           // set options
    //remove.setInputFormat(train);                          // inform filter about dataset **AFTER** setting options
    //Instances newData = Filter.useFilter(train, remove);   // apply filter

    //rm.setAttributeIndices("2");
    //rm.setAttributeIndices("3");
    //rm.setAttributeIndices("4");
    //rm.setAttributeIndices("5");
    //rm.setAttributeIndices("6");

    //rm.setAttributeIndices("6");
    //rm.setAttributeIndices("5");

    //Remove rm = new Remove();
    //rm.setInputFormat(train);
    //rm.setAttributeIndices("1");
    //FilteredClassifier fc = new FilteredClassifier();
    //cls.setOptions(args);
    //J48 cls = new J48();
    //LibSVM cls = new LibSVM();
    //SMO cls = new SMO();
    //Logistic cls = new Logistic();
    //BayesianLogisticRegression cls = new BayesianLogisticRegression();
    //cls.setThreshold(0.52);
    //AdaBoostM1 cls = new AdaBoostM1();
    //NaiveBayes cls = new NaiveBayes();
    //weka.classifiers.meta.Bagging cls = new Bagging();
    //weka.classifiers.functions.IsotonicRegression cls = new IsotonicRegression();
    //j48.setUnpruned(true);        // using an unpruned J48
    // meta-classifier

    //BayesNet cls = new BayesNet();
    RandomForest cls = new RandomForest();
    //cls.setNumTrees(100);
    //cls.setMaxDepth(3);
    //cls.setNumFeatures(3);

    //fc.setClassifier(cls);
    //fc.setFilter(rm);

    // train and make predictions
    //System.out.println(fc.globalInfo());
    //System.out.println(fc.getFilter());
    //fc.buildClassifier(train);
    cls.buildClassifier(newData);
    //Evaluation eval = new Evaluation(newData);
    //Random rand = new Random(1);  // using seed = 1
    //int folds = 2;
    //eval.crossValidateModel(cls, newData, folds, rand);
    //System.out.println(eval.toSummaryString());
    //System.out.println("precision on buy " + eval.precision(newData.classAttribute().indexOfValue("buy")));

    //System.out.println("recall on buy " + eval.recall(newData.classAttribute().indexOfValue("buy")));
    //System.out.println(eval.confusionMatrix().toString());
    //System.out.println("Precision " + eval.precision(newData.classIndex()-1));
    //System.out.println("Recall " + eval.recall(newData.classIndex()-1));
    //Classfier cls = new weka.classifiers.bayes.NaiveBayes();
    //FilteredClassifier fc = new FilteredClassifier();
    //fc.setFilter(rm);
    //fc.setClassifier(cls);

    //train and make predictions
    //fc.buildClassifier(train);

    // serialize model
    ObjectOutputStream oos;
    if (own_training == 1)
        oos = new ObjectOutputStream(new FileOutputStream("D://own_training//item//model//train.model"));
    else
        oos = new ObjectOutputStream(new FileOutputStream("E://recsys//item//model//train.model"));

    oos.writeObject(cls);
    oos.flush();
    oos.close();
}