Example usage for weka.classifiers.trees RandomForest computeAverageImpurityDecreasePerAttribute

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

Introduction

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

Prototype

public double[] computeAverageImpurityDecreasePerAttribute(double[] nodeCounts) throws WekaException 

Source Link

Document

Computes the average impurity decrease per attribute over the trees

Usage

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

License:Open Source License

/**
 * {@inheritDoc }//from   w  ww  .  j a v  a  2 s  .  c o  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 "";
}