决策树python

时间:2017-10-09 13:56:17

标签: python decision-tree

我的代码只返回最后一个当前节点。我想要完整的树,但我不知道下面的代码有什么问题:

def load_game(self,universe_list):
   self.universe_list = universe_list
    self.root = self.universe_list[0]

    self.temp_stack = stack.Stack()
    self.temp_stack.push(self.root)
    self.__curr_node = btree.BTree(self.root, None, None)         
    #self.root = self.__curr_node

    for item in self.universe_list[1:]:
        #print(item+"--"+self.temp_stack.peek())

        if self.__curr_node.left is None:
            self.__curr_node.attachItemLeft(item)
            #print('currentnode = '+self.__curr_node.data+' leftnode= '+self.__curr_node.left.data)

        else:
            self.__curr_node.attachItemRight(item)
            #print('currentnode = '+self.__curr_node.data+'leftnode = '+self.__curr_node.left.data+' rightnode= '+self.__curr_node.right.data)
            self.temp_stack.pop()
            if not self.temp_stack.is_empty():
                topVal = self.temp_stack.peek()
                self.__curr_node = btree.BTree(topVal,self.__curr_node,None)
            else:
                self.__curr_node = btree.BTree(self.__curr_node,None) 
            #self.__curr_node = btree.BTree(topVal,topVal,None)
            #print(self.__curr_node)

        if item[-1] == "?":
            self.temp_stack.push(item)
            self.__curr_node = btree.BTree(item,None,None)

        self.__tree = self.__curr_node
    self.__tree.set_traverse(btree.BTree.PRE)

输入= ["Legs?","cat","underground?","Earthworm","Water?","Fish","Snake"]

output = ["Water?","Fish","Snake"]

0 个答案:

没有答案