Example usage for weka.core AlgVector scalarMultiply

List of usage examples for weka.core AlgVector scalarMultiply

Introduction

In this page you can find the example usage for weka.core AlgVector scalarMultiply.

Prototype

public final void scalarMultiply(double s) 

Source Link

Document

Computes the scalar product of this vector with a scalar

Usage

From source file:moa.reduction.bayes.OFSGDAttributeEval.java

License:Open Source License

public void updateEvaluator(Instance inst) throws Exception {

    if (weights == null) {
        weights = new AlgVector(new double[inst.numAttributes() - 1]);
        for (int i = 0; i < weights.numElements(); i++)
            weights.setElement(i, 0);/*w  ww  .  jav  a2  s  .c o  m*/
    }

    double[] rawx = Arrays.copyOfRange(inst.toDoubleArray(), 0, inst.numAttributes() - 1);
    AlgVector x = new AlgVector(rawx);
    double pred = weights.dotMultiply(x);

    if (pred * inst.classValue() <= 1) {
        x.scalarMultiply(eta * inst.classValue());
        weights = weights.add(x);
        weights.scalarMultiply(Math.min(1.0, 1 / (Math.sqrt(lambda) * weights.norm())));

        int counts = 0;
        Pair[] array = new Pair[weights.numElements()];
        for (int i = 0; i < weights.numElements(); i++) {
            array[i] = new Pair(i, weights.getElement(i));
            if (weights.getElement(i) != 0)
                counts++;
        }

        // Truncate
        if (counts > numFeatures) {
            Arrays.sort(array);
            for (int i = numFeatures + 1; i < array.length; i++)
                weights.setElement(array[i].index, 0);
        }
    }
    updated = true;
}