/** * Computes the sum of all the instances' weights. * * @return the sum of all the instances' weights as a double */ public/* @pure@ */double sumOfWeights() { double sum = 0; for (int i = 0; i < numInstances(); i++) { sum += instance(i).weight(); } return sum; }
/** * Shifts given instance from one bag to another one. * * @exception Exception if something goes wrong */ public final void shift(int from, int to, Instance instance) throws Exception { int classIndex; double weight; classIndex = (int) instance.classValue(); weight = instance.weight(); m_perClassPerBag[from][classIndex] -= weight; m_perClassPerBag[to][classIndex] += weight; m_perBag[from] -= weight; m_perBag[to] += weight; }
/** * Adds all instances in given range to given bag. * * @exception Exception if something goes wrong */ public final void addRange(int bagIndex, Instances source, int startIndex, int lastPlusOne) throws Exception { double sumOfWeights = 0; int classIndex; Instance instance; int i; for (i = startIndex; i < lastPlusOne; i++) { instance = source.instance(i); classIndex = (int) instance.classValue(); sumOfWeights = sumOfWeights + instance.weight(); m_perClassPerBag[bagIndex][classIndex] += instance.weight(); m_perClass[classIndex] += instance.weight(); } m_perBag[bagIndex] += sumOfWeights; totaL += sumOfWeights; }
/** * Computes the attribute means. */ protected double[] computeMeans(Instances insts) { // We can assume that all the attributes are numeric and that // we don't have any missing attribute values (including the class) double[] means = new double[insts.numAttributes()]; double[] counts = new double[insts.numAttributes()]; for (int j = 0; j < insts.numInstances(); j++) { Instance inst = insts.instance(j); for (int i = 0; i < insts.numAttributes(); i++) { means[i] += inst.weight() * inst.value(i); counts[i] += inst.weight(); } } for (int i = 0; i < insts.numAttributes(); i++) { if (counts[i] > 0) { means[i] /= counts[i]; } else { means[i] = 0.0; } } return means; }
/** * Convert a single instance over. Selected attributes only are transfered. * The converted instance is added to the end of the output queue. * * @param instance the instance to convert * @throws Exception if something goes wrong */ protected void convertInstance(Instance instance) throws Exception { double[] newVals = new double[getOutputFormat().numAttributes()]; if (m_ASEvaluator instanceof AttributeTransformer) { Instance tempInstance = ((AttributeTransformer) m_ASEvaluator).convertInstance(instance); for (int i = 0; i < m_SelectedAttributes.length; i++) { int current = m_SelectedAttributes[i]; newVals[i] = tempInstance.value(current); } } else { for (int i = 0; i < m_SelectedAttributes.length; i++) { int current = m_SelectedAttributes[i]; newVals[i] = instance.value(current); } } if (instance instanceof SparseInstance) { push(new SparseInstance(instance.weight(), newVals)); } else { push(new DenseInstance(instance.weight(), newVals)); } }
/** * Private function to compute default number of accurate instances in the * specified data for the consequent of the rule * * @param data the data in question * @return the default accuracy number */ private double computeDefAccu(Instances data) { double defAccu = 0; for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); if ((int) inst.classValue() == (int) m_Consequent) { defAccu += inst.weight(); } } return defAccu; }
/** * Tests a simple 2 class cost matrix with fixed costs * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixNoExpressions() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(0.5, weighted.instance(0).weight()); assertEquals(1.5, weighted.instance(1).weight()); }
/** * Updates the classifier with the given instance. * * @param instance the new training instance to include in the model * @exception Exception if the instance could not be incorporated in the * model. */ public void updateClassifier(Instance instance) throws Exception { if (!instance.classIsMissing()) { Enumeration<Attribute> enumAtts = m_Instances.enumerateAttributes(); int attIndex = 0; while (enumAtts.hasMoreElements()) { Attribute attribute = enumAtts.nextElement(); if (!instance.isMissing(attribute)) { m_Distributions[attIndex][(int) instance.classValue()].addValue( instance.value(attribute), instance.weight()); } attIndex++; } m_ClassDistribution.addValue(instance.classValue(), instance.weight()); } }
/** * Convert a single instance over. The converted instance is added to the end * of the output queue. * * @param instance the instance to convert * @throws Exception if something goes wrong */ protected void convertInstance(Instance instance) throws Exception { // Make copy and set weight to one Instance cp = (Instance) instance.copy(); cp.setWeight(1.0); // Set up values double[] instanceVals = new double[outputFormatPeek().numAttributes()]; double[] vals = m_partitionGenerator.getMembershipValues(cp); System.arraycopy(vals, 0, instanceVals, 0, vals.length); if (instance.classIndex() >= 0) { instanceVals[instanceVals.length - 1] = instance.classValue(); } push(new SparseInstance(instance.weight(), instanceVals)); }
@Override public MTask call() { for (int l = m_start; l < m_end; l++) { Instance in = m_inst.instance(l); for (int i = 0; i < m_num_clusters; i++) { for (int j = 0; j < m_num_attribs; j++) { if (m_inst.attribute(j).isNominal()) { m_taskModel[i][j].addValue(in.value(j), in.weight() * m_weights[l][i]); } else { m_taskModelNormal[i][j][0] += (in.value(j) * in.weight() * m_weights[l][i]); m_taskModelNormal[i][j][2] += in.weight() * m_weights[l][i]; m_taskModelNormal[i][j][1] += (in.value(j) * in.value(j) * in.weight() * m_weights[l][i]); } } } } // completedMTask(this, true); return this; } }
/** * converts a single instance to the required format * * @param instance the instance to convert * @return the converted instance */ protected Instance convertInstance(Instance instance) throws Exception { double vals[] = new double[outputFormatPeek().numAttributes()]; for (int j = 0; j < m_k; j++) { for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index != instance.classIndex()) { double value = instance.valueSparse(i); if (!Utils.isMissingValue(value)) { vals[j] += m_rmatrix[j][index] * value; } } else { vals[m_k] = instance.valueSparse(i); } } } return new DenseInstance(instance.weight(), vals); }
/** * Computes the mean vector for the given dataset. */ protected Vector computeMean(Instances data, double[] totalWeight, int aI) { Vector meanVector = new DenseVector(data.numAttributes() - 1); totalWeight[aI] = 0; for (Instance inst : data) { if (!inst.classIsMissing()) { meanVector.add(inst.weight(), instanceToVector(inst)); totalWeight[aI] += inst.weight(); } } meanVector.scale(1.0 / totalWeight[aI]); return meanVector; }
/** * Updates the classifier with information from one training instance. * * @param instance the instance to be incorporated * @throws Exception if the instance cannot be processed successfully. */ public void updateClassifier(Instance instance) throws Exception { double classValue = instance.value(instance.classIndex()); if (!Utils.isMissingValue(classValue)) { int classIndex = (int) classValue; m_probOfClass[classIndex] += instance.weight(); for (int a = 0; a < instance.numValues(); a++) { if (instance.index(a) != instance.classIndex()) { if (!instance.isMissingSparse(a)) { double numOccurrences = instance.valueSparse(a) * instance.weight(); if (numOccurrences < 0) throw new Exception("Numeric attribute values must all be greater or equal to zero."); m_wordsPerClass[classIndex] += numOccurrences; m_probOfWordGivenClass[classIndex][instance.index(a)] += numOccurrences; } } } } }
/** * Computes the attribute means. */ protected double[] computeMeans(Instances insts) { // We can assume that all the attributes are numeric and that // we don't have any missing attribute values (including the class) double[] means = new double[insts.numAttributes()]; double[] counts = new double[insts.numAttributes()]; for (int j = 0; j < insts.numInstances(); j++) { Instance inst = insts.instance(j); for (int i = 0; i < insts.numAttributes(); i++) { means[i] += inst.weight() * inst.value(i); counts[i] += inst.weight(); } } for (int i = 0; i < insts.numAttributes(); i++) { if (counts[i] > 0) { means[i] /= counts[i]; } else { means[i] = 0.0; } } return means; }
/** * Computes the sum of all the instances' weights. * * @return the sum of all the instances' weights as a double */ public/* @pure@ */double sumOfWeights() { double sum = 0; for (int i = 0; i < numInstances(); i++) { sum += instance(i).weight(); } return sum; }
/** * Updates the classifier with the given instance. * * @param bayesNet the bayes net to use * @param instance the new training instance to include in the model * @throws Exception if the instance could not be incorporated in the model. */ @Override public void updateClassifier(BayesNet bayesNet, Instance instance) throws Exception { for (int iAttribute = 0; iAttribute < bayesNet.m_Instances.numAttributes(); iAttribute++) { double iCPT = 0; for (int iParent = 0; iParent < bayesNet.getParentSet(iAttribute) .getNrOfParents(); iParent++) { int nParent = bayesNet.getParentSet(iAttribute).getParent(iParent); iCPT = iCPT * bayesNet.m_Instances.attribute(nParent).numValues() + instance.value(nParent); } bayesNet.m_Distributions[iAttribute][(int) iCPT].addValue( instance.value(iAttribute), instance.weight()); } } // updateClassifier
/** * Private function to compute default number of accurate instances in the * specified data for the consequent of the rule * * @param data the data in question * @return the default accuracy number */ private double computeDefAccu(Instances data) { double defAccu = 0; for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); if ((int) inst.classValue() == (int) m_Consequent) { defAccu += inst.weight(); } } return defAccu; }
/** * Shifts given instance from one bag to another one. * * @exception Exception if something goes wrong */ public final void shift(int from, int to, Instance instance) throws Exception { int classIndex; double weight; classIndex = (int) instance.classValue(); weight = instance.weight(); m_perClassPerBag[from][classIndex] -= weight; m_perClassPerBag[to][classIndex] += weight; m_perBag[from] -= weight; m_perBag[to] += weight; }
/** * Tests a simple 2 class cost matrix with fixed costs * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixNoExpressions() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(0.5, weighted.instance(0).weight()); assertEquals(1.5, weighted.instance(1).weight()); }