예제 #1
0
 public void AddChild(Value value, Node node)
 {
     if (children.ContainsKey (value.AsString ())) {
         this.children [value.AsString ()] = node;
     } else {
         this.children.Add (value.AsString (), node);
     }
 }
예제 #2
0
        private Node DecisionTreeLearner(List<Data> examples, List<Attribute> attributes, List<Data> parent_examples, bool prune=false)
        {
            if (examples.Count == 0) {
                return new Leaf (this.Plurality (parent_examples));
            } else if (this.SameClassification (examples)) {
                return new Leaf (examples [0].Target);
            } else if (attributes.Count == 0) {
                return new Leaf (this.Plurality (examples));
            } else {
                var mostImportantAttribute = this.Importance (examples, attributes);

                var filtered_attributes = attributes.ToList ();
                filtered_attributes.RemoveAll (x => x.Name == mostImportantAttribute.Name);

                var tree = new Node (mostImportantAttribute);
                var partitions = this.Partition (examples, mostImportantAttribute);
                var values = mostImportantAttribute.Values;

                foreach (var kvp in partitions) {
                    tree.AddChild (kvp.Key, this.DecisionTreeLearner (kvp.Value, filtered_attributes, examples, prune));
                    if (values.Contains (kvp.Key)) {
                        values.Remove (kvp.Key);
                    }
                }

                foreach (var value in values) {
                    tree.AddChild (value, new Leaf (this.Plurality (examples)));
                }

                if (prune && tree.IsEndNode () && !this.IsSignificant (partitions, examples)) {
                    tree = new Leaf (this.Plurality (examples));
                }

                return tree;
            }
        }