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


Python Tree.addchild方法代码示例

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


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

示例1: ID3

# 需要导入模块: import Tree [as 别名]
# 或者: from Tree import addchild [as 别名]
def ID3(ex, att, attr_labels, rl, mr):
    '''
    Performs the Interactive Dichotomiser 3 algorithm on a list of examples
    and a list of lists of attributes.  attr_labels is the list of labels
    for the attributes, and rl is the recursion level
    '''
    root = Tree()
    print rl
    if rl>mr: #Upon max recursion, return most frequent
        root.data = most_common(ex)
        return root
    if len(set(ex))==1: #if only 1 class of ie left, use it
        root.data = ex[0]
    elif len(att)<1: #if no attributes left, use most common
        root.data = most_common(ex)
    else:
        #Find attribute that best classifies examples
        maxinfo = 0
        i_A = -1
        for i in range(0,len(att)):
            thisinfo = info_gain(ex, att[i])
            if thisinfo>maxinfo:
                i_A = i
                maxinfo=thisinfo
        if i_A>=0:
            root.data = attr_labels[i_A] #Store node attribute label
            A = att[i_A]
        else: #no class has any more info than any other
            root.data = attr_labels[0]
            A = att[0]
        #For each possible value of max info attribute, add branch
        for v in set(A):
            child = Tree()
            child.data = v
            root.addchild(child)
            #Take 'used' elements out of ex and att!
            E_v = []
            att_v = [[] for i in range(0,len(att))]
            for i in range(0,len(A)):
                if A[i]==v:
                    E_v.append(ex[i])
                    [att_v[j].append(att[j][i]) for j in range(0,len(att))]
            if len(E_v)<1:
                leaf = Tree()
                leaf.data = most_common(ex)
                child.addchild(leaf)
            else:
                 child.addchild(ID3(E_v, all_except(att_v,i_A), all_except(attr_labels,i_A), rl+1, mr))
    return root
开发者ID:winsto99,项目名称:decision-tree,代码行数:51,代码来源:dt_utils.py


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