当前位置: 首页>>代码示例>>Python>>正文


Python Node.children[key]方法代码示例

本文整理汇总了Python中node.Node.children[key]方法的典型用法代码示例。如果您正苦于以下问题:Python Node.children[key]方法的具体用法?Python Node.children[key]怎么用?Python Node.children[key]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在node.Node的用法示例。


在下文中一共展示了Node.children[key]方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ID3_recursive

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def ID3_recursive(data_set, attribute_metadata, numerical_splits_count, depth, attribute_modes_dict):
    if depth == 0 or check_homogenous(data_set) is not None or len(attribute_metadata) == 0:
        return default_node(data_set)    
    else:
        (best_attribute, split_value) = pick_best_attribute(data_set, attribute_metadata, numerical_splits_count)
        if best_attribute == False:
            return default_node(data_set)
        
        node = Node()
        node.decision_attribute = best_attribute
        node.name = attribute_metadata[best_attribute]['name']
        node.is_nominal = attribute_metadata[best_attribute]['is_nominal']
        node.value = attribute_modes_dict[best_attribute]
        updated_numerical_splits_count = copy.deepcopy(numerical_splits_count)
        updated_numerical_splits_count[best_attribute] -= 1

        if node.is_nominal:
            examples = split_on_nominal(data_set, best_attribute)
            for key, values in examples.items():
                node.children[key] = ID3_recursive(values, attribute_metadata, updated_numerical_splits_count, depth - 1, attribute_modes_dict)
        else:
            node.splitting_value = split_value
            (less, greater_or_equal) = split_on_numerical(data_set, best_attribute, split_value)
            node.children[0] = ID3_recursive(less, attribute_metadata, updated_numerical_splits_count, depth - 1, attribute_modes_dict)
            node.children[1] = ID3_recursive(greater_or_equal, attribute_metadata, updated_numerical_splits_count, depth - 1, attribute_modes_dict)
        
        return node
开发者ID:benmel,项目名称:ml_ps2,代码行数:29,代码来源:ID3.py

示例2: ID3

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def ID3(data_set, attribute_metadata, numerical_splits_count, depth):
    '''
    See Textbook for algorithm.
    Make sure to handle unknown values, some suggested approaches were
    given in lecture.
    ========================================================================================================
    Input:  A data_set, attribute_metadata, maximum number of splits to consider for numerical attributes,
    maximum depth to search to (depth = 0 indicates that this node should output a label)
    ========================================================================================================
    Output: The node representing the decision tree learned over the given data set
    ========================================================================================================

    '''
    node = Node() # new node
    entropy_bound = 0.15 # entropy of data_set must be below bound to become a leaf
    pick_best = pick_best_attribute(data_set, attribute_metadata, numerical_splits_count) # tuple
    best_attribute = pick_best[0] # best attribute to split on
    split_value = pick_best[1] # best value to split on
    if entropy(data_set) < entropy_bound or depth == 0 or best_attribute == False: 
        node.label = mode(data_set)
        return node
    if split_value is not False: # if there is a split value (best attribute is numeric)
        split_data = split_on_numerical(data_set, best_attribute, split_value) # splitting data by split value (lesser, greater)
        node.is_nominal = False # node is numeric
        node.splitting_value = split_value # best value to split on
        node.children[0] = ID3(split_data[0], attribute_metadata, numerical_splits_count, depth - 1) # less than split value
        node.children[1] = ID3(split_data[1], attribute_metadata, numerical_splits_count, depth - 1) # greater than split value
        node.name = attribute_metadata[best_attribute]['name']
        node.decision_attribute = best_attribute # best attribute to split on
    else: # best_attribute is nominal
        split_data = split_on_nominal(data_set, best_attribute) # returns a dictionary with nominal attributes as keys
        node.is_nominal = True # node is nominal
        split_data_copy = deepcopy(split_data) # deep copy split_data
        ### filling in missing data ###
        for key in split_data_copy.keys():
            if key is None: 
                # find most common attribute and add the missing attribute data into the most common attribute
                greatest_length = -1
                mode_att = None
                for att, data in split_data_copy.iteritems():
                    if len(data) > greatest_length:
                        greatest_length = len(data)
                        mode_att = att
                for data in split_data_copy[key]:
                    split_data_copy[mode_att].append(data) # adds all the None data into the mode attribute 
                split_data_copy.pop(key, None) # removes the None attribute data
        # add a children for each nominal attribute
        for key in split_data_copy: 
            node.children[key] = ID3(split_data_copy[key], attribute_metadata, numerical_splits_count, depth - 1)
        node.name = attribute_metadata[best_attribute]['name']
        node.decision_attribute = best_attribute
    # print node.children
    return node
开发者ID:brianzhan,项目名称:EECS349Psets,代码行数:55,代码来源:ID3.py

示例3: copy_node

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def copy_node(node):
    new_node = Node()
    new_node.label = node.label
    new_node.decision_attribute = node.decision_attribute
    new_node.is_nominal = node.is_nominal
    new_node.value = node.value
    new_node.splitting_value = node.splitting_value

    if node.is_nominal:
        new_node.children = {}
        for key in node.children:
            new_node.children[key] = copy_node(node.children[key])
    else:
        new_node.children = []
        for i in range(len(node.children)):
            new_node.children.append(copy_node(node.children[i]))
    new_node.name = node.name

    return new_node
开发者ID:obiorahm,项目名称:PS2code,代码行数:21,代码来源:pruning.py

示例4: helper

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def helper(data_set, attribute_metadata, numerical_splits_count, depth):
    root = Node()
    root.name = 'default'
    if len(data_set) == 0 :
        return root
    else :
        if check_homogenous(data_set) != None :
            root.label = check_homogenous(data_set)
            return root
        else :
            if  len(attribute_metadata) == 1 or depth == 0 :
                root.label = mode(data_set)
                return root
            else :
                best_attribute = pick_best_attribute(data_set, attribute_metadata , numerical_splits_count)
                if best_attribute[0] == False :
                    root.label = mode(data_set)
                    return root


                else :
                    root.name = attribute_metadata[best_attribute[0]]['name']

                    root.decision_attribute = best_attribute[0]

                    if best_attribute[1] == False : # dictionary

                        root.is_nominal = None
                        temp_dict = split_on_nominal(data_set,best_attribute[0])
                        depth -= 1
                        for key in temp_dict.keys():
                            root.children[key] = helper(temp_dict[key],attribute_metadata,numerical_splits_count,depth)
                    else :
                        numerical_splits_count[best_attribute[0]] -= 1

                        root.is_nominal = best_attribute[1]
                        root.splitting_value = best_attribute[1]
                        temp_tuple = split_on_numerical(data_set,best_attribute[0] , best_attribute[1])
                        depth -= 1
                        root.children[0] = (helper(temp_tuple[0] ,attribute_metadata,numerical_splits_count,depth))
                        root.children[1] = (helper(temp_tuple[1] ,attribute_metadata,numerical_splits_count,depth))
                    return root
开发者ID:Jiawen-Ou,项目名称:Machine-Learning---Decision-Tree,代码行数:44,代码来源:ID3.py

示例5: ID3

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def ID3(data_set, attribute_metadata, numerical_splits_count, depth):
    '''
    See Textbook for algorithm.
    Make sure to handle unknown values, some suggested approaches were
    given in lecture.
    ========================================================================================================
    Input:  A data_set, attribute_metadata, maximum number of splits to consider for numerical attributes,
	maximum depth to search to (depth = 0 indicates that this node should output a label)
    ========================================================================================================
    Output: The node representing the decision tree learned over the given data set
    ========================================================================================================

    '''
    # need to keep track of numerical_splits_count
    # decrease corresponding numerical_splits_count entry each time a numeric entry is split on

    if data_set == None:
        return None
    if len(data_set) == 0:
        return None

    n = Node()

    homogenous_value = check_homogenous(data_set)
    if homogenous_value != None:
        n.label = homogenous_value
        return n
    
    if depth == 0:
        n.label = mode(data_set)
        return n

    (best_i, split_value) = pick_best_attribute(data_set, attribute_metadata, numerical_splits_count)

    if best_i == False:
        n.label = mode(data_set)
        return n

    n.label = None

    # setting decision attribute to index of the attribute with the highest gain ratio
    n.decision_attribute = best_i
    n.name = attribute_metadata[best_i]['name']

    # setting splitting_value to the split_value from pick_best_attribute
    n.splitting_value = split_value

    # if split_value is not false then we are dealing with a numeric
    if split_value != False:      
        n.is_nominal = False
        
        left_data, right_data = split_on_numerical (data_set, best_i, split_value)

        numerical_splits_count[best_i] -= 1

        left_node = ID3(left_data, attribute_metadata, numerical_splits_count, depth-1)
        right_node = ID3(right_data, attribute_metadata, numerical_splits_count, depth-1)
        n.children = []

        n.children.append(left_node)
        n.children.append(right_node)

        return n
        
    else:
        n.is_nominal = True
        kid_set = split_on_nominal(data_set, best_i)
        n.children = {}
        for key, val in kid_set.iteritems():
            newNode = ID3(val, attribute_metadata, numerical_splits_count, depth - 1)
            if newNode != None:
                n.children[key] = newNode
        return n
开发者ID:cyrusaf,项目名称:decision_tree,代码行数:75,代码来源:ID3.py

示例6: ID3

# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[key] [as 别名]
def ID3(data_set, attribute_metadata, numerical_splits_count, depth):
    '''
    See Textbook for algorithm.
    Make sure to handle unknown values, some suggested approaches were
    given in lecture.
    ========================================================================================================
    Input:  A data_set, attribute_metadata, maximum number of splits to consider for numerical attributes,
	maximum depth to search to (depth = 0 indicates that this node should output a label)
    ========================================================================================================
    Output: The node representing the decision tree learned over the given data set
    ========================================================================================================

    '''
    
    n = Node()
    if data_set == None or len(data_set) == 0:
        # print "reach state where data is empty"
        n.label = 1
        return n
    
    homo = check_homogenous(data_set)
    if homo != None:
        n.label = homo
        return n
    
    if depth == 0:
        n.label = mode (data_set)
        return n

    (best, split) = pick_best_attribute(data_set, attribute_metadata, numerical_splits_count)

    if best == False:
        n.label = mode (data_set)
        return n

    n.label = None
    n.decision_attribute = best
    n.name = attribute_metadata[best]['name']
    n.splitting_value = split
    # Numeric case

    if split != False:      
        n.is_nominal = False
        
        leftData, rightData = split_on_numerical (data_set, best, split)

        numerical_splits_count[best] -= 1

        
        leftTree = ID3(leftData, attribute_metadata, numerical_splits_count, depth-1)
        rightTree = ID3(rightData, attribute_metadata, numerical_splits_count, depth-1)
        n.children = []


        
        n.children.append(leftTree)
        n.children.append(rightTree)

        return n
        
    else:
        n.is_nominal = True
        dsets = split_on_nominal (data_set, best)
        n.children = {}
        for key, val in dsets.iteritems():
            newTree = ID3(val, attribute_metadata, numerical_splits_count, depth - 1)
            if newTree != None:
                n.children[key] = newTree
        return n
    pass
开发者ID:RifleZhang,项目名称:eecs349,代码行数:72,代码来源:ID3.py


注:本文中的node.Node.children[key]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。