/** * 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; }
/** * Note: for Nominal Class Only! * Returns an array of the class values * * @return an array of class values */ private int [] classValues() { int [] classval = new int[m_NumInstances]; for (int i=0; i < m_NumInstances; i++) { try { classval[i] = (int)m_Train.instance(i).classValue(); } catch (Exception ex) { ex.printStackTrace(); } } return classval; }
/** * Computes the Y-values (actual class probabilities) for a set of instances. * * @param data the data to compute the Y-values from * @return the Y-values */ protected double[][] getYs(Instances data) { double[][] dataYs = new double[data.numInstances()][m_numClasses]; for (int j = 0; j < m_numClasses; j++) { for (int k = 0; k < data.numInstances(); k++) { dataYs[k][j] = (data.instance(k).classValue() == j) ? 1.0 : 0.0; } } return dataYs; }
/** * 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 all instances in given range from one bag to another one. * * @exception Exception if something goes wrong */ public final void shiftRange(int from, int to, Instances source, int startIndex, int lastPlusOne) throws Exception { int classIndex; double weight; Instance instance; int i; for (i = startIndex; i < lastPlusOne; i++) { instance = source.instance(i); 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; } }
/** * The constructor. */ public Bag(Instance bagInstance) { instances = bagInstance.relationalValue(1); positive = bagInstance.classValue() == 1.0; bagWeight = 1.0 / instances.numInstances(); bagWeightMultiplied = bagWeight; id = bagInstance.stringValue(0); }
/** * Computes the Y-values (actual class probabilities) for a set of instances. * * @param data the data to compute the Y-values from * @return the Y-values */ protected double[][] getYs(Instances data) { double[][] dataYs = new double[data.numInstances()][m_numClasses]; for (int j = 0; j < m_numClasses; j++) { for (int k = 0; k < data.numInstances(); k++) { dataYs[k][j] = (data.instance(k).classValue() == j) ? 1.0 : 0.0; } } return dataYs; }
/** * 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; }
/** * Shifts all instances in given range from one bag to another one. * * @exception Exception if something goes wrong */ public final void shiftRange(int from, int to, Instances source, int startIndex, int lastPlusOne) throws Exception { int classIndex; double weight; Instance instance; int i; for (i = startIndex; i < lastPlusOne; i++) { instance = source.instance(i); 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; } }
/** * Classifies the given test instance. * * @param instance the instance to be classified * @return the predicted class for the instance * @throws Exception if the instance can't be classified */ public double classifyInstance(Instance instance) throws Exception { if (m_Train.numInstances() == 0) { throw new Exception("No training instances!"); } double distance, minDistance = Double.MAX_VALUE, classValue = 0; updateMinMax(instance); Enumeration enu = m_Train.enumerateInstances(); while (enu.hasMoreElements()) { Instance trainInstance = (Instance) enu.nextElement(); if (!trainInstance.classIsMissing()) { distance = distance(instance, trainInstance); if (distance < minDistance) { minDistance = distance; classValue = trainInstance.classValue(); } } } return classValue; }
/** * Note: for Nominal Class Only! * Returns an array of the class values * * @return an array of class values */ private int [] classValues() { int [] classval = new int[m_NumInstances]; for (int i=0; i < m_NumInstances; i++) { try { classval[i] = (int)m_Train.instance(i).classValue(); } catch (Exception ex) { ex.printStackTrace(); } } return classval; }
/** * returns the data class column as matrix * * @param instances the data to work on * @return the class attribute */ protected Matrix getY(Instances instances) { double[][] y; Matrix result; int i; y = new double[instances.numInstances()][1]; for (i = 0; i < instances.numInstances(); i++) { y[i][0] = instances.instance(i).classValue(); } result = new Matrix(y); return result; }
/** * Private function to compute number of accurate instances based on the * specified predicted class * * @param data the data in question * @param clas the predicted class * @return the default accuracy number */ private double computeAccu(Instances data, int clas) { double accu = 0; for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); if ((int) inst.classValue() == clas) { accu += inst.weight(); } } return accu; }
/** * Deletes given instance from given bag. * * @exception Exception if something goes wrong */ public final void del(int bagIndex, Instance instance) throws Exception { int classIndex; double weight; classIndex = (int) instance.classValue(); weight = instance.weight(); m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex] - weight; m_perBag[bagIndex] = m_perBag[bagIndex] - weight; m_perClass[classIndex] = m_perClass[classIndex] - weight; totaL = totaL - 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 entropy of a dataset. * * @param data the data for which entropy is to be computed * @return the entropy of the data's class distribution * @throws Exception if computation fails */ private double computeEntropy(Instances data) throws Exception { double [] classCounts = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); classCounts[(int) inst.classValue()]++; } double entropy = 0; for (int j = 0; j < data.numClasses(); j++) { if (classCounts[j] > 0) { entropy -= classCounts[j] * Utils.log2(classCounts[j]); } } entropy /= (double) data.numInstances(); return entropy + Utils.log2(data.numInstances()); }
/** * Calculate the squared error of a regression model on the training data * * @param selectedAttributes an array of flags indicating which attributes are * included in the regression model * @param coefficients an array of coefficients for the regression model * @return the mean squared error on the training data * @throws Exception if there is a missing class value in the training data */ protected double calculateSE(boolean[] selectedAttributes, double[] coefficients) throws Exception { double mse = 0; for (int i = 0; i < m_TransformedData.numInstances(); i++) { double prediction = regressionPrediction(m_TransformedData.instance(i), selectedAttributes, coefficients); double error = prediction - m_TransformedData.instance(i).classValue(); mse += error * error; } return mse; }
/** * Private function to compute the squared error of the specified data and the * specified mean * * @param data the data in question * @param mean the specified mean * @return the default mean-squared error */ private double meanSquaredError(Instances data, double mean) { if (Utils.eq(data.sumOfWeights(), 0.0)) { return 0; } double mSqErr = 0, sum = data.sumOfWeights(); for (int i = 0; i < data.numInstances(); i++) { Instance datum = data.instance(i); mSqErr += datum.weight() * (datum.classValue() - mean) * (datum.classValue() - mean); } return (mSqErr / sum); }
/** * Subtracts given instance from given bag. * * @exception Exception if something goes wrong */ public final void sub(int bagIndex, Instance instance) throws Exception { int classIndex; double weight; classIndex = (int) instance.classValue(); weight = instance.weight(); m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex] - weight; m_perBag[bagIndex] = m_perBag[bagIndex] - weight; m_perClass[classIndex] = m_perClass[classIndex] - weight; totaL = totaL - weight; }