Example usage for weka.classifiers.trees RandomForest setComputeAttributeImportance

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

Introduction

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

Prototype

public void setComputeAttributeImportance(boolean computeAttributeImportance) 

Source Link

Document

Set whether to compute and output attribute importance scores

Usage

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

License:Open Source License

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