private double predictUsingCurtailment(Example example, ExtendedTree node) { double dPrediction = 0; if (node.isLeaf()) { dPrediction = predictAsLeaf(example, node); } else { boolean bEdgeConditionFound = false; Iterator<Edge> childIterator = node.childIterator(); while ((childIterator.hasNext()) && (bEdgeConditionFound == false)) { Edge edge = childIterator.next(); SplitCondition condition = edge.getCondition(); if (condition.test(example)) { bEdgeConditionFound = true; ExtendedTree childTree = (ExtendedTree) edge.getChild(); if (childTree.getTotalExamples() >= dCurtailmentThreshold) dPrediction = (predictUsingCurtailment(example, childTree)); else dPrediction = predictAsLeaf(example, node); } } if (bEdgeConditionFound == false) { dPrediction = predictAsLeaf(example, node); } } return dPrediction; }
@Override public String getEdgeName(String object) { SplitCondition condition = edgeMap.get(object); if (condition != null) { return condition.getRelation() + " " + condition.getValueString(); } else { return null; } }
@Override public String getVertexName(String object) { Tree node = vertexMap.get(object); String name = ""; if (node != null) { if (node.isLeaf()) { name = node.getLabel(); } else { Iterator<Edge> e = node.childIterator(); while (e.hasNext()) { SplitCondition condition = e.next().getCondition(); name = condition.getAttributeName(); break; } } } return name; }
@Override public Model learn(ExampleSet exampleSet) throws OperatorException { Attribute label = exampleSet.getAttributes().getLabel(); RuleModel ruleModel = new RuleModel(exampleSet); double pureness = getParameterAsDouble(PARAMETER_PURENESS); TermDetermination termDetermination = new TermDetermination(new AccuracyCriterion(), 0.5d); ExampleSet trainingSet = (ExampleSet) exampleSet.clone(); for (String labelName : label.getMapping().getValues()) { trainingSet.recalculateAttributeStatistics(label); int oldSize = -1; while (trainingSet.size() > 0 && trainingSet.size() != oldSize && trainingSet.getStatistics(label, Statistics.COUNT, labelName) > 0) { Rule rule = new Rule(labelName); ExampleSet oldTrainingSet = (ExampleSet) trainingSet.clone(); // grow rule int growOldSize = -1; ExampleSet growSet = (ExampleSet) trainingSet.clone(); while (growSet.size() > 0 && growSet.size() != growOldSize && !rule.isPure(growSet, pureness) && growSet.getAttributes().size() > 0) { SplitCondition term = termDetermination.getBestTerm(growSet, labelName); if (term == null) { break; } rule.addTerm(term); Attribute splitAttribute = growSet.getAttributes().get(term.getAttributeName()); growSet.getAttributes().remove(splitAttribute); growOldSize = growSet.size(); growSet = rule.getCovered(growSet); } // add rule if not empty if (rule.getTerms().size() > 0) { growSet = rule.getCovered(trainingSet); growSet.recalculateAttributeStatistics(label); int[] frequencies = new int[label.getMapping().size()]; int counter = 0; for (String value : label.getMapping().getValues()) { frequencies[counter++] = (int) growSet.getStatistics(label, Statistics.COUNT, value); } rule.setFrequencies(frequencies); ruleModel.addRule(rule); oldSize = trainingSet.size(); trainingSet = rule.removeCovered(oldTrainingSet); } else { break; // no other terms found for this class --> next class } trainingSet.recalculateAttributeStatistics(label); } checkForStop(); } // training set not empty? add default rule if (trainingSet.size() > 0) { trainingSet.recalculateAttributeStatistics(label); int index = (int) trainingSet.getStatistics(label, Statistics.MODE); String defaultLabel = label.getMapping().mapIndex(index); Rule defaultRule = new Rule(defaultLabel); int[] frequencies = new int[label.getMapping().size()]; int counter = 0; for (String value : label.getMapping().getValues()) { frequencies[counter++] = (int) trainingSet.getStatistics(label, Statistics.COUNT, value); } defaultRule.setFrequencies(frequencies); ruleModel.addRule(defaultRule); } return ruleModel; }