Example usage for weka.core Instance setClassValue

List of usage examples for weka.core Instance setClassValue

Introduction

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

Prototype

public void setClassValue(String value);

Source Link

Document

Sets the class value of an instance to the given value.

Usage

From source file:put.semantic.fcanew.ml.WekaClassifier.java

@Override
public void addExample(Map<String, Double> features, boolean accept) {
    Instance i = makeInstance(features);
    i.setClassValue(classes.elementAt(accept ? 1 : 0).toString());
    if (!accept) {
        i.setWeight(getRejectedWeight());
    }/* w  w w .  ja va2  s . c  o m*/
    instances.add(i);
    tableModel.fireTableRowsInserted(instances.numInstances() - 1, instances.numInstances() - 1);
}

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

License:Open Source License

/**
 * This is the main classification function that is used by the GUI
 *///w w w.java 2 s . c o  m
public double[] getVotesForInstance(Instance inst) {
    DoubleVector combinedVote = new DoubleVector();
    DoubleVector confidenceVec = new DoubleVector();
    double[] ensembleVotes = new double[inst.numClasses()];
    double qbcEntropy = 0.0;
    int success = 0;
    int alpha1 = 1;
    int alpha2 = 1;
    for (int i = 0; i < this.ensemble.length; i++) {
        DoubleVector vote = new DoubleVector(this.ensemble[i].getVotesForInstance(inst));
        if (vote.sumOfValues() > 0.0) {
            vote.normalize();
            confidenceVec.addValues(vote);
            if ((this.useWeightOption != null) && this.useWeightOption.isSet()) {
                vote.scaleValues(1.0 / (this.error[i] * this.error[i]));
                //System.out.println("Ensemble : " + i + " Error: " + this.error[i]);
            }
            combinedVote.addValues(vote);
        }
        //
        //Ignore the classifiers which have high error ratio
        //
        if (this.error[i] < 0.23) {
            //
            // this is the votes of the ensembles for the classes
            //
            success++;
            ensembleVotes[combinedVote.maxIndex()] += combinedVote.getValue(combinedVote.maxIndex());
        }
    }
    //For confidence measure add to the pool  and in order to fit the confidence value between 0 and 1 divide by success val
    //System.out.println("Confidence " + combinedVote.getValue(combinedVote.maxIndex()));
    if ((confidenceVec.getValue(combinedVote.maxIndex())) >= confidenceThreshold) {
        qbcEntropy = queryByCommitee(ensembleVotes, inst.numClasses(), success);
        double activeLearningRatio = (qbcEntropy)
                * (combinedVote.getValue(combinedVote.maxIndex()) / this.ensemble.length);
        inst.setClassValue(combinedVote.maxIndex()); //Set the class value of the instance
        instConfPool.addVotedInstance(inst, combinedVote.getValue(combinedVote.maxIndex()),
                activeLearningRatio);
        instConfCount++;
    }
    return combinedVote.getArrayRef();
}

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

License:Open Source License

/**
 * This is the main classification function that is used by the GUI
 *//*from w  ww. j  ava  2 s  . c o m*/
public double[] getVotesForInstanceOrig(Instance inst) {
    DoubleVector combinedVote = new DoubleVector();
    double[] ensembleVotes = new double[inst.numClasses()];
    double qbcEntropy = 0.0;
    int success = 0;

    for (int i = 0; i < this.ensemble.length; i++) {
        DoubleVector vote = new DoubleVector(this.ensemble[i].getVotesForInstance(inst));
        // This will call the HoeffdingTree's getVotesForInstance Function
        if (vote.sumOfValues() > 0.0) {
            vote.normalize();
            if ((this.useWeightOption != null) && this.useWeightOption.isSet()) {
                vote.scaleValues(1.0 / (this.error[i] * this.error[i]));
                System.out.println("Ensemble : " + i + " Error: " + this.error[i]);
            }
            //
            //Ignore the ensembles which have high error ratio
            //
            if (this.error[i] < 0.3) {
                combinedVote.addValues(vote);
            }
        }
        //
        // this is the votes of the ensembles for the classes
        //
        if (this.error[i] < 0.3) {
            success++;
            ensembleVotes[combinedVote.maxIndex()] += combinedVote.getValue(combinedVote.maxIndex());
        }
    }
    // For confidence measure add to the pool  and in order to fit the confidence value between 0 and 1 divide by success val

    if ((combinedVote.getValue(combinedVote.maxIndex()) / success) >= confidenceThreshold) {
        qbcEntropy = queryByCommitee(ensembleVotes, inst.numClasses(), 0);
        System.out.println("QBC Entropy: " + qbcEntropy);
        double activeLearningRatio = (qbcEntropy)
                + (combinedVote.getValue(combinedVote.maxIndex()) / this.ensemble.length);
        inst.setClassValue(combinedVote.maxIndex());
        instConfPool.addVotedInstance(inst, combinedVote.getValue(combinedVote.maxIndex()),
                activeLearningRatio);
    }
    return combinedVote.getArrayRef();
}

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 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;
    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.LeveragingBagHalf.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  a v a  2s.  com
                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();
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = this.classifierRandom.nextBoolean() ? 0 : (int) this.weightShrinkOption.getValue(); //half bagging
        if (k > 0) {
            if (this.outputCodesOption.isSet()) {
                weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);
            }
            weightedInst.setWeight(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 ww  .  j  av a 2 s. com*/
                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.LeveragingSubag.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;/*  ww  w .jav a 2s .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();

    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = MiscUtils.poisson(1, this.classifierRandom);
        k = (k > 0) ? (int) this.weightShrinkOption.getValue() : 0;
        if (k > 0) {
            if (this.outputCodesOption.isSet()) {
                weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);
            }
            weightedInst.setWeight(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.ADError[imax] = new ADWIN((double) this.deltaAdwinOption.getValue());
        }
    }
}

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  ww  w.  j  a v a 2s.  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.classifiers.OzaBoostAdwin.java

License:Open Source License

public double[] getVotesForInstanceBinary(Instance inst) {
    double combinedVote[] = new double[(int) inst.numClasses()];
    Instance weightedInst = (Instance) inst.copy();
    if (this.initMatrixCodes) {
        for (int i = 0; i < this.ensemble.length; i++) {
            //Replace class by OC
            weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);

            double vote[];
            vote = this.ensemble[i].getVotesForInstance(weightedInst);
            //Binary Case
            int voteClass = 0;
            if (vote.length == 2) {
                voteClass = (vote[1] > vote[0] ? 1 : 0);
            }//from  w  w w . j ava 2 s  . co m
            //Update votes
            for (int j = 0; j < inst.numClasses(); j++) {
                if (this.matrixCodes[i][j] == voteClass) {
                    combinedVote[j] += getEnsembleMemberWeight(i);
                }
            }
        }
    }
    return combinedVote;
}

From source file:tr.gov.ulakbim.jDenetX.streams.clustering.RandomRBFGeneratorEvents.java

License:Open Source License

public Instance nextInstance() {
    numGeneratedInstances++;/*  ww w  . j  av a 2s.  c o m*/
    eventScheduler();

    //make room for thge classlabel
    double[] values_new = new double[numAttsOption.getValue() + 1];
    double[] values = null;
    int clusterChoice = -1;

    if (instanceRandom.nextDouble() > noiseLevelOption.getValue()) {
        clusterChoice = chooseWeightedElement();
        values = kernels.get(clusterChoice).generator.sample(instanceRandom).toDoubleArray();
    } else {
        //get ranodm noise point
        values = getNewSample();
    }

    if (Double.isNaN(values[0])) {
        System.out.println("Instance corrupted:" + numGeneratedInstances);
    }
    System.arraycopy(values, 0, values_new, 0, values.length);

    Instance inst = new DenseInstance(1.0, values_new);
    inst.setDataset(getHeader());
    if (clusterChoice == -1) {
        inst.setClassValue(-1);
    } else {
        inst.setClassValue(kernels.get(clusterChoice).generator.getId());
        //Do we need micro cluster representation if have overlapping clusters?
        //if(!overlappingOption.isSet())
        kernels.get(clusterChoice).addInstance(inst);
    }
    //        System.out.println(numGeneratedInstances+": Overlap is"+updateOverlaps());

    return inst;
}