Example usage for weka.core SparseInstance weight

List of usage examples for weka.core SparseInstance weight

Introduction

In this page you can find the example usage for weka.core SparseInstance weight.

Prototype

@Override
public finaldouble weight() 

Source Link

Document

Returns the instance's weight.

Usage

From source file:cluster.ABC.ClusterUtils.java

License:Open Source License

/** Fast version of meanOrMode - streamlined from Instances.meanOrMode for efficiency 
 *  Does not check for missing attributes, assumes numeric attributes, assumes Sparse instances
 *//*from ww  w.  ja  v  a2  s  .  co m*/

public static double[] meanOrMode(Instances insts) {

    int numAttributes = insts.numAttributes();
    double[] value = new double[numAttributes];
    double weight = 0;

    for (int i = 0; i < numAttributes; i++) {
        value[i] = 0;
    }

    for (int j = 0; j < insts.numInstances(); j++) {
        SparseInstance inst = (SparseInstance) (insts.instance(j));
        weight += inst.weight();

        for (int i = 0; i < inst.numValues(); i++) {
            int indexOfIndex = inst.index(i);
            value[indexOfIndex] += inst.weight() * inst.valueSparse(i);
        }
    }

    if (Utils.eq(weight, 0)) {
        for (int k = 0; k < numAttributes; k++) {
            value[k] = 0;
        }
    } else {
        for (int k = 0; k < numAttributes; k++) {
            value[k] = value[k] / weight;
        }
    }

    return value;
}