本文整理汇总了Python中node.Node.children[0]方法的典型用法代码示例。如果您正苦于以下问题:Python Node.children[0]方法的具体用法?Python Node.children[0]怎么用?Python Node.children[0]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类node.Node
的用法示例。
在下文中一共展示了Node.children[0]方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ID3_recursive
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[0] [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
示例2: ID3
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[0] [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
示例3: helper
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[0] [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
示例4: test_calc_node_score
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import children[0] [as 别名]
def test_calc_node_score(self):
root = Node(7)
root.val = 50
root.update = 1350
v0 = Node(7)
v0.val = 80+90+100/3
v0.update = 1380
v0.parent = root
# child num < 2 case
child = Node(7)
child.val, child.update = 50,100
v0.children[0] = child
# mean operator case
eq_(1.0*v0.val/v0.update, self.M.calc_node_score(v0,0))
data =[ (80,450), (90,400), (100, 500)]
for i in range(3):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
# max operator case
eq_(100/500.0, self.M.calc_node_score(v0,0))
data =[ (80,450), (90,400), (100, 400)]
for i in range(3):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
# mean operator case
eq_(1.0*v0.val/v0.update, self.M.calc_node_score(v0,0))