Example usage for org.apache.mahout.classifier.df.node Node classify

List of usage examples for org.apache.mahout.classifier.df.node Node classify

Introduction

In this page you can find the example usage for org.apache.mahout.classifier.df.node Node classify.

Prototype

public abstract double classify(Instance instance);

Source Link

Document

predicts the label for the instance

Usage

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

License:Apache License

/**
 * Classifies the data and calls callback for each classification
 *//*  w  w w  . java  2 s.  c  om*/
public void classify(Data data, double[] predictions) {
    Preconditions.checkArgument(data.size() == predictions.length,
            "predictions.length must be equal to data.size()");

    if (data.isEmpty()) {
        return; // nothing to classify
    }

    for (Node tree : trees) {
        for (int index = 0; index < data.size(); index++) {
            predictions[index] = tree.classify(data.get(index));
        }
    }
}

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

License:Apache License

/**
 * predicts the label for the instance//from w w  w  .jav  a2s  .  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);
    }
}