Example usage for weka.core Instance classValue

List of usage examples for weka.core Instance classValue

Introduction

In this page you can find the example usage for weka.core Instance classValue.

Prototype

public double classValue();

Source Link

Document

Returns an instance's class value as a floating-point number.

Usage

From source file:moa.classifiers.functions.NoChange.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.lastSeenClass = inst.classValue();
}

From source file:moa.classifiers.functions.Perceptron.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {

    //Init Perceptron
    if (this.reset == true) {
        this.reset = false;
        this.numberAttributes = inst.numAttributes();
        this.numberClasses = inst.numClasses();
        this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()];
        for (int i = 0; i < inst.numClasses(); i++) {
            for (int j = 0; j < inst.numAttributes(); j++) {
                weightAttribute[i][j] = 0.2 * this.classifierRandom.nextDouble() - 0.1;
            }//from   ww  w  .  j ava2 s  . c o  m
        }
    }

    double[] preds = new double[inst.numClasses()];
    for (int i = 0; i < inst.numClasses(); i++) {
        preds[i] = prediction(inst, i);
    }
    double learningRatio = learningRatioOption.getValue();

    int actualClass = (int) inst.classValue();
    for (int i = 0; i < inst.numClasses(); i++) {
        double actual = (i == actualClass) ? 1.0 : 0.0;
        double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]);
        for (int j = 0; j < inst.numAttributes() - 1; j++) {
            this.weightAttribute[i][j] += learningRatio * delta * inst.value(j);
        }
        this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta;
    }
}

From source file:moa.classifiers.functions.SGD.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 *///from w ww.  j  a  v a2s  .  c  o  m
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new DoubleVector();
        m_bias = 0.0;
    }

    if (!instance.classIsMissing()) {

        double wx = dotProd(instance, m_weights, instance.classIndex());

        double y;
        double z;
        if (instance.classAttribute().isNominal()) {
            y = (instance.classValue() == 0) ? -1 : 1;
            z = y * (wx + m_bias);
        } else {
            y = instance.classValue();
            z = y - (wx + m_bias);
            y = 1;
        }

        // Compute multiplier for weight decay
        double multiplier = 1.0;
        if (m_numInstances == 0) {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_t;
        } else {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances;
        }
        for (int i = 0; i < m_weights.numValues(); i++) {
            m_weights.setValue(i, m_weights.getValue(i) * multiplier);
        }

        // Only need to do the following if the loss is non-zero
        if (m_loss != HINGE || (z < 1)) {

            // Compute Factor for updates
            double factor = m_learningRate * y * dloss(z);

            // Update coefficients for attributes
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    m_weights.addToValue(indS, factor * instance.valueSparse(p1));
                }
            }

            // update the bias
            m_bias += factor;
        }
        m_t++;
    }
}

From source file:moa.classifiers.functions.SGDMultiClass.java

License:Open Source License

public void trainOnInstanceImpl(Instance instance, int classLabel) {
    if (!instance.classIsMissing()) {

        double wx = dotProd(instance, m_weights[classLabel], instance.classIndex());

        double y;
        double z;
        if (instance.classAttribute().isNominal()) {
            y = (instance.classValue() != classLabel) ? -1 : 1;
            z = y * (wx + m_bias[classLabel]);
        } else {// w  w  w  . java2 s.c  om
            y = instance.classValue();
            z = y - (wx + m_bias[classLabel]);
            y = 1;
        }

        // Compute multiplier for weight decay
        double multiplier = 1.0;
        if (m_numInstances == 0) {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_t;
        } else {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances;
        }
        for (int i = 0; i < m_weights[classLabel].numValues(); i++) {
            m_weights[classLabel].setValue(i, m_weights[classLabel].getValue(i) * multiplier);
        }

        // Only need to do the following if the loss is non-zero
        if (m_loss != HINGE || (z < 1)) {

            // Compute Factor for updates
            double factor = m_learningRate * y * dloss(z);

            // Update coefficients for attributes
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    m_weights[classLabel].addToValue(indS, factor * instance.valueSparse(p1));
                }
            }

            // update the bias
            m_bias[classLabel] += factor;
        }

    }
}

From source file:moa.classifiers.functions.SGDOld.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 */// w  w  w.  j a v a  2s.co m
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new double[instance.numAttributes() + 1];
    }

    if (!instance.classIsMissing()) {

        double wx = dotProd(instance, m_weights, instance.classIndex());

        double y;
        double z;
        if (instance.classAttribute().isNominal()) {
            y = (instance.classValue() == 0) ? -1 : 1;
            z = y * (wx + m_weights[m_weights.length - 1]);
        } else {
            y = instance.classValue();
            z = y - (wx + m_weights[m_weights.length - 1]);
            y = 1;
        }

        // Compute multiplier for weight decay
        double multiplier = 1.0;
        if (m_numInstances == 0) {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_t;
        } else {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances;
        }
        for (int i = 0; i < m_weights.length - 1; i++) {
            m_weights[i] *= multiplier;
        }

        // Only need to do the following if the loss is non-zero
        if (m_loss != HINGE || (z < 1)) {

            // Compute Factor for updates
            double factor = m_learningRate * y * dloss(z);

            // Update coefficients for attributes
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    m_weights[indS] += factor * instance.valueSparse(p1);
                }
            }

            // update the bias
            m_weights[m_weights.length - 1] += factor;
        }
        m_t++;
    }
}

