How to save a decision tree

I tried several different methods, some of which I found here that include creating a Node class and nested dictionaries, but I can't get them to work.

My code is currently running a few lines of DNA (a, t, g, c) and then stored as a numpy array. Then it finds the attribute that gives the greatest gain and splits the data into 4 new numpy arrays (depending on the presence of a, t, g or c in the attribute).

I cannot make a recursive function that can build a tree. I am completely new to python programming and programming, so please describe in detail what I should do.

Thanks for any help

+3
source share
3 answers

python, Sci-kit, : http://scikit-learn.org/stable/modules/tree.html. Scikit Learn, , , .

Sci-kit, python, Anacondas, python. Anaconda Continuum : http://continuum.io/downloads

1

Hacker News. Python PostgreSQL , . : http://www.garysieling.com/blog/building-decision-tree-python-postgres-data

+1

, . , node , - . :

class Node(object):
    def __init__(self):
        self.split_variable = None
        self.left_child = None
        self.right_child = None

    def get_name(self):
        return 'Node'

class Leaf(object):
    def __init__(self):
        self.value = None

    def get_name(self):
        return 'Leaf'

node: "split_variable" , split, : [a, t, g, c] "left_child" "right_child" node Leaf. / /. ( node "split_value" /, , ).

"": "" (.. ).

, / . . get_name() , . , , pandas DataFrames, . ():

def evaluate_tree(your_data, node):
    if your_data[node.split_variable]:
        if node.left_child.get_name() == 'Node':
            evaluate_tree(your_data, node.left_child)
        elif node.left_child.get_name() == 'Leaf':
            return node.left_child.value
    else:
        if node.right_child.get_name() == 'Node':
            evaluate_tree(your_data, node.right_child)
        elif node.right_child.get_name() == 'Leaf':
            return node.right_child.value

!

+2

This is probably what you want:

node example:

{'sex': {'yes': 'send email', 'no': 'not send email'}}
+1
source

All Articles