List of usage examples for weka.core Instance setWeight
public void setWeight(double weight);
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 ww. j a v a2 s.c o m*/ instances.add(i); tableModel.fireTableRowsInserted(instances.numInstances() - 1, instances.numInstances() - 1); }
From source file:put.semantic.fcanew.ml.WekaClassifier.java
@Override public void loadExamples(File f) throws IOException { ArffLoader l = new ArffLoader(); l.setFile(f);/*from www . j a v a 2s . c o m*/ Instances structure = l.getStructure(); Instance i; while ((i = l.getNextInstance(structure)) != null) { if (!instances.checkInstance(i)) { i = convert(i, structure, instances); } else { i.setDataset(instances); } if (instances.checkInstance(i)) { if (i.classValue() == 0) { i.setWeight(getRejectedWeight()); } instances.add(i); } else { System.err.println("Ignoring incompatible instance"); } } updateModel(); tableModel.fireTableDataChanged(); }
From source file:resample.OverSubsample.java
License:Open Source License
/** * Creates a subsample of the current set of input instances. The output * instances are pushed onto the output queue for collection. *///from www . j a v a2 s.c om private void createSubsample() { int classI = getInputFormat().classIndex(); // Sort according to class attribute. getInputFormat().sort(classI); // Determine where each class starts in the sorted dataset int[] classIndices = getClassIndices(); // Get the existing class distribution int[] counts = new int[getInputFormat().numClasses()]; double[] weights = new double[getInputFormat().numClasses()]; int max = -1; for (int i = 0; i < getInputFormat().numInstances(); i++) { Instance current = getInputFormat().instance(i); if (current.classIsMissing() == false) { counts[(int) current.classValue()]++; weights[(int) current.classValue()] += current.weight(); } } // Convert from total weight to average weight for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { weights[i] = weights[i] / counts[i]; } /* System.err.println("Class:" + i + " " + getInputFormat().classAttribute().value(i) + " Count:" + counts[i] + " Total:" + weights[i] * counts[i] + " Avg:" + weights[i]); */ } // find the class with the minimum number of instances int maxIndex = -1; for (int i = 0; i < counts.length; i++) { if ((max < 0) && (counts[i] > 0)) { max = counts[i]; maxIndex = i; } else if ((counts[i] > max) && (counts[i] > 0)) { max = counts[i]; maxIndex = i; } } if (max < 0) { System.err.println("SpreadSubsample: *warning* none of the classes have any values in them."); return; } // determine the new distribution int[] new_counts = new int[getInputFormat().numClasses()]; for (int i = 0; i < counts.length; i++) { new_counts[i] = (int) Math.abs(Math.max(counts[i], max * m_DistributionSpread)); if (i == maxIndex) { if (m_DistributionSpread > 0 && m_DistributionSpread < 1.0) { // don't undersample the majority class! new_counts[i] = counts[i]; } } if (m_DistributionSpread == 0) { new_counts[i] = counts[i]; } if (m_MaxCount > 0) { new_counts[i] = Math.min(new_counts[i], m_MaxCount); } } // Sample with replacement Random random = new Random(m_RandomSeed); //Hashtable t = new Hashtable(); for (int j = 0; j < new_counts.length; j++) { double newWeight = 1.0; if (m_AdjustWeights && (new_counts[j] > 0)) { newWeight = weights[j] * counts[j] / new_counts[j]; /* System.err.println("Class:" + j + " " + getInputFormat().classAttribute().value(j) + " Count:" + counts[j] + " Total:" + weights[j] * counts[j] + " Avg:" + weights[j] + " NewCount:" + new_counts[j] + " NewAvg:" + newWeight); */ } int index = -1; for (int k = 0; k < new_counts[j]; k++) { //boolean ok = false; //do { index = classIndices[j] + (Math.abs(random.nextInt()) % (classIndices[j + 1] - classIndices[j])); // Have we used this instance before? //if (t.get("" + index) == null) { // if not, add it to the hashtable and use it //t.put("" + index, ""); //ok = true; if (index >= 0) { Instance newInst = (Instance) getInputFormat().instance(index).copy(); if (m_AdjustWeights) { newInst.setWeight(newWeight); } push(newInst); } //} //} while (!ok); } } }
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 ww .j ava2 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;//w w w . j a va 2s. 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: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;// ww w. jav a2s . 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(); //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;/* w w w .ja va2 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(); //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;//from ww w .j a 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: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 ww w . j a va 2 s . c o m*/ 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 }/*from w w w . j ava 2 s .c om*/ 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!!!!!"); } } }