List of usage examples for weka.core Instance classValue
public double classValue();
From source file:moa.classifiers.LeveragingBag.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 == false) { 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 . jav a2s . co m*/ //Update votes for (int j = 0; j < inst.numClasses(); j++) { if (this.matrixCodes[i][j] == voteClass) { combinedVote[j] += 1; } } } } return combinedVote; }
From source file:moa.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 ww .j a va 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(); //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: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 ww .j ava2 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; 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 ww w .j a v 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(); //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.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;/*from ww w . ja v a 2 s. 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(); //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:moa.classifiers.LimAttClassifier.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { int numClasses = inst.numClasses(); //Init Ensemble if (this.initClassifiers == true) { numberAttributes = numAttributesOption.getValue(); if (bigTreesOption.isSet()) { numberAttributes = inst.numAttributes() - 1 - numAttributesOption.getValue(); }//from w ww . ja v a 2 s. c o m CombinationGenerator x = new CombinationGenerator(inst.numAttributes() - 1, this.numberAttributes); int numberClassifiers = x.getTotal().intValue(); this.ensemble = new Classifier[numberClassifiers]; Classifier baseLearner = (Classifier) getPreparedClassOption(this.baseLearnerOption); baseLearner.resetLearning(); for (int i = 0; i < this.ensemble.length; i++) { this.ensemble[i] = baseLearner.copy(); } this.ADError = new ADWIN[this.ensemble.length]; for (int i = 0; i < this.ensemble.length; i++) { this.ADError[i] = new ADWIN((double) this.deltaAdwinOption.getValue()); } this.numberOfChangesDetected = 0; //Prepare combinations int i = 0; if (baseLearner instanceof LimAttHoeffdingTree) { while (x.hasMore()) { ((LimAttHoeffdingTree) this.ensemble[i]).setlistAttributes(x.getNext()); i++; } } this.initClassifiers = false; } boolean Change = false; Instance weightedInst = (Instance) inst.copy(); //Train Perceptron double[][] votes = new double[this.ensemble.length + 1][numClasses]; for (int i = 0; i < this.ensemble.length; i++) { double[] v = new double[numClasses]; for (int j = 0; j < v.length; j++) { v[j] = (double) this.oddsOffsetOption.getValue(); } double[] vt = this.ensemble[i].getVotesForInstance(inst); double sum = Utils.sum(vt); if (!Double.isNaN(sum) && (sum > 0)) { for (int j = 0; j < vt.length; j++) { vt[j] /= sum; } } else { // Just in case the base learner returns NaN for (int k = 0; k < vt.length; k++) { vt[k] = 0.0; } } sum = numClasses * (double) this.oddsOffsetOption.getValue(); for (int j = 0; j < vt.length; j++) { v[j] += vt[j]; sum += vt[j]; } for (int j = 0; j < vt.length; j++) { votes[i][j] = Math.log(v[j] / (sum - v[j])); } } if (adwinReplaceWorstClassifierOption.isSet() == false) { //Train ensemble of classifiers for (int i = 0; i < this.ensemble.length; i++) { boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst); double ErrEstim = this.ADError[i].getEstimation(); if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) { numInstances = initialNumInstancesOption.getValue(); if (this.ADError[i].getEstimation() > ErrEstim) { Change = true; //Replace classifier if ADWIN has detected change numberOfChangesDetected++; this.ensemble[i].resetLearning(); this.ADError[i] = new ADWIN((double) this.deltaAdwinOption.getValue()); for (int ii = 0; ii < inst.numClasses(); ii++) { weightAttribute[ii][i] = 0.0;// 0.2 * Math.random() - 0.1; } } } } } else { //Train ensemble of classifiers for (int i = 0; i < this.ensemble.length; i++) { 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; } } } //Replace classifier with higher error if ADWIN has detected change 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()); for (int ii = 0; ii < inst.numClasses(); ii++) { weightAttribute[ii][imax] = 0.0; } } } } trainOnInstanceImplPerceptron(inst.numClasses(), (int) inst.classValue(), votes); for (int i = 0; i < this.ensemble.length; i++) { this.ensemble[i].trainOnInstance(inst); } }
From source file:moa.classifiers.macros.TACNB.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance instance) { this.baseLearner.trainOnInstance(extendWithOldLabels(instance)); if (this.labelDelayOption.isSet() == false) { // Use true old Labels to add attributes to instances addOldLabel(instance.classValue()); }//w w w .java 2 s. c o m }
From source file:moa.classifiers.meta.AccuracyUpdatedEnsemble.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.initVariables(); this.classDistributions[(int) inst.classValue()]++; this.currentChunk.add(inst); this.processedInstances++; if (this.processedInstances % this.chunkSizeOption.getValue() == 0) { this.processChunk(); }//from w w w.j a va 2s . c o m }
From source file:moa.classifiers.meta.AccuracyUpdatedEnsemble2.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.initVariables(); this.classDistributions[(int) inst.classValue()]++; this.currentChunk.add(inst); this.processedInstances++; if (this.processedInstances % this.chunkSizeOption.getValue() == 0) { this.processChunk(); }/*from w w w . j a v a 2 s . c o m*/ }
From source file:moa.classifiers.meta.AccuracyUpdatedEnsemble2.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.initVariables(); this.classDistributions[(int) inst.classValue()]++; this.currentChunk.add(inst); this.processedInstances++; if (this.processedInstances % this.chunkSizeOption.getValue() == 0) { this.processChunk(); }//from w ww.ja v a2 s . co m }