本文整理汇总了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