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


Python Node.answer方法代码示例

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


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

示例1: DLT

# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import answer [as 别名]
def DLT(dataset, parent_dataset):

  if not dataset['data']:
    if not parent_dataset:
      return ''
    else:
      node = Node()
      node.answer = p_val(parent_dataset)
      return node

  # creates an array with all the last column data values
  data = dataset['data']
  dataLastCol = []
  for line in data:
    dataLastCol.append(line[-1])

  if all(x == dataLastCol[0] for x in dataLastCol):
    node = Node()
    # all values are the same in dataLastCol, choose any index
    node.answer = dataLastCol[0]
    return node
  
  elif not dataset['attributes']:
    # if there are no attributes left in the dataset
    node = Node()
    node.answer = p_val(dataset)
    return node
  
  else:
    #print "We are in the else case!"
    
    # Find max gain
    gains = []
    attributes = dataset['attributes']
    for attr in attributes[:-1]:
      gains.append(gain(dataset, attr[0]))

    max_attr_index = gains.index(max(gains))
    max_attr = attributes[max_attr_index]
    
    # Create a new new for the tree
    node = Node()
    node.attribute = max_attr[0]

    # Clone the attributes list but remove the current attribute
    attributes_new = []
    for attr in attributes:
      if attr != max_attr:
        attributes_new.append(attr)

    # Branch the values
    for attr_value in max_attr[1]:
      data_new = []

      # Clone the data for the chosen attribute
      for dataLine in data:
        if dataLine[max_attr_index] == attr_value:
          d = list(dataLine) # copy the tuple to a new list
          #print "removing", max_attr_index, "from", d
          del d[max_attr_index] # remove the current attribute
          data_new.append(tuple(d))

      # Create the branched dataset
      branched_dataset = {}
      branched_dataset['attributes'] = attributes_new
      branched_dataset['data'] = data_new

      # Create the subtree/child
      child = DLT(branched_dataset, dataset) #DLT(dataset, parent_dataset)
      child.parent_attribute = node.attribute
      child.value = attr_value
      node.addChild(child)

    # Return the subtree/node
    return node
开发者ID:kasperomeck,项目名称:EDA132,代码行数:77,代码来源:DLT.py


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