树丛中的递归

时间:2015-01-21 03:23:44

标签: python recursion tree decision-tree

我在Python中构建一个简单的二元决策树。我使用递归来构建树,但是,作为一个没有牢牢把握这个概念的人,我遇到了一些麻烦。我希望在树到达某个深度时停止递归,但是我不确定在哪里增加值,因此它一次构建多个分支。现在,树只向右分支,直到它达到5,然后停止。我应该在哪里/如何增加值?现在我在函数底部的for循环中执行此操作。

def buildTree(currentNode, maxDepth, currentDepth, minGain, currentGain, allFeatures):
print(currentNode.data)
if maxDepth <= currentDepth:
    return None
else:
    splitOn, hasFeat, noFeat, allFeatures, maxGain = split(currentNode, allFeatures)
    print(len(hasFeat), len(noFeat))
    currentNode.left = Tree()
    currentNode.left.vectors = hasFeat
    currentNode.left.data = splitOn
    currentNode.left.entropy = getEntropy(getInstances(currentNode.left.vectors))
    currentNode.right = Tree()
    currentNode.right.vectors = noFeat
    currentNode.right.data = "!" + splitOn
    currentNode.right.entropy = getEntropy(getInstances(currentNode.right.vectors))
    nodeList = [currentNode.right, currentNode.left]
    for node in nodeList:
        return buildTree(node, maxDepth, currentDepth + 1, minGain, maxGain, allFeatures)

2 个答案:

答案 0 :(得分:2)

递归调用应在currentNode.leftcurrentNode.right上使用。

代码应该是这样的:

def buildTree(currentNode, maxDepth, currentDepth, minGain, currentGain, allFeatures):
    print(currentNode.data)
    if maxDepth <= currentDepth:
        return None
    else:
        splitOn, hasFeat, noFeat, allFeatures, maxGain = split(currentNode, allFeatures)
        print(len(hasFeat), len(noFeat))
        currentNode.left = buildTree(Tree(), maxDepth, currentDepth + 1, minGain, maxGain, allFeatures)
        currentNode.left.vectors = hasFeat
        currentNode.left.data = splitOn
        currentNode.left.entropy = getEntropy(getInstances(currentNode.left.vectors))
        currentNode.right = buildTree(Tree(), maxDepth, currentDepth + 1, minGain, maxGain, allFeatures)
        currentNode.right.vectors = noFeat
        currentNode.right.data = "!" + splitOn
        currentNode.right.entropy = getEntropy(getInstances(currentNode.right.vectors))
        nodeList = [currentNode.right, currentNode.left]
        return nodeList

答案 1 :(得分:0)

你的功能只能返回一次,这就是为什么它只能构建正确的节点;它也不应该返回它的子节点。您是否尝试使用以下内容替换最后一行:

for node in nodeList:
    node = buildTree(node, maxDepth, currentDepth + 1, minGain, maxGain, allFeatures)
return currentNode