@SuppressWarnings("unchecked") @Override public Enumeration enumerateInstances() { return super.enumerateInstances(); }
/** * Constructor. */ public WekaObviousIntIterator(Instances instances) { this.enumInst = instances.enumerateInstances(); }
@SuppressWarnings("unchecked") @Override public int getRow() { Instances instances = instance.dataset(); Enumeration instEnum = instances.enumerateInstances(); int count = -1; while (instEnum.hasMoreElements()) { count++; if (instance.equals(instEnum.nextElement())) { return count; } } return -1; }
/** * Does E contain any examples in the class C? * * @param E the instances to be checked * @param C the class * @return true if there are any instances of class C * @throws Exception if something goes wrong */ private static boolean contains(Instances E, int C) throws Exception { Enumeration enu = E.enumerateInstances(); while (enu.hasMoreElements()) { if ((int) ((Instance) enu.nextElement()).classValue() == C) { return true; } } return false; }
/** * estimateCPTs estimates the conditional probability tables for the Bayes Net * using the network structure. * * @param bayesNet the bayes net to use * @throws Exception if something goes wrong */ @Override public void estimateCPTs(BayesNet bayesNet) throws Exception { initCPTs(bayesNet); // Compute counts Enumeration<Instance> enumInsts = bayesNet.m_Instances.enumerateInstances(); while (enumInsts.hasMoreElements()) { Instance instance = enumInsts.nextElement(); updateClassifier(bayesNet, instance); } } // estimateCPTs
/** * estimateCPTs estimates the conditional probability tables for the Bayes Net * using the network structure. * * @param bayesNet the bayes net to use * @throws Exception if something goes wrong */ @Override public void estimateCPTs(BayesNet bayesNet) throws Exception { initCPTs(bayesNet); // Compute counts Enumeration<Instance> enumInsts = bayesNet.m_Instances.enumerateInstances(); while (enumInsts.hasMoreElements()) { Instance instance = enumInsts.nextElement(); updateClassifier(bayesNet, instance); } } // estimateCPTs
/** * Constructor that takes instances and the classification. * * @param data the instances * @param cl the class * @exception Exception if something goes wrong */ public PrismRule(Instances data, int cl) throws Exception { m_instances = data; m_classification = cl; m_test = null; m_next = null; m_errors = 0; Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { if ((int) ((Instance) enu.nextElement()).classValue() != cl) { m_errors++; } } m_instances = new Instances(m_instances, 0); }
/** * Creates a distribution with only one bag according to instances in source. * * @exception Exception if something goes wrong */ public Distribution(Instances source) throws Exception { m_perClassPerBag = new double[1][0]; m_perBag = new double[1]; totaL = 0; m_perClass = new double[source.numClasses()]; m_perClassPerBag[0] = new double[source.numClasses()]; Enumeration<Instance> enu = source.enumerateInstances(); while (enu.hasMoreElements()) { add(0, enu.nextElement()); } }
/** * Creates a distribution with only one bag according to instances in source. * * @exception Exception if something goes wrong */ public Distribution(Instances source) throws Exception { m_perClassPerBag = new double[1][0]; m_perBag = new double[1]; totaL = 0; m_perClass = new double[source.numClasses()]; m_perClassPerBag[0] = new double[source.numClasses()]; Enumeration<Instance> enu = source.enumerateInstances(); while (enu.hasMoreElements()) { add(0, enu.nextElement()); } }
/** * Returns the set of instances that are covered by this rule. * * @param data the instances to be checked * @return the instances covered */ public Instances coveredBy(Instances data) { Instances r = new Instances(data, data.numInstances()); Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { Instance i = (Instance) enu.nextElement(); if (resultRule(i) != -1) { r.add(i); } } r.compactify(); return r; }
/** * Returns the set of instances that are not covered by this rule. * * @param data the instances to be checked * @return the instances not covered */ public Instances notCoveredBy(Instances data) { Instances r = new Instances(data, data.numInstances()); Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { Instance i = (Instance) enu.nextElement(); if (resultRule(i) == -1) { r.add(i); } } r.compactify(); return r; }
/** * 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; }
public static void buildEstimator(Estimator est, Instances instances, int attrIndex, int classIndex, int classValueIndex, boolean isIncremental) throws Exception { // DBO.pln("buildEstimator 2 " + classValueIndex); // non-incremental estimator add all instances at once if (!isIncremental) { if (classValueIndex == -1) { // DBO.pln("before addValues -- Estimator"); est.addValues(instances, attrIndex); } else { // DBO.pln("before addValues with classvalue -- Estimator"); est.addValues(instances, attrIndex, classIndex, classValueIndex); } } else { // incremental estimator, read one value at a time Enumeration<Instance> enumInsts = (instances).enumerateInstances(); while (enumInsts.hasMoreElements()) { Instance instance = enumInsts.nextElement(); ((IncrementalEstimator) est).addValue(instance.value(attrIndex), instance.weight()); } } }
/** * Sets the weights for the next iteration. * * @param training the training instances * @param reweight the reweighting factor * @throws Exception if something goes wrong */ protected void setWeights(Instances training, double reweight) throws Exception { double oldSumOfWeights, newSumOfWeights; oldSumOfWeights = training.sumOfWeights(); Enumeration<Instance> enu = training.enumerateInstances(); while (enu.hasMoreElements()) { Instance instance = enu.nextElement(); if (!Utils.eq( m_Classifiers[m_NumIterationsPerformed].classifyInstance(instance), instance.classValue())) { instance.setWeight(instance.weight() * reweight); } } // Renormalize weights newSumOfWeights = training.sumOfWeights(); enu = training.enumerateInstances(); while (enu.hasMoreElements()) { Instance instance = enu.nextElement(); instance.setWeight(instance.weight() * oldSumOfWeights / newSumOfWeights); } }
public static void buildEstimator(Estimator est, Instances instances, int attrIndex, int classIndex, int classValueIndex, boolean isIncremental) throws Exception { // DBO.pln("buildEstimator 2 " + classValueIndex); // non-incremental estimator add all instances at once if (!isIncremental) { if (classValueIndex == -1) { // DBO.pln("before addValues -- Estimator"); est.addValues(instances, attrIndex); } else { // DBO.pln("before addValues with classvalue -- Estimator"); est.addValues(instances, attrIndex, classIndex, classValueIndex); } } else { // incremental estimator, read one value at a time Enumeration<Instance> enumInsts = (instances).enumerateInstances(); while (enumInsts.hasMoreElements()) { Instance instance = enumInsts.nextElement(); ((IncrementalEstimator) est).addValue(instance.value(attrIndex), instance.weight()); } } }
/** * Splits a dataset according to the values of a nominal attribute. * * @param data the data which is to be split * @param att the attribute to be used for splitting * @return the sets of instances produced by the split */ private Instances[] splitData(Instances data, Attribute att) { Instances[] splitData = new Instances[att.numValues()]; for (int j = 0; j < att.numValues(); j++) { splitData[j] = new Instances(data, data.numInstances()); } Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); splitData[(int) inst.value(att)].add(inst); } for (int i = 0; i < splitData.length; i++) { splitData[i].compactify(); } return splitData; }
/** * 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()); }
/** * Creates split on enumerated attribute. * * @exception Exception if something goes wrong */ private void handleEnumeratedAttribute(Instances trainInstances) throws Exception { Instance instance; m_distribution = new Distribution(m_complexityIndex, trainInstances.numClasses()); // Only Instances with known values are relevant. Enumeration<Instance> enu = trainInstances.enumerateInstances(); while (enu.hasMoreElements()) { instance = enu.nextElement(); if (!instance.isMissing(m_attIndex)) { m_distribution.add((int) instance.value(m_attIndex), instance); } } // Check if minimum number of Instances in at least two // subsets. if (m_distribution.check(m_minNoObj)) { m_numSubsets = m_complexityIndex; m_infoGain = infoGainCrit.splitCritValue(m_distribution, m_sumOfWeights); m_gainRatio = gainRatioCrit.splitCritValue(m_distribution, m_sumOfWeights, m_infoGain); } }
/** * Generates the classifier. * * @param instances set of instances serving as training data * @throws Exception if the classifier has not been generated successfully */ public void buildClassifier(Instances instances) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(instances); // remove instances with missing class instances = new Instances(instances); instances.deleteWithMissingClass(); m_Train = new Instances(instances, 0, instances.numInstances()); m_MinArray = new double [m_Train.numAttributes()]; m_MaxArray = new double [m_Train.numAttributes()]; for (int i = 0; i < m_Train.numAttributes(); i++) { m_MinArray[i] = m_MaxArray[i] = Double.NaN; } Enumeration enu = m_Train.enumerateInstances(); while (enu.hasMoreElements()) { updateMinMax((Instance) enu.nextElement()); } }
private double calcNodeScorePlain(int nNode) { Instances instances = m_BayesNet.m_Instances; ParentSet oParentSet = m_BayesNet.getParentSet(nNode); // determine cardinality of parent set & reserve space for frequency counts int nCardinality = oParentSet.getCardinalityOfParents(); int numValues = instances.attribute(nNode).numValues(); int[] nCounts = new int[nCardinality * numValues]; // initialize (don't need this?) for (int iParent = 0; iParent < nCardinality * numValues; iParent++) { nCounts[iParent] = 0; } // estimate distributions Enumeration<Instance> enumInsts = instances.enumerateInstances(); while (enumInsts.hasMoreElements()) { Instance instance = enumInsts.nextElement(); // updateClassifier; double iCPT = 0; for (int iParent = 0; iParent < oParentSet.getNrOfParents(); iParent++) { int nParent = oParentSet.getParent(iParent); iCPT = iCPT * instances.attribute(nParent).numValues() + instance.value(nParent); } nCounts[numValues * ((int) iCPT) + (int) instance.value(nNode)]++; } return calcScoreOfCounts(nCounts, nCardinality, numValues, instances); } // CalcNodeScore