List of usage examples for weka.core Instance numClasses
public int numClasses();
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//*from w ww . j ava 2 s . c om*/ @Override public void trainOnInstanceImpl(Instance instance) { if (m_weights == null) { int length; if (instance.classAttribute().isNominal()) { length = instance.numClasses(); } else { length = 1; } m_weights = new DoubleVector[length]; m_bias = new double[length]; for (int i = 0; i < m_weights.length; i++) { m_weights[i] = new DoubleVector(); m_bias[i] = 0.0; } } for (int i = 0; i < m_weights.length; i++) { this.trainOnInstanceImpl(instance, i); } m_t++; }
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from www.j a v a 2 s .co m*/ * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance inst) { if (m_weights == null) { return new double[inst.numClasses()]; } double[] result = (inst.classAttribute().isNominal()) ? new double[inst.numClasses()] : new double[1]; if (inst.classAttribute().isNumeric()) { double wx = dotProd(inst, m_weights[0], inst.classIndex());// * m_wScale; double z = (wx + m_bias[0]); result[0] = z; return result; } for (int i = 0; i < m_weights.length; i++) { double wx = dotProd(inst, m_weights[i], inst.classIndex());// * m_wScale; double z = (wx + m_bias[i]); if (z <= 0) { // z = 0; if (m_loss == LOGLOSS) { //result[0] = 1.0 / (1.0 + Math.exp(z)); //result[1] = 1.0 - result[0]; result[i] = 1.0 - 1.0 / (1.0 + Math.exp(z)); } else { //result[0] = 1; result[i] = 0; } } else { if (m_loss == LOGLOSS) { //result[1] = 1.0 / (1.0 + Math.exp(-z)); //result[0] = 1.0 - result[1]; result[i] = 1.0 / (1.0 + Math.exp(-z)); } else { //result[1] = 1; result[i] = 1; } } } return result; }
From source file:moa.classifiers.lazy.kNN.java
License:Open Source License
@Override public double[] getVotesForInstance(Instance inst) { double v[] = new double[C + 1]; try {// www . j ava 2s .c om NearestNeighbourSearch search; if (this.nearestNeighbourSearchOption.getChosenIndex() == 0) { search = new LinearNNSearch(this.window); } else { search = new KDTree(); search.setInstances(this.window); } if (this.window.numInstances() > 0) { Instances neighbours = search.kNearestNeighbours(inst, Math.min(kOption.getValue(), this.window.numInstances())); for (int i = 0; i < neighbours.numInstances(); i++) { v[(int) neighbours.instance(i).classValue()]++; } } } catch (Exception e) { //System.err.println("Error: kNN search failed."); //e.printStackTrace(); //System.exit(1); return new double[inst.numClasses()]; } return v; }
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;//from w ww.j a 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(); /*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); }//w w w . j av a 2s . c o 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;/*from w ww .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 = 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 w w . ja v a 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; 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.j a v 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: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 w w w . j av a 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(); //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(); }// www . jav 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); } }