@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; //< inst.classIndex() ? this.attIndex //: this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : (int) inst.value(instAttIndex); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; //< inst.classIndex() ? this.attIndex //: this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : (int) inst.value(instAttIndex); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; // < inst.classIndex() ? this.attIndex // : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.value(instAttIndex); if (v == this.attValue) { return this.equalsPassesTest ? 0 : 1; } return v < this.attValue ? 0 : 1; }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; //< inst.classIndex() ? this.attIndex //: this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : (int) inst.value(instAttIndex); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; // < inst.classIndex() ? this.attIndex // : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.value(instAttIndex); if (v == this.attValue) { return this.equalsPassesTest ? 0 : 1; } return v < this.attValue ? 0 : 1; }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex ; // < inst.classIndex() ? this.attIndex // : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.valueInputAttribute(instAttIndex); // if the attIndex is not calculated above this is the correct method call if (v == this.attValue) { return this.equalsPassesTest ? 0 : 1; } return v < this.attValue ? 0 : 1; }
/** * Test if an instance is within the given ranges. * * @param instance the instance * @param ranges the ranges the instance is tested to be in * @return true if instance is within the ranges */ public boolean inRanges(Instance instance, double[][] ranges) { boolean isIn = true; // updateRangesFirst must have been called on ranges for (int j = 0; isIn && (j < ranges.length); j++) { if (!instance.isMissing(j)) { double value = instance.value(j); isIn = value <= ranges[j][R_MAX]; if (isIn) isIn = value >= ranges[j][R_MIN]; } } return isIn; }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : ((int) inst.value(instAttIndex) == this.attValue ? 0 : 1); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : ((int) inst.value(instAttIndex) == this.attValue ? 0 : 1); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; return inst.isMissing(instAttIndex) ? -1 : ((int) inst.value(instAttIndex) == this.attValue ? 0 : 1); }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.value(instAttIndex); int ret = 0; switch (this.operator) { case 0: ret = (v == this.attValue) ? 0 : 1; break; case 1: ret = (v <= this.attValue) ? 0 : 1; break; case 2: ret = (v > this.attValue) ? 0 : 1; } return ret; }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.value(instAttIndex); int ret = 0; switch (this.operator) { case 0: ret = (v == this.attValue) ? 0 : 1; break; case 1: ret = (v <= this.attValue) ? 0 : 1; break; case 2: ret = (v > this.attValue) ? 0 : 1; } return ret; }
@Override public int branchForInstance(Instance inst) { int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1; if (inst.isMissing(instAttIndex)) { return -1; } double v = inst.value(instAttIndex); int ret = 0; switch (this.operator) { case 0: ret = (v == this.attValue) ? 0 : 1; break; case 1: ret = (v <= this.attValue) ? 0 : 1; break; case 2: ret = (v > this.attValue) ? 0 : 1; } return ret; }
/** * Used to initialize the ranges. For this the values of the first * instance is used to save time. * Sets low and high to the values of the first instance and * width to zero. * * @param instance the new instance * @param numAtt number of attributes in the model * @param ranges low, high and width values for all attributes */ public void updateRangesFirst(Instance instance, int numAtt, double[][] ranges) { for (int j = 0; j < numAtt; j++) { if (!instance.isMissing(j)) { ranges[j][R_MIN] = instance.value(j); ranges[j][R_MAX] = instance.value(j); ranges[j][R_WIDTH] = 0.0; } else { // if value was missing ranges[j][R_MIN] = Double.POSITIVE_INFINITY; ranges[j][R_MAX] = -Double.POSITIVE_INFINITY; ranges[j][R_WIDTH] = Double.POSITIVE_INFINITY; } } }
/** * Updates the ranges given a new instance. * * @param instance the new instance * @param ranges low, high and width values for all attributes * @return the updated ranges */ public double[][] updateRanges(Instance instance, double[][] ranges) { // updateRangesFirst must have been called on ranges for (int j = 0; j < ranges.length; j++) { double value = instance.value(j); if (!instance.isMissing(j)) { if (value < ranges[j][R_MIN]) { ranges[j][R_MIN] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } else { if (instance.value(j) > ranges[j][R_MAX]) { ranges[j][R_MAX] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } } } } return ranges; }
public double totalSize(Instance instance) { int classIndex = instance.classIndex(); double total = 0.0; for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index == classIndex || instance.isMissing(i)) { continue; } double count = instance.valueSparse(i); if (count >= 0) { total += count; } else { //throw new Exception("Numeric attribute value is not >= 0. " + i + " " + index + " " + // instance.valueSparse(i) + " " + " " + instance); } } return total; }
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance instance) { if (this.reset == true) { return new double[2]; } double[] probOfClassGivenDoc = new double[m_numClasses]; double totalSize = totalSize(instance); for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]); } for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index == instance.classIndex() || instance.isMissing(i)) { continue; } double wordCount = instance.valueSparse(i); for (int c = 0; c < m_numClasses; c++) { double value = m_wordTotalForClass[c].getValue(index); probOfClassGivenDoc[c] += wordCount * Math.log(value == 0 ? this.laplaceCorrectionOption.getValue() : value ); } } return Utils.logs2probs(probOfClassGivenDoc); }
public void theBestAttributes(Instance instance, AutoExpandVector<AttributeClassObserver> observersParameter) { for(int z = 0; z < instance.numAttributes() - 1; z++){ if(!instance.isMissing(z)){ int instAttIndex = modelAttIndexToInstanceAttIndex(z, instance); ArrayList<Double> attribBest = new ArrayList<Double>();
public static double[] doNaiveBayesPredictionLog(Instance inst, DoubleVector observedClassDistribution, AutoExpandVector<AttributeClassObserver> observers, AutoExpandVector<AttributeClassObserver> observers2) { AttributeClassObserver obs; double[] votes = new double[observedClassDistribution.numValues()]; double observedClassSum = observedClassDistribution.sumOfValues(); for (int classIndex = 0; classIndex < votes.length; classIndex++) { votes[classIndex] = Math.log10(observedClassDistribution.getValue(classIndex) / observedClassSum); for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) { int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst); if (inst.attribute(instAttIndex).isNominal()) { obs = observers.get(attIndex); } else { obs = observers2.get(attIndex); } if ((obs != null) && !inst.isMissing(instAttIndex)) { votes[classIndex] += Math.log10(obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex), classIndex)); } } } return votes; }
public static double[] doNaiveBayesPrediction(Instance inst, DoubleVector observedClassDistribution, AutoExpandVector<AttributeClassObserver> attributeObservers) { double[] votes = new double[observedClassDistribution.numValues()]; double observedClassSum = observedClassDistribution.sumOfValues(); for (int classIndex = 0; classIndex < votes.length; classIndex++) { votes[classIndex] = observedClassDistribution.getValue(classIndex) / observedClassSum; for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) { int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst); AttributeClassObserver obs = attributeObservers.get(attIndex); if ((obs != null) && !inst.isMissing(instAttIndex)) { votes[classIndex] *= obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex), classIndex); } } } // TODO: need logic to prevent underflow? return votes; }