Ejemplo n.º 1
0
def create_random_decision_tree(context, depth):
    if 0 == depth: return LeafNode(choice(context.outcomes))
    else:
        rule, num_outcomes = new_rule(context.attributes)
        children = [
          create_random_decision_tree(context, depth-1) for i in xrange(num_outcomes)
        ]
        return DecisionNode(rule, children)
Ejemplo n.º 2
0
def replace_rule(decision_node, context, depth = 0):
    "Replace the rule in the node with a completely new random rule"
    decision_node.rule, num_outcomes = new_rule(context.attributes)
    while num_outcomes > len(decision_node.children): # add children if needed
        decision_node.children.append(create_random_decision_tree(context, depth))
    while num_outcomes < len(decision_node.children): # remove children if too many
        decision_node.children.pop()
    shuffle(decision_node.children) # randomise order
Ejemplo n.º 3
0
def insert_decision(decision_node, context, depth = 0):
    "Insert an extra decision in the tree"
    child_to_replace = choice(decision_node.children)
    idx_to_replace = decision_node.children.index(child_to_replace)
    rule, num_outcomes = new_rule(context.attributes)
    children = [ child_to_replace ]
    while num_outcomes > len(children): # add other children
        children.append(create_random_decision_tree(context, depth))
    decision_node.children[idx_to_replace] = DecisionNode(rule, children)