List of usage examples for weka.core Instance copy
Object copy();
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; }//from w ww .j a v a2s . 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;/* w w w. j ava2s .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(); /*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.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); }/* ww w. j av a 2s. c om*/ //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;//from w ww . ja v a2s .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 = 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;//from w ww. ja v a2s. 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; 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 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. 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(); //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.meta.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;//w ww . j a va 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; Instance weightedInst = (Instance) inst.copy(); double w = this.weightShrinkOption.getValue(); //Train ensemble of classifiers for (int i = 0; i < this.ensemble.length; i++) { double k = 0.0; switch (this.leveraginBagAlgorithmOption.getChosenIndex()) { case 0: //LeveragingBag k = MiscUtils.poisson(w, this.classifierRandom); break; case 1: //LeveragingBagME double error = this.ADError[i].getEstimation(); k = !this.ensemble[i].correctlyClassifies(weightedInst) ? 1.0 : (this.classifierRandom.nextDouble() < (error / (1.0 - error)) ? 1.0 : 0.0); break; case 2: //LeveragingBagHalf w = 1.0; k = this.classifierRandom.nextBoolean() ? 0.0 : w; break; case 3: //LeveragingBagWT w = 1.0; k = 1.0 + MiscUtils.poisson(w, this.classifierRandom); break; case 4: //LeveragingSubag w = 1.0; k = MiscUtils.poisson(1, this.classifierRandom); k = (k > 0) ? w : 0; break; } 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.meta.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 a2 s. co m pineg[j] = 1.0; m[j] = -1; if (this.ensemble[j].correctlyClassifies(inst) == true) { 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); } } }