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

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

Introduction

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

Prototype

public static int sum(int[] values) 

Source Link

Document

Computes the sum of the values

Usage

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

License:Apache License

/**
 * predicts the label for the instance/*from w ww.j ava  2 s.  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);
    }
}