Exemplo n.º 1
0
 def decision_tree_learning(examples, attributes, parent_examples=()):
     if len(examples) == 0:
         return plurality_value(parent_examples)
     elif same_classification(examples):
         return DecisionLeaf(examples[0][target])
     elif len(attributes) == 0:
         return plurality_value(examples)
     elif percent_error(examples) < error_threshold:
         return plurality_value(examples)
     else:
         a = importance(attributes, examples)
         tree = DecisionTree(a, dataset.attrnames[a])
         for (val_i, exs_i) in split_by(a, examples):
             subtree = decision_tree_learning(exs_i, removeall(a, attributes), examples)
             tree.add(val_i, subtree)
         return tree
Exemplo n.º 2
0
 def decision_tree_learning(examples, attributes, m, parent_examples=()):
     if len(examples) == 0:
         return majority_value(parent_examples)
     elif same_classification(examples):
         return DecisionLeaf(examples[0][target])
     elif len(attributes) == 0:
         return majority_value(examples)
     elif misclass_error(examples) < m:
         return majority_value(examples)
     else:
         A = pick_attribute(attributes, examples)
         tree = DecisionTree(A, dataset.attrnames[A])
         nonlocal internal_nodes
         internal_nodes += 1
         for (val_i, exs_i) in split(A, examples):
             subtree = decision_tree_learning(exs_i,
                                              removeall(A, attributes), m,
                                              examples)
             tree.add(val_i, subtree)
         return tree