Example usage for weka.core Instance weight

List of usage examples for weka.core Instance weight

Introduction

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

Prototype

public double weight();

Source Link

Document

Returns the instance's weight.

Usage

From source file:moa.classifiers.bayes.NaiveBayesMultinomial.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 va 2  s .  c  o  m*/
@Override
public void trainOnInstanceImpl(Instance inst) {
    if (this.reset == true) {
        this.m_numClasses = inst.numClasses();
        double laplace = this.laplaceCorrectionOption.getValue();
        int numAttributes = inst.numAttributes();

        m_probOfClass = new double[m_numClasses];
        Arrays.fill(m_probOfClass, laplace);

        m_classTotals = new double[m_numClasses];
        Arrays.fill(m_classTotals, laplace * numAttributes);

        m_wordTotalForClass = new DoubleVector[m_numClasses];
        for (int i = 0; i < m_numClasses; i++) {
            //Arrays.fill(wordTotal, laplace);
            m_wordTotalForClass[i] = new DoubleVector();
        }
        this.reset = false;
    }
    // Update classifier
    int classIndex = inst.classIndex();
    int classValue = (int) inst.value(classIndex);

    double w = inst.weight();
    m_probOfClass[classValue] += w;

    m_classTotals[classValue] += w * totalSize(inst);
    double total = m_classTotals[classValue];

    for (int i = 0; i < inst.numValues(); i++) {
        int index = inst.index(i);
        if (index != classIndex && !inst.isMissing(i)) {
            //m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i);
            double laplaceCorrection = 0.0;
            if (m_wordTotalForClass[classValue].getValue(index) == 0) {
                laplaceCorrection = this.laplaceCorrectionOption.getValue();
            }
            m_wordTotalForClass[classValue].addToValue(index, w * inst.valueSparse(i) + laplaceCorrection);
        }
    }
}

From source file:moa.classifiers.DecisionStump.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
    for (int i = 0; i < inst.numAttributes() - 1; i++) {
        int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);
        AttributeClassObserver obs = this.attributeObservers.get(i);
        if (obs == null) {
            obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                    : newNumericClassObserver();
            this.attributeObservers.set(i, obs);
        }//from  w ww  .j av  a 2  s  .  c o  m
        obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight());
    }
    if (this.trainingWeightSeenByModel - this.weightSeenAtLastSplit >= this.gracePeriodOption.getValue()) {
        this.bestSplit = findBestSplit((SplitCriterion) getPreparedClassOption(this.splitCriterionOption));
        this.weightSeenAtLastSplit = this.trainingWeightSeenByModel;
    }
}

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

License:Apache License

@Override
public double[] getVotesForInstance(Instance i) {
    DoubleVector observedClassDistribution = new DoubleVector();
    if (this.randNumGen.nextFloat() < this.desiredAccuracyOption.getValue()) {
        observedClassDistribution.addToValue((int) i.classValue(), i.weight());
    } else {//  w w  w.  j a v  a2 s  .co m
        observedClassDistribution.addToValue(((int) i.classValue() + 1) % i.numClasses(), i.weight());

    }
    return observedClassDistribution.getArrayCopy();
}

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

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
}

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

License:Apache License

@Override
public double[] getVotesForInstance(Instance i) {
    DoubleVector observedClassDistribution = new DoubleVector();
    int classToGuess = this.randNumGen.nextInt(i.numClasses());
    //double weightToGuess = this.randNumGen.nextDouble();
    observedClassDistribution.addToValue(classToGuess, i.weight());
    return observedClassDistribution.getArrayCopy();
}

From source file:moa.classifiers.imbalanced.SamplingClassifier.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classIndex() == 0) {
        this.rareCount += 1.0;
    }//w ww .j a  v a 2 s .com
    this.count += 1.0;
    double w;

    if (this.overSampleOption.isSet() && inst.classIndex() == 0) {
        w = 1.0 / (this.rareCount / this.count);
        if (this.logTransformOption.isSet()) {
            w = Math.log(w);
        }
    } else if (this.underSampleOption.isSet() && inst.classIndex() != 0) {
        w = 1.0 - this.rareCount / this.count;
    } else {
        w = 1.0;
    }
    int k = MiscUtils.poisson(w, this.classifierRandom);
    Instance weightedInst = (Instance) inst.copy();
    weightedInst.setWeight(inst.weight() * k);
    this.classifier.trainOnInstance(weightedInst);
}

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   ww  w.  j  a  va2s. co  m*/
                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());
        }
    }
}

From source file:moa.classifiers.LeveragingBagME.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;/*  w w w.j  av  a  2 s. c  o m*/
                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;
    Instance weightedInst = (Instance) inst.copy();
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        double error = this.ADError[i].getEstimation();
        double k = !this.ensemble[i].correctlyClassifies(weightedInst) ? 1.0
                : (this.classifierRandom.nextDouble() < (error / (1.0 - error)) ? 1.0 : 0.0);///error);
        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());
        }
    }
}

From source file:moa.classifiers.LeveragingBagWT.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 w w  .jav  a2 s  . c  o  m
                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();
    //update w
    w = this.weightShrinkOption.getValue();
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = 1 + 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());
        }
    }
}

From source file:moa.classifiers.macros.TACNB.java

License:Open Source License

public Instance extendWithOldLabels(Instance instance) {
    if (this.header == null) {
        initHeader(instance.dataset());/*from  w  w  w  . j ava  2 s .c o m*/
    }
    int numLabels = this.oldLabels.length;
    if (numLabels == 0) {
        return instance;
    }
    double[] x = instance.toDoubleArray();
    double[] x2 = Arrays.copyOfRange(this.oldLabels, 0, numLabels + x.length);
    System.arraycopy(x, 0, x2, numLabels, x.length);
    Instance extendedInstance = new DenseInstance(instance.weight(), x2);
    extendedInstance.setDataset(this.header);
    //System.out.println( extendedInstance);
    return extendedInstance;
}