(self, datas, labels, attrs, threshold=0.01)
return node
train函数调用的真正训练函数
train函数调用的真正训练函数
train函数调用的真正训练函数
def __train(self, datas, labels, attrs, threshold=0.01):
        """train函数调用的真正训练函数
        """
        label_dict = Counter(labels)
        if len(label_dict.keys()) == 1:
            return float(label_dict.keys()[0])
        if len(attrs) == 0:
            return sum(label for label in labels) / float(len(labels))
        attr_gain, attr, split_value, left_data, left_label, right_data, right_label = self.get_best_feature(
            datas, labels, attrs)  # 得到最好信息增益的属性
        if attr_gain < threshold:
            return sum(label for label in labels) / float(len(labels))
        node = Node(attr, split_value)
        child_attr = self.get_split_attr(# 为下轮切割属性
            attrs, attr
        )
        #创建左子树
        node.left_tree = self.__train(
            self.split_data_by_attr(left_data , attrs , attr) , left_label, child_attr, threshold)
        #创建
        node.right_tree = self.__train(
            self.split_data_by_attr(right_data , attrs , attr), right_label, child_attr, threshold)
        return node
def __train ( self , datas , labels , attrs , threshold = 0.01 ) : label_dict = Counter ( labels ) if len ( label_dict . keys ( ) ) == 1 : return float ( label_dict . keys ( ) [ 0 ] ) if len ( attrs ) == 0 : return sum ( label for label in labels ) / float ( len ( labels ) ) attr_gain , attr , split_value , left_data , left_label , right_data , right_label = self . get_best_feature ( datas , labels , attrs ) # 得到最好信息增益的属性 if attr_gain < threshold : return sum ( label for label in labels ) / float ( len ( labels ) ) node = Node ( attr , split_value ) child_attr = self . get_split_attr ( # 为下轮切割属性 attrs , attr ) #创建左子树 node . left_tree = self . __train ( self . split_data_by_attr ( left_data , attrs , attr ) , left_label , child_attr , threshold ) #创建 node . right_tree = self . __train ( self . split_data_by_attr ( right_data , attrs , attr ) , right_label , child_attr , threshold ) return node