From source file:moa.classifiers.functions.SPegasos.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance the new training instance to include in the model
 *//*w w  w  .  j  a v a 2 s .co  m*/
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new double[instance.numAttributes() + 1];
    }
    if (!instance.classIsMissing()) {

        double learningRate = 1.0 / (m_lambda * m_t);
        //double scale = 1.0 - learningRate * m_lambda;
        double scale = 1.0 - 1.0 / m_t;
        double y = (instance.classValue() == 0) ? -1 : 1;
        double wx = dotProd(instance, m_weights, instance.classIndex());
        double z = y * (wx + m_weights[m_weights.length - 1]);

        for (int j = 0; j < m_weights.length - 1; j++) {
            if (j != instance.classIndex()) {
                m_weights[j] *= scale;
            }
        }

        if (m_loss == LOGLOSS || (z < 1)) {
            double loss = dloss(z);
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    double m = learningRate * loss * (instance.valueSparse(p1) * y);
                    m_weights[indS] += m;
                }
            }

            // update the bias
            m_weights[m_weights.length - 1] += learningRate * loss * y;
        }

        double norm = 0;
        for (int k = 0; k < m_weights.length - 1; k++) {
            if (k != instance.classIndex()) {
                norm += (m_weights[k] * m_weights[k]);
            }
        }

        double scale2 = Math.min(1.0, (1.0 / (m_lambda * norm)));
        if (scale2 < 1.0) {
            scale2 = Math.sqrt(scale2);
            for (int j = 0; j < m_weights.length - 1; j++) {
                if (j != instance.classIndex()) {
                    m_weights[j] *= scale2;
                }
            }
        }
        m_t++;
    }
}

From source file:moa.classifiers.lazy.kNN.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C)
        C = (int) inst.classValue();
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }//  w w  w .  j ava  2s  . co m
    if (this.limitOption.getValue() <= this.window.numInstances()) {
        this.window.delete(0);
    }
    this.window.add(inst);
}

From source file:moa.classifiers.lazy.kNNwithPAW.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C) {
        C = (int) inst.classValue();
    }//from ww w  .ja  v  a2 s  .c  om
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }

    for (int i = 0; i < this.window.size(); i++) {
        if (this.classifierRandom.nextDouble() > this.prob) {
            this.window.delete(i);
        }
    }
    this.window.add(inst);

}

From source file:moa.classifiers.lazy.kNNwithPAWandADWIN.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C) {
        C = (int) inst.classValue();
    }/* ww w.j  a v  a2 s .  c o m*/
    // ADWIN
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }

    if (this.timeStamp == null) {
        this.timeStamp = new ArrayList<Integer>(10);
    }
    for (int i = 0; i < this.window.size(); i++) {
        if (this.classifierRandom.nextDouble() > this.prob) {
            this.window.delete(i);
            this.timeStamp.remove(i);
        }
    }
    this.window.add(inst);
    this.timeStamp.add(this.time);
    this.time++;
    boolean correctlyClassifies = this.correctlyClassifies(inst);
    if (this.adwin.setInput(correctlyClassifies ? 0 : 1)) {
        //Change
        int size = (int) this.adwin.getWidth();
        for (int i = 0; i < this.window.size(); i++) {
            if (this.timeStamp.get(i) < this.time - size) {
                this.window.delete(i);
                this.timeStamp.remove(i);
            }
        }
    }

}

From source file:moa.classifiers.LeveragingBag.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    //Output Codes
    if (this.initMatrixCodes == true) {
        this.matrixCodes = new int[this.ensemble.length][inst.numClasses()];
        for (int i = 0; i < this.ensemble.length; i++) {
            int numberOnes;
            int numberZeros;

            do { // until we have the same number of zeros and ones
                numberOnes = 0;//from w ww  .j ava 2s .c  om
                numberZeros = 0;
                for (int j = 0; j < numClasses; j++) {
                    int result = 0;
                    if (j == 1 && numClasses == 2) {
                        result = 1 - this.matrixCodes[i][0];
                    } else {
                        result = (this.classifierRandom.nextBoolean() ? 1 : 0);
                    }
                    this.matrixCodes[i][j] = result;
                    if (result == 1) {
                        numberOnes++;
                    } else {
                        numberZeros++;
                    }
                }
            } while ((numberOnes - numberZeros) * (numberOnes - numberZeros) > (this.ensemble.length % 2));

        }
        this.initMatrixCodes = false;
    }

    boolean Change = false;
    double w = 1.0;
    double mt = 0.0;
    Instance weightedInst = (Instance) inst.copy();
    /*for (int i = 0; i < this.ensemble.length; i++) {
    if (this.outputCodesOption.isSet()) {
    weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()] );
    }
    if(!this.ensemble[i].correctlyClassifies(weightedInst)) {
    mt++;
    }
    }*/
    //update w
    w = this.weightShrinkOption.getValue(); //1.0 +mt/2.0;
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = MiscUtils.poisson(w, this.classifierRandom);
        if (k > 0) {
            if (this.outputCodesOption.isSet()) {
                weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);
            }
            weightedInst.setWeight(inst.weight() * k);
            this.ensemble[i].trainOnInstance(weightedInst);
        }
        boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst);
        double ErrEstim = this.ADError[i].getEstimation();
        if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) {
            if (this.ADError[i].getEstimation() > ErrEstim) {
                Change = true;
            }
        }
    }
    if (Change) {
        numberOfChangesDetected++;
        double max = 0.0;
        int imax = -1;
        for (int i = 0; i < this.ensemble.length; i++) {
            if (max < this.ADError[i].getEstimation()) {
                max = this.ADError[i].getEstimation();
                imax = i;
            }
        }
        if (imax != -1) {
            this.ensemble[imax].resetLearning();
            //this.ensemble[imax].trainOnInstance(inst);
            this.ADError[imax] = new ADWIN((double) this.deltaAdwinOption.getValue());
        }
    }
}