private RuleModel createNumericalRuleModel(ExampleSet trainingSet, Attribute attribute) { RuleModel model = new RuleModel(trainingSet); // split by best attribute int oldSize = -1; while ((trainingSet.size() > 0) && (trainingSet.size() != oldSize)) { ExampleSet exampleSet = (ExampleSet) trainingSet.clone(); Split bestSplit = splitter.getBestSplit(exampleSet, attribute, null); double bestSplitValue = bestSplit.getSplitPoint(); if (!Double.isNaN(bestSplitValue)) { SplittedExampleSet splittedSet = SplittedExampleSet.splitByAttribute(exampleSet, attribute, bestSplitValue); Attribute label = splittedSet.getAttributes().getLabel(); splittedSet.selectSingleSubset(0); SplitCondition condition = new LessEqualsSplitCondition(attribute, bestSplitValue); splittedSet.recalculateAttributeStatistics(label); int labelValue = (int) splittedSet.getStatistics(label, Statistics.MODE); String labelName = label.getMapping().mapIndex(labelValue); Rule rule = new Rule(labelName, condition); int[] frequencies = new int[label.getMapping().size()]; int counter = 0; for (String value : label.getMapping().getValues()) frequencies[counter++] = (int) splittedSet.getStatistics(label, Statistics.COUNT, value); rule.setFrequencies(frequencies); model.addRule(rule); oldSize = trainingSet.size(); trainingSet = rule.removeCovered(trainingSet); } else { break; } } // add default rule if some examples were not yet covered if (trainingSet.size() > 0) { Attribute label = trainingSet.getAttributes().getLabel(); 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); model.addRule(defaultRule); } return model; }
private RuleModel createNominalRuleModel(ExampleSet exampleSet, Attribute attribute) { RuleModel model = new RuleModel(exampleSet); SplittedExampleSet splittedSet = SplittedExampleSet.splitByAttribute(exampleSet, attribute); Attribute label = splittedSet.getAttributes().getLabel(); for (int i = 0; i < splittedSet.getNumberOfSubsets(); i++) { splittedSet.selectSingleSubset(i); splittedSet.recalculateAttributeStatistics(label); SplitCondition term = new NominalSplitCondition(attribute, attribute.getMapping().mapIndex(i)); int labelValue = (int) splittedSet.getStatistics(label, Statistics.MODE); String labelName = label.getMapping().mapIndex(labelValue); Rule rule = new Rule(labelName, term); int[] frequencies = new int[label.getMapping().size()]; int counter = 0; for (String value : label.getMapping().getValues()) frequencies[counter++] = (int) splittedSet.getStatistics(label, Statistics.COUNT, value); rule.setFrequencies(frequencies); model.addRule(rule); } return model; }
@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; }