Example usage for org.apache.mahout.classifier.df.data Dataset nblabels

List of usage examples for org.apache.mahout.classifier.df.data Dataset nblabels

Introduction

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

Prototype

public int nblabels() 

Source Link

Usage

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

License:Apache License

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