/** * Method that finds all class association rules. * * @throws Exception if an attribute is numeric */ private void findCarRulesQuickly() throws Exception { ArrayList<Object>[] rules; // Build rules for (int j = 0; j < m_Ls.size(); j++) { ArrayList<Object> currentLabeledItemSets = m_Ls.get(j); Enumeration<Object> enumLabeledItemSets = new WekaEnumeration<Object>( currentLabeledItemSets); while (enumLabeledItemSets.hasMoreElements()) { LabeledItemSet currentLabeledItemSet = (LabeledItemSet) enumLabeledItemSets .nextElement(); rules = currentLabeledItemSet.generateRules(m_minMetric, false); for (int k = 0; k < rules[0].size(); k++) { m_allTheRules[0].add(rules[0].get(k)); m_allTheRules[1].add(rules[1].get(k)); m_allTheRules[2].add(rules[2].get(k)); } } } }
public TreeNode getChildAt(int index, boolean filterIsActive) { if (!filterIsActive) { return super.getChildAt(index); } if (children == null) { throw new ArrayIndexOutOfBoundsException("node has no children"); } int realIndex = -1; int visibleIndex = -1; @SuppressWarnings("unchecked") Enumeration<InvisibleNode> e = new WekaEnumeration<InvisibleNode>( children); while (e.hasMoreElements()) { InvisibleNode node = e.nextElement(); if (node.isVisible()) { visibleIndex++; } realIndex++; if (visibleIndex == index) { return (TreeNode) children.elementAt(realIndex); } } throw new ArrayIndexOutOfBoundsException("index unmatched"); }
public int getChildCount(boolean filterIsActive) { if (!filterIsActive) { return super.getChildCount(); } if (children == null) { return 0; } int count = 0; @SuppressWarnings("unchecked") Enumeration<InvisibleNode> e = new WekaEnumeration<InvisibleNode>( children); while (e.hasMoreElements()) { InvisibleNode node = e.nextElement(); if (node.isVisible()) { count++; } } return count; }
/** * Tries to classify an example. * * @param example the example to classify * @return true if it could be classified * @throws Exception if something goes wrong */ private boolean classifyExample(Instance example) throws Exception { double classValue = -1.0; for (Enumeration<RuleList> e = new WekaEnumeration<RuleList>(m_DecisionList); e .hasMoreElements() && classValue < 0.0;) { RuleList rl = e.nextElement(); classValue = rl.classifyInstance(example); } if (classValue >= 0.0) { example.setClassValue(classValue); return true; } else { return false; } }
/** * Updates counter of a specific item set * * @param itemSets an item sets * @param instancesNoClass instances without the class attribute * @param instancesClass the values of the class attribute sorted according to * instances */ public static void upDateCounters(ArrayList<Object> itemSets, Instances instancesNoClass, Instances instancesClass) { for (int i = 0; i < instancesNoClass.numInstances(); i++) { Enumeration<Object> enu = new WekaEnumeration<Object>(itemSets); while (enu.hasMoreElements()) { ((LabeledItemSet) enu.nextElement()).upDateCounter( instancesNoClass.instance(i), instancesClass.instance(i)); } } }
/** * Updates counter of a specific item set * * @param itemSets an item sets * @param instancesNoClass instances without the class attribute * @param instancesClass the values of the class attribute sorted according to * instances */ public static void upDateCountersTreatZeroAsMissing( ArrayList<LabeledItemSet> itemSets, Instances instancesNoClass, Instances instancesClass) { for (int i = 0; i < instancesNoClass.numInstances(); i++) { Enumeration<LabeledItemSet> enu = new WekaEnumeration<LabeledItemSet>( itemSets); while (enu.hasMoreElements()) { enu.nextElement().upDateCounterTreatZeroAsMissing( instancesNoClass.instance(i), instancesClass.instance(i)); } } }
/** * Method that finds all association rules and performs significance test. * * @throws Exception if an attribute is numeric */ private void findRulesBruteForce() throws Exception { ArrayList<Object>[] rules; // Build rules for (int j = 1; j < m_Ls.size(); j++) { ArrayList<Object> currentItemSets = m_Ls.get(j); Enumeration<Object> enumItemSets = new WekaEnumeration<Object>( currentItemSets); while (enumItemSets.hasMoreElements()) { AprioriItemSet currentItemSet = (AprioriItemSet) enumItemSets .nextElement(); // AprioriItemSet currentItemSet = new // AprioriItemSet((ItemSet)enumItemSets.nextElement()); rules = currentItemSet.generateRulesBruteForce(m_minMetric, m_metricType, m_hashtables, j + 1, m_instances.numInstances(), m_significanceLevel); for (int k = 0; k < rules[0].size(); k++) { m_allTheRules[0].add(rules[0].get(k)); m_allTheRules[1].add(rules[1].get(k)); m_allTheRules[2].add(rules[2].get(k)); m_allTheRules[3].add(rules[3].get(k)); m_allTheRules[4].add(rules[4].get(k)); m_allTheRules[5].add(rules[5].get(k)); } } } }
/** * Method that finds all association rules. * * @throws Exception if an attribute is numeric */ private void findRulesQuickly() throws Exception { ArrayList<Object>[] rules; // Build rules for (int j = 1; j < m_Ls.size(); j++) { ArrayList<Object> currentItemSets = m_Ls.get(j); Enumeration<Object> enumItemSets = new WekaEnumeration<Object>( currentItemSets); while (enumItemSets.hasMoreElements()) { AprioriItemSet currentItemSet = (AprioriItemSet) enumItemSets .nextElement(); // AprioriItemSet currentItemSet = new // AprioriItemSet((ItemSet)enumItemSets.nextElement()); rules = currentItemSet.generateRules(m_minMetric, m_hashtables, j + 1); for (int k = 0; k < rules[0].size(); k++) { m_allTheRules[0].add(rules[0].get(k)); m_allTheRules[1].add(rules[1].get(k)); m_allTheRules[2].add(rules[2].get(k)); if (rules.length > 3) { m_allTheRules[3].add(rules[3].get(k)); m_allTheRules[4].add(rules[4].get(k)); m_allTheRules[5].add(rules[5].get(k)); } } } } }
/** * Updates counters for a set of item sets and a set of instances. * * @param itemSets the set of item sets which are to be updated * @param instances the instances to be used for updating the counters */ public static void upDateCounters(ArrayList<Object> itemSets, Instances instances) { for (int i = 0; i < instances.numInstances(); i++) { Enumeration<Object> enu = new WekaEnumeration<Object>(itemSets); while (enu.hasMoreElements()) { ((ItemSet) enu.nextElement()).upDateCounter(instances.instance(i)); } } }
/** * Updates counters for a set of item sets and a set of instances. * * @param itemSets the set of item sets which are to be updated * @param instances the instances to be used for updating the counters */ public static void upDateCountersTreatZeroAsMissing( ArrayList<Object> itemSets, Instances instances) { for (int i = 0; i < instances.numInstances(); i++) { Enumeration<Object> enu = new WekaEnumeration<Object>(itemSets); while (enu.hasMoreElements()) { ((ItemSet) enu.nextElement()).updateCounterTreatZeroAsMissing(instances .instance(i)); } } }
/** * Generate all examples of the dataset. * * @param random the random number generator to use * @param format the dataset format * @return the instance generated * @throws Exception if format not defined */ public Instances generateExamples(Random random, Instances format) throws Exception { Instance example = null; if (format == null) { throw new Exception("Dataset format not defined."); } // generate examples for one cluster after another int cNum = 0; for (Enumeration<Cluster> enm = new WekaEnumeration<Cluster>(m_ClusterList); enm .hasMoreElements(); cNum++) { Cluster cl = enm.nextElement(); double stdDev = cl.getStdDev(); int instNum = cl.getInstNum(); double[] center = cl.getCenter(); String cName = "c" + cNum; for (int i = 0; i < instNum; i++) { // generate example example = generateInstance(format, random, stdDev, center, cName); if (example != null) { example.setDataset(format); } format.add(example); } } return (format); }
/** * Compiles documentation about the data generation before the generation * process * * @return string with additional information */ @Override public String generateStart() { StringBuffer docu = new StringBuffer(); int sumInst = 0; int cNum = 0; for (Enumeration<Cluster> enm = new WekaEnumeration<Cluster>(m_ClusterList); enm .hasMoreElements(); cNum++) { Cluster cl = enm.nextElement(); docu.append("%\n"); docu.append("% Cluster: c" + cNum + "\n"); docu.append("% ----------------------------------------------\n"); docu.append("% StandardDeviation: " + Utils.doubleToString(cl.getStdDev(), 2) + "\n"); docu.append("% Number of instances: " + cl.getInstNum() + "\n"); sumInst += cl.getInstNum(); double[] center = cl.getCenter(); docu.append("% "); for (int i = 0; i < center.length - 1; i++) { docu.append(Utils.doubleToString(center[i], 2) + ", "); } docu.append(Utils.doubleToString(center[center.length - 1], 2) + "\n"); } docu.append("%\n% ----------------------------------------------\n"); docu.append("% Total number of instances: " + sumInst + "\n"); docu.append("% in " + cNum + " clusters\n"); docu.append("% Pattern chosen : "); if (m_Pattern == GRID) { docu.append("GRID, " + "distance multiplier = " + Utils.doubleToString(m_DistMult, 2) + "\n"); } else if (m_Pattern == SINE) { docu.append("SINE\n"); } else { docu.append("RANDOM\n"); } return docu.toString(); }
/** * classifies the given example * * @param example the instance to classify * @return the classification * @throws Exception if classification fails */ private double classifyInstance(Instance example) throws Exception { boolean passedAllTests = true; for (Enumeration<Test> e = new WekaEnumeration<Test>(m_RuleList); passedAllTests && e.hasMoreElements();) { Test test = e.nextElement(); passedAllTests = test.passesTest(example); } if (passedAllTests) { return m_ClassValue; } else { return -1.0; } }
/** * Classify example with maximum vote the following way. With every rule in * the decisionlist, it is evaluated if the given instance could be the class * of the rule. Finally the class value that receives the highest number of * votes is assigned to the example. * * @param example example to be reclassified * @return instance with new class value * @throws Exception if classification fails */ private Instance votedReclassifyExample(Instance example) throws Exception { int classVotes[] = new int[getNumClasses()]; for (int i = 0; i < classVotes.length; i++) { classVotes[i] = 0; } for (Enumeration<RuleList> e = new WekaEnumeration<RuleList>(m_DecisionList); e .hasMoreElements();) { RuleList rl = e.nextElement(); int classValue = (int) rl.classifyInstance(example); if (classValue >= 0) { classVotes[classValue]++; } } int maxVote = 0; int vote = -1; for (int i = 0; i < classVotes.length; i++) { if (classVotes[i] > maxVote) { maxVote = classVotes[i]; vote = i; } } if (vote >= 0) { example.setClassValue(vote); } else { throw new Exception("Error in instance classification."); } return example; }