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:tr.gov.ulakbim.jDenetX.classifiers.CoOzaBagASHT.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int trueClass = (int) inst.classValue();
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = MiscUtils.poisson(1.0, this.classifierRandom);
        if (k > 0) {
            Instance weightedInst = (Instance) inst.copy();
            weightedInst.setWeight(inst.weight() * k);
            if (Utils.maxIndex(this.ensemble[i].getVotesForInstance(inst)) == trueClass) { // Here we used the getVotesForInstanceFunction of HoeffdingTree
                this.error[i] += alpha * (0.0 - this.error[i]); // EWMA
            } else {
                this.error[i] += alpha * (1.0 - this.error[i]); // EWMA
            }/*from  w  w  w .  j  a  v a  2  s  .  com*/
            this.ensemble[i].trainOnInstance(weightedInst);
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.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 a  va  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());
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.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 . j  a  va 2  s .  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();
    //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:tr.gov.ulakbim.jDenetX.classifiers.OCBoost.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    double d = 1.0;
    int[] m = new int[this.ensemble.length];
    for (int j = 0; j < this.ensemble.length; j++) {
        int j0 = 0; //max(0,j-K)
        pipos[j] = 1.0;/*from  w  w  w . j a v a 2  s .c  om*/
        pineg[j] = 1.0;
        m[j] = -1;
        if (this.ensemble[j].correctlyClassifies(inst)) {
            m[j] = 1;
        }
        for (int k = j0; k <= j - 1; k++) {
            pipos[j] *= wpos[j][k] / wpos[j][j] * Math.exp(-alphainc[k])
                    + (1.0 - wpos[j][k] / wpos[j][j]) * Math.exp(alphainc[k]);
            pineg[j] *= wneg[j][k] / wneg[j][j] * Math.exp(-alphainc[k])
                    + (1.0 - wneg[j][k] / wneg[j][j]) * Math.exp(alphainc[k]);
        }
        for (int k = 0; k <= j; k++) {
            wpos[j][k] = wpos[j][k] * pipos[j] + d * (m[k] == 1 ? 1 : 0) * (m[j] == 1 ? 1 : 0);
            wneg[j][k] = wneg[j][k] * pineg[j] + d * (m[k] == -1 ? 1 : 0) * (m[j] == -1 ? 1 : 0);
        }
        alphainc[j] = -alpha[j];
        alpha[j] = 0.5 * Math.log(wpos[j][j] / wneg[j][j]);
        alphainc[j] += alpha[j];

        d = d * Math.exp(-alpha[j] * m[j]);

        if (d > 0.0) {
            Instance weightedInst = (Instance) inst.copy();
            weightedInst.setWeight(inst.weight() * d);
            this.ensemble[j].trainOnInstance(weightedInst);
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.OzaBagASHOT.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    final int trueClass = (int) inst.classValue();
    //System.out.println("Ensemble Length " + this.ensemble.length);
    for (int i = 0; i < this.ensemble.length; i++) {
        final int k = MiscUtils.poisson(1.0, this.classifierRandom);
        if (k > 0) {
            final Instance weightedInst = (Instance) inst.copy();
            weightedInst.setWeight(inst.weight() * k);
            if (Utils.maxIndex(this.ensemble[i].getVotesForInstance(inst)) == trueClass) {
                this.error[i] += alpha * (0.0 - this.error[i]); //EWMA
            } else {
                this.error[i] += alpha * (1.0 - this.error[i]); //EWMA
            }/* w  ww.  ja va  2  s. com*/
            this.ensemble[i].trainOnInstance(weightedInst);
        }
        //   System.out.println("ClassifierRandom: " + k);
        //System.out.println("EWMA Error Ensemble "+i+" "+ this.error[i]);
        if (this.error[i] > 0.6) {
            System.out.println("Error is " + this.error[i]);
            System.out.println("Ensemble " + i);
            System.err.println("Warning!!!!!");
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.OzaBoostAdwin.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    // Set log (k-1) and (k-1) for SAMME Method
    if (this.sammeOption.isSet()) {
        this.Km1 = numClasses - 1;
        this.logKm1 = Math.log(this.Km1);
        this.initKm1 = false;
    }/*from  w  w  w.  j a v  a2 s  . co  m*/
    //Output Codes
    if (this.initMatrixCodes) {

        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;
                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 lambda_d = 1.0;
    Instance weightedInst = (Instance) inst.copy();
    for (int i = 0; i < this.ensemble.length; i++) {
        double k = this.pureBoostOption.isSet() ? lambda_d
                : MiscUtils.poisson(lambda_d * this.Km1, this.classifierRandom);
        if (k > 0.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);
        if (correctlyClassifies) {
            this.scms[i] += lambda_d;
            lambda_d *= this.trainingWeightSeenByModel / (2 * this.scms[i]);
        } else {
            this.swms[i] += lambda_d;
            lambda_d *= this.trainingWeightSeenByModel / (2 * this.swms[i]);
        }

        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());
            this.scms[imax] = 0;
            this.swms[imax] = 0;
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

protected int selfTest(InstanceStream testStream) {
    int returnStatus = 1;
    Instance testInst = null;
    int maxInstances = this.maxInstancesOption.getValue();
    long instancesProcessed = 0;
    //InstanceStream testStream = (InstanceStream) getPreparedClassOption(this.testStreamOption);
    ClassificationPerformanceEvaluator evaluator = new BasicClassificationPerformanceEvaluator();
    evaluator.reset();//from w  w w.  ja v a  2s. c o m

    while (testStream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances))) {
        testInst = (Instance) testStream.nextInstance().copy();
        int trueClass = (int) testInst.classValue();
        testInst.setClassMissing();
        double[] prediction = model.getVotesForInstance(testInst);
        evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight());
        instancesProcessed++;
        if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {

            long estimatedRemainingInstances = testStream.estimatedRemainingInstances();
            if (maxInstances > 0) {
                long maxRemaining = maxInstances - instancesProcessed;
                if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
            }
            System.out.println(estimatedRemainingInstances < 0 ? -1.0
                    : (double) instancesProcessed
                            / (double) (instancesProcessed + estimatedRemainingInstances));

        }
    }
    return returnStatus;
}

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

public LearningEvaluation evalModel(InstanceStream trainStream, InstanceStream testStream,
        AbstractClassifier model) {//  w ww.jav  a  2  s . co  m

    model = new SelfOzaBoostID();
    InstanceStream stream = (InstanceStream) trainStream.copy();
    ClassificationPerformanceEvaluator evaluator = new BasicClassificationPerformanceEvaluator();
    Instance testInst = null;
    int maxInstances = this.maxInstancesOption.getValue();
    long instancesProcessed = 0;
    System.out.println("Evaluating model...");
    while (stream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances))) {
        testInst = (Instance) stream.nextInstance().copy();
        int trueClass = (int) testInst.classValue();
        testInst.setClassMissing();
        double[] prediction = model.getVotesForInstance(testInst);
        evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight());
        instancesProcessed++;
        if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
            long estimatedRemainingInstances = stream.estimatedRemainingInstances();
            if (maxInstances > 0) {
                long maxRemaining = maxInstances - instancesProcessed;
                if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
            }
            System.out.println(estimatedRemainingInstances < 0 ? -1.0
                    : (double) instancesProcessed
                            / (double) (instancesProcessed + estimatedRemainingInstances));

        }
    }
    System.out.println("Accuracy result before self-train: " + evaluator.getPerformanceMeasurements()[1]);
    selfTrain(testInst);
    int returnStatus = selfTest(testStream);
    EvalActiveBoostingID.model.resetLearningImpl(); //Learning is completed so we can reset
    return new LearningEvaluation(evaluator.getPerformanceMeasurements());
}

From source file:tr.gov.ulakbim.jDenetX.streams.filters.AddNoiseFilter.java

License:Open Source License

public Instance nextInstance() {
    Instance inst = (Instance) this.inputStream.nextInstance().copy();
    for (int i = 0; i < inst.numAttributes(); i++) {
        double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue()
                : this.attNoiseFractionOption.getValue();
        if (inst.attribute(i).isNominal()) {
            DoubleVector obs = (DoubleVector) this.attValObservers.get(i);
            if (obs == null) {
                obs = new DoubleVector();
                this.attValObservers.set(i, obs);
            }// w  w w .j a v a  2 s . co  m
            int originalVal = (int) inst.value(i);
            if (!inst.isMissing(i)) {
                obs.addToValue(originalVal, inst.weight());
            }
            if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) {
                do {
                    inst.setValue(i, this.random.nextInt(obs.numValues()));
                } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0));
            }
        } else {
            GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i);
            if (obs == null) {
                obs = new GaussianEstimator();
                this.attValObservers.set(i, obs);
            }
            obs.addObservation(inst.value(i), inst.weight());
            inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac);
        }
    }
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.tasks.EvaluateInterleavedTestThenTrain.java

License:Open Source License

@Override
protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) {
    Classifier learner = (Classifier) getPreparedClassOption(this.learnerOption);
    InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
    ClassificationPerformanceEvaluator evaluator = (ClassificationPerformanceEvaluator) getPreparedClassOption(
            this.evaluatorOption);
    learner.setModelContext(stream.getHeader());
    int maxInstances = this.instanceLimitOption.getValue();
    long instancesProcessed = 0;
    int maxSeconds = this.timeLimitOption.getValue();
    int secondsElapsed = 0;
    monitor.setCurrentActivity("Evaluating learner...", -1.0);
    LearningCurve learningCurve = new LearningCurve("learning evaluation instances");
    File dumpFile = this.dumpFileOption.getFile();
    PrintStream immediateResultStream = null;
    if (dumpFile != null) {
        try {/*from ww w  . ja v  a 2 s  .  c  o  m*/
            if (dumpFile.exists()) {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile, true), true);
            } else {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile), true);
            }
        } catch (Exception ex) {
            throw new RuntimeException("Unable to open immediate result file: " + dumpFile, ex);
        }
    }
    boolean firstDump = true;
    boolean preciseCPUTiming = TimingUtils.enablePreciseTiming();
    long evaluateStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
    long lastEvaluateStartTime = evaluateStartTime;
    double RAMHours = 0.0;
    while (stream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances))
            && ((maxSeconds < 0) || (secondsElapsed < maxSeconds))) {
        Instance trainInst = stream.nextInstance();
        Instance testInst = (Instance) trainInst.copy();
        int trueClass = (int) trainInst.classValue();
        testInst.setClassMissing();
        double[] prediction = learner.getVotesForInstance(testInst);
        evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight());
        learner.trainOnInstance(trainInst);
        instancesProcessed++;
        if (instancesProcessed % this.sampleFrequencyOption.getValue() == 0) {
            long evaluateTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
            double time = TimingUtils.nanoTimeToSeconds(evaluateTime - evaluateStartTime);
            double timeIncrement = TimingUtils.nanoTimeToSeconds(evaluateTime - lastEvaluateStartTime);
            double RAMHoursIncrement = learner.measureByteSize() / (1024.0 * 1024.0 * 1024.0); //GBs
            RAMHoursIncrement *= (timeIncrement / 3600.0); //Hours
            RAMHours += RAMHoursIncrement;
            lastEvaluateStartTime = evaluateTime;
            learningCurve.insertEntry(new LearningEvaluation(
                    new Measurement[] { new Measurement("learning evaluation instances", instancesProcessed),
                            new Measurement("evaluation time (" + (preciseCPUTiming ? "cpu " : "") + "seconds)",
                                    time),
                            new Measurement("model cost (RAM-Hours)", RAMHours) },
                    evaluator, learner));
            if (immediateResultStream != null) {
                if (firstDump) {
                    immediateResultStream.println(learningCurve.headerToString());
                    firstDump = false;
                }
                immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1));
                immediateResultStream.flush();
            }
        }
        if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
            if (monitor.taskShouldAbort()) {
                return null;
            }
            long estimatedRemainingInstances = stream.estimatedRemainingInstances();
            if (maxInstances > 0) {
                long maxRemaining = maxInstances - instancesProcessed;
                if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
            }
            monitor.setCurrentActivityFractionComplete(estimatedRemainingInstances < 0 ? -1.0
                    : (double) instancesProcessed
                            / (double) (instancesProcessed + estimatedRemainingInstances));
            if (monitor.resultPreviewRequested()) {
                monitor.setLatestResultPreview(learningCurve.copy());
            }
            secondsElapsed = (int) TimingUtils
                    .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - evaluateStartTime);
        }
    }
    if (immediateResultStream != null) {
        immediateResultStream.close();
    }
    return learningCurve;
}