Example usage for weka.classifiers.trees.m5 PreConstructedLinearModel classifyInstance

List of usage examples for weka.classifiers.trees.m5 PreConstructedLinearModel classifyInstance

Introduction

In this page you can find the example usage for weka.classifiers.trees.m5 PreConstructedLinearModel classifyInstance.

Prototype

public double classifyInstance(Instance inst) throws Exception 

Source Link

Document

Predicts the class of the supplied instance using the linear model.

Usage

From source file:cn.ict.zyq.bestConf.COMT2.Branch2.java

License:Open Source License

public Instance maxPoint(Instances dataset) throws Exception {
    Instance max = new DenseInstance(dataset.numAttributes());
    max.setDataset(dataset);/*from  w  w  w .ja  v  a  2 s.  com*/

    double[] combinedCoefs = null;
    int len = 0;
    for (PreConstructedLinearModel model : linearModelList) {
        //initialization
        if (combinedCoefs == null) {
            len = model.coefficients().length;
            combinedCoefs = new double[len];
            for (int i = 0; i < len; i++)
                combinedCoefs[i] = 0;
        }

        for (int i = 0; i < len; i++)
            combinedCoefs[i] += model.coefficients()[i];
    }

    //the max value is obtained at ends of a range
    for (Map.Entry<Attribute, Range<Double>> ent : rangeMap.entrySet()) {
        int attIdx = ent.getKey().index();
        if (combinedCoefs[attIdx] > 0) {
            //use the upper bound
            if (ent.getValue().hasUpperBound())
                max.setValue(attIdx, ent.getValue().upperEndpoint());
        } else if (combinedCoefs[attIdx] < 0) {
            //use the lower bound
            if (ent.getValue().hasLowerBound())
                max.setValue(attIdx, ent.getValue().lowerEndpoint());
        }
    }

    //now we set the predicted values
    double y = 0;
    for (PreConstructedLinearModel model : linearModelList) {
        y += model.classifyInstance(max);
    }
    y /= linearModelList.size();
    max.setClassValue(y);

    return max;
}