Example usage for org.apache.mahout.classifier.df.data DataUtils maxindex

List of usage examples for org.apache.mahout.classifier.df.data DataUtils maxindex

Introduction

In this page you can find the example usage for org.apache.mahout.classifier.df.data DataUtils maxindex.

Prototype

public static int maxindex(Random rng, int[] values) 

Source Link

Document

return the index of the maximum of the array, breaking ties randomly

Usage

From source file:com.wsc.myexample.decisionForest.MyDecisionForest.java

License:Apache License

/**
 * predicts the label for the instance//from   w ww. j  a va2s.  c  o  m
 * 
 * @param rng
 *          Random number generator, used to break ties randomly
 * @return -1 if the label cannot be predicted
 */
public double classify(Dataset dataset, Random rng, Instance instance) {
    if (dataset.isNumerical(dataset.getLabelId())) {
        double sum = 0;
        int cnt = 0;
        for (Node tree : trees) {
            double prediction = tree.classify(instance);
            if (prediction != -1) {
                sum += prediction;
                cnt++;
            }
        }
        return sum / cnt;
    } else {
        int[] predictions = new int[dataset.nblabels()];
        for (Node tree : trees) {
            double prediction = tree.classify(instance);
            if (prediction != -1) {
                predictions[(int) prediction]++;
            }
        }

        if (DataUtils.sum(predictions) == 0) {
            return -1; // no prediction available
        }

        return DataUtils.maxindex(rng, predictions);
    }
}