List of usage examples for weka.core Instance numClasses
public int numClasses();
From source file:moa.classifiers.LimAttClassifier.java
License:Open Source License
@Override public double[] getVotesForInstance(Instance inst) { if (this.initClassifiers == true) { return new double[0]; }//from w w w . j a va 2s. c o m int numClasses = inst.numClasses(); int sizeEnsemble = this.ensemble.length; if (pruneOption.isSet()) { sizeEnsemble = this.numEnsemblePruningOption.getValue(); } double[][] votes = new double[sizeEnsemble + 1][numClasses]; int[] bestClassifiers = new int[sizeEnsemble]; if (pruneOption.isSet()) { //Check for the best classifiers double[] weight = new double[this.ensemble.length]; for (int i = 0; i < numClasses; i++) { for (int j = 0; j < this.ensemble.length; j++) { weight[j] += weightAttribute[i][j]; } } Arrays.sort(weight); double cutValue = weight[this.ensemble.length - sizeEnsemble]; //reverse order int ii = 0; for (int j = 0; j < this.ensemble.length; j++) { if (weight[j] >= cutValue && ii < sizeEnsemble) { bestClassifiers[ii] = j; ii++; } } } else { //Not pruning: all classifiers for (int ii = 0; ii < sizeEnsemble; ii++) { bestClassifiers[ii] = ii; } } for (int ii = 0; ii < sizeEnsemble; ii++) { int i = bestClassifiers[ii]; 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[ii][j] = Math.log(v[j] / (sum - v[j])); // votes[i][j] = vt[j]; } } return getVotesForInstancePerceptron(votes, bestClassifiers, inst.numClasses()); }
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 w 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; 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.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; }/* w ww .java 2 s . com*/ //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; 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:moa.classifiers.meta.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 == 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 ww w . j a v a2s .c o 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:moa.classifiers.NaiveBayesMultinomial.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//*www . ja v a 2 s. c o m*/ @Override public void trainOnInstanceImpl(Instance inst) { if (this.reset == true) { this.m_numClasses = inst.numClasses(); double laplace = this.laplaceCorrectionOption.getValue(); int numAttributes = inst.numAttributes(); m_probOfClass = new double[m_numClasses]; Arrays.fill(m_probOfClass, laplace); m_classTotals = new double[m_numClasses]; Arrays.fill(m_classTotals, laplace * numAttributes); m_wordTotalForClass = new double[numAttributes][m_numClasses]; for (double[] wordTotal : m_wordTotalForClass) { Arrays.fill(wordTotal, laplace); } this.reset = false; } // Update classifier int classIndex = inst.classIndex(); int classValue = (int) inst.value(classIndex); double w = inst.weight(); m_probOfClass[classValue] += w; m_classTotals[classValue] += w * totalSize(inst); double total = m_classTotals[classValue]; for (int i = 0; i < inst.numValues(); i++) { int index = inst.index(i); if (index != classIndex && !inst.isMissing(i)) { m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i); } } }
From source file:moa.classifiers.novelClass.AbstractNovelClassClassifier.java
License:Apache License
@Override public boolean correctlyClassifies(Instance inst) { double[] votes = getVotesForInstance(inst); int prediction = Utils.maxIndex(votes); int groundTruth = (int) inst.classValue(); boolean isCorrect = (prediction == groundTruth); if (!isCorrect && votes.length > inst.numClasses()) { // If it was intially wrong, if (!this.labelsSeen.containsKey(groundTruth) // becuase the we don't know about the true label, && (votes.length >= (this.novelLabelIndex - 1)) // but we used a novel class detection classifier, && votes[this.novelLabelIndex] > 0) { // and it marked it as a novel label isCorrect = true; // Then we can count it as correct }/*from ww w .j a v a 2 s. c om*/ } return isCorrect; }
From source file:moa.classifiers.Perceptron.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { //Init Perceptron if (this.reset == true) { this.reset = false; this.numberAttributes = inst.numAttributes(); this.numberClasses = inst.numClasses(); this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()]; for (int i = 0; i < inst.numClasses(); i++) { for (int j = 0; j < inst.numAttributes(); j++) { weightAttribute[i][j] = 0.2 * Math.random() - 0.1; }//ww w . j a v a 2 s . c o m } } double[] preds = new double[inst.numClasses()]; for (int i = 0; i < inst.numClasses(); i++) { preds[i] = prediction(inst, i); } double learningRatio = learningRatioOption.getValue(); int actualClass = (int) inst.classValue(); for (int i = 0; i < inst.numClasses(); i++) { double actual = (i == actualClass) ? 1.0 : 0.0; double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]); for (int j = 0; j < inst.numAttributes() - 1; j++) { this.weightAttribute[i][j] += learningRatio * delta * inst.value(j); } this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta; } }
From source file:moa.classifiers.rules.GeRules.java
License:Open Source License
@Override public double[] getVotesForInstance(Instance inst) { // TODO Auto-generated method stub // increase no. of seen intances totalSeenInstances++;/*from ww w.j a v a 2 s. co m*/ // check if there is any rules that cover the instance ArrayList<Rule> coveredRules = RulesCoveredInstance(inst); // logger.debug("No. Rules cover instance: " + coveredRules.size()); // logger.debug(inst); // return prediction if there are rules that cover the instance if (coveredRules.size() > 0) { actualAttempts++; double[] classPrediction = new double[inst.numClasses()]; // vote class labels from all available rules for (Rule rule : coveredRules) { classPrediction[(int) rule.classification]++; // logger.debug(rule.printRule()); } // actual attempt if (Utils.maxIndex(classPrediction) == (int) inst.classValue()) { actualAttemptsCorrectlyClassified++; } return classPrediction; } // otherwise, return the majority class return observedClassDistribution.getArrayCopy(); }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
public void updateRuleAttribStatistics(Instance inst, Rule rl, int ruleIndex) { rl.instancesSeen++;//from w ww . j a v a2 s .com if (rl.squaredAttributeStatisticsSupervised.size() == 0 && rl.attributeStatisticsSupervised.size() == 0) { for (int s = 0; s < inst.numAttributes() - 1; s++) { ArrayList<Double> temp1 = new ArrayList<Double>(); ArrayList<Double> temp2 = new ArrayList<Double>(); rl.attributeStatisticsSupervised.add(temp1); rl.squaredAttributeStatisticsSupervised.add(temp2); int instAttIndex = modelAttIndexToInstanceAttIndex(s, inst); if (instance.attribute(instAttIndex).isNumeric()) { for (int i = 0; i < inst.numClasses(); i++) { rl.attributeStatisticsSupervised.get(s).add(0.0); rl.squaredAttributeStatisticsSupervised.get(s).add(1.0); } } } } for (int s = 0; s < inst.numAttributes() - 1; s++) { int instAttIndex = modelAttIndexToInstanceAttIndex(s, inst); if (!inst.isMissing(instAttIndex)) { if (instance.attribute(instAttIndex).isNumeric()) { rl.attributeStatistics.addToValue(s, inst.value(s)); rl.squaredAttributeStatistics.addToValue(s, inst.value(s) * inst.value(s)); double sumValue = rl.attributeStatisticsSupervised.get(s).get((int) inst.classValue()) + inst.value(s); rl.attributeStatisticsSupervised.get(s).set((int) inst.classValue(), sumValue); double squaredSumvalue = rl.squaredAttributeStatisticsSupervised.get(s) .get((int) inst.classValue()) + (inst.value(s) * inst.value(s)); rl.squaredAttributeStatisticsSupervised.get(s).set((int) inst.classValue(), squaredSumvalue); } } else { rl.attributeMissingValues.addToValue(s, 1); } } }
From source file:moa.classifiers.WEKAClassifier.java
License:Open Source License
public double[] getVotesForInstance(Instance inst) { double[] votes = new double[inst.numClasses()]; if (isClassificationEnabled == false) { for (int i = 0; i < inst.numClasses(); i++) { votes[i] = 1.0 / inst.numClasses(); }/* w w w . j a v a2 s . c o m*/ } else { try { votes = this.classifier.distributionForInstance(inst); } catch (Exception e) { System.err.println(e.getMessage()); } } return votes; }