public virtual void addNode(String attributeValue, DecisionTree tree) { if (nodes.ContainsKey(attributeValue)) { nodes[attributeValue] = tree; } else { nodes.Add(attributeValue, tree); } }
public static DecisionTree getStumpFor(DataSet ds, String attributeName, String attributeValue, String returnValueIfMatched, List<String> unmatchedValues, String returnValueIfUnmatched) { DecisionTree dt = new DecisionTree(attributeName); dt.addLeaf(attributeValue, returnValueIfMatched); foreach (String unmatchedValue in unmatchedValues) { dt.addLeaf(unmatchedValue, returnValueIfUnmatched); } return dt; }
public virtual String ToString(int depth, StringBuilder buf) { if (attributeName != null) { buf.Append(Util.ntimes("\t", depth)); buf.Append(Util.ntimes("***", 1)); buf.Append(attributeName + " \n"); foreach (String attributeValue in nodes.Keys) { buf.Append(Util.ntimes("\t", depth + 1)); buf.Append("+" + attributeValue); buf.Append("\n"); DecisionTree child = nodes[attributeValue]; buf.Append(child.ToString(depth + 1, new StringBuilder())); } } return(buf.ToString()); }
public static List <DecisionTree> getStumpsFor(DataSet ds, String returnValueIfMatched, String returnValueIfUnmatched) { List <String> attributes = ds.getNonTargetAttributes(); List <DecisionTree> trees = new List <DecisionTree>(); foreach (String attribute in attributes) { List <String> values = ds.getPossibleAttributeValues(attribute); foreach (String value in values) { List <String> unmatchedValues = Util.removeFrom(ds .getPossibleAttributeValues(attribute), value); DecisionTree tree = getStumpFor(ds, attribute, value, returnValueIfMatched, unmatchedValues, returnValueIfUnmatched); trees.Add(tree); } } return(trees); }
// // PRIVATE METHODS // private static DecisionTree createInducedRestaurantDecisionTree() { // from AIMA 2nd ED // Fig 18.6 // friday saturday node DecisionTree frisat = new DecisionTree("fri/sat"); frisat.addLeaf(Util.YES, Util.YES); frisat.addLeaf(Util.NO, Util.NO); // type node DecisionTree type = new DecisionTree("type"); type.addLeaf("French", Util.YES); type.addLeaf("Italian", Util.NO); type.addNode("Thai", frisat); type.addLeaf("Burger", Util.YES); // hungry node DecisionTree hungry = new DecisionTree("hungry"); hungry.addLeaf(Util.NO, Util.NO); hungry.addNode(Util.YES, type); // patrons node DecisionTree patrons = new DecisionTree("patrons"); patrons.addLeaf("None", Util.NO); patrons.addLeaf("Some", Util.YES); patrons.addNode("Full", hungry); return patrons; }
private static DecisionTree createActualRestaurantDecisionTree() { // from AIMA 2nd ED // Fig 18.2 // raining node DecisionTree raining = new DecisionTree("raining"); raining.addLeaf(Util.YES, Util.YES); raining.addLeaf(Util.NO, Util.NO); // bar node DecisionTree bar = new DecisionTree("bar"); bar.addLeaf(Util.YES, Util.YES); bar.addLeaf(Util.NO, Util.NO); // friday saturday node DecisionTree frisat = new DecisionTree("fri/sat"); frisat.addLeaf(Util.YES, Util.YES); frisat.addLeaf(Util.NO, Util.NO); // second alternate node to the right of the diagram below hungry DecisionTree alternate2 = new DecisionTree("alternate"); alternate2.addNode(Util.YES, raining); alternate2.addLeaf(Util.NO, Util.YES); // reservation node DecisionTree reservation = new DecisionTree("reservation"); frisat.addNode(Util.NO, bar); frisat.addLeaf(Util.YES, Util.YES); // first alternate node to the left of the diagram below waitestimate DecisionTree alternate1 = new DecisionTree("alternate"); alternate1.addNode(Util.NO, reservation); alternate1.addNode(Util.YES, frisat); // hungry node DecisionTree hungry = new DecisionTree("hungry"); hungry.addLeaf(Util.NO, Util.YES); hungry.addNode(Util.YES, alternate2); // wait estimate node DecisionTree waitEstimate = new DecisionTree("wait_estimate"); waitEstimate.addLeaf(">60", Util.NO); waitEstimate.addNode("30-60", alternate1); waitEstimate.addNode("10-30", hungry); waitEstimate.addLeaf("0-10", Util.YES); // patrons node DecisionTree patrons = new DecisionTree("patrons"); patrons.addLeaf("None", Util.NO); patrons.addLeaf("Some", Util.YES); patrons.addNode("Full", waitEstimate); return patrons; }
public override void addNode(String attributeValue, DecisionTree tree) { throw new ApplicationException("cannot add Node to ConstantDecisonTree"); }
// // PRIVATE METHODS // private DecisionTree decisionTreeLearning(DataSet ds, List<String> attributeNames, ConstantDecisonTree defaultTree) { if (ds.size() == 0) { return defaultTree; } if (allExamplesHaveSameClassification(ds)) { return new ConstantDecisonTree(ds.getExample(0).targetValue()); } if (attributeNames.Count == 0) { return majorityValue(ds); } String chosenAttribute = chooseAttribute(ds, attributeNames); DecisionTree tree = new DecisionTree(chosenAttribute); ConstantDecisonTree m = majorityValue(ds); List<String> values = ds.getPossibleAttributeValues(chosenAttribute); foreach (String v in values) { DataSet filtered = ds.matchingDataSet(chosenAttribute, v); List<String> newAttribs = Util.removeFrom(attributeNames, chosenAttribute); DecisionTree subTree = decisionTreeLearning(filtered, newAttribs, m); tree.addNode(v, subTree); } return tree; }
public virtual void train(DataSet ds) { List<String> attributes = ds.getNonTargetAttributes(); this.tree = decisionTreeLearning(ds, attributes, new ConstantDecisonTree(defaultValue)); }
// used when you have to test a non induced tree (eg: for testing) public DecisionTreeLearner(DecisionTree tree, String defaultValue) { this.tree = tree; this.defaultValue = defaultValue; }
public StumpLearner(DecisionTree sl, String unable_to_classify) : base(sl, unable_to_classify) { }
public override void addNode(String attributeValue, DecisionTree tree) { throw new ApplicationException("cannot add Node to ConstantDecisonTree"); }