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


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

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


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

示例1: ID3

# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import children[value] [as 别名]
 def ID3(self, features, attributes):
     self.nodeCnt += 1
     wholeCnt = len(features)
     positiveCnt = self.countPositive(features)
     if positiveCnt == wholeCnt:
         return Node(-1, 1, {})
     elif positiveCnt == 0:
         return Node(-1, 0, {})
     elif len(attributes) == 0:                  # return major label
         return Node(-1, 1 if positiveCnt/float(wholeCnt) >= self.postiveRatio else 0, {})
     else:
         candidates = []
         maxGain = 0
         cnt = 0
         for attribute in attributes:
             gain, childs = self.computeGain(features, attribute)
             print(str(cnt) + ' : ' + str(gain))
             cnt += 1
             #gain /= self.splitRatio(features, childs)
             if gain > maxGain:
                 maxGain = gain
                 candidates = []
                 candidates.append([attribute, childs])
             elif gain == maxGain:
                 candidates.append([attribute, childs])
         bestPick = candidates[randint(0, len(candidates)-1)]                # [attribute, childs]
         print('choose: ' + str(bestPick[0]) + ' ' + self.featureNames[bestPick[0]])
         chi = self.computeChiSquaredCriterion(features, bestPick[1])
         pValue = 1 - stats.chi2.cdf(chi, len(bestPick[1]) - 1)
         print('chi: ' + str(chi) + '  pvalue: ' + str(pValue) )
         if pValue > self.chiCriterion:                                         # split stop
             return Node(-1, 1 if positiveCnt/float(wholeCnt) >= self.postiveRatio else 0, {})
         currentNode = Node(bestPick[0], '', {})
         newAttributes = copy.deepcopy(attributes)
         newAttributes.remove(bestPick[0])
         for i in range(len(self.featureValue[bestPick[0]])):
             value = self.featureValue[bestPick[0]][i]
             currentNode.children[value] = self.ID3(bestPick[1][i], newAttributes)
         return currentNode
开发者ID:ZephyrYin,项目名称:decisionTree,代码行数:41,代码来源:decisionTree.py


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