2015-01-21 93 views
1

我正在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) 

回答

0

你的函數只能返回一次,這就是爲什麼它只是建立正確的節點;它也不應該返回它的子節點。您是否嘗試過與替換最後一行:

for node in nodeList: 
    node = buildTree(node, maxDepth, currentDepth + 1, minGain, maxGain, allFeatures) 
return currentNode 
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 
+0

我可以看到它如何能工作,但節點需要屬性「載體」來決定哪些功能分割上樹,如果我遞歸在那指出它不會有這個。 – user3499704 2015-01-21 03:46:17

+0

因此,首先設置屬性,然後才能進行遞歸調用(切換行的順序)。 – alfasin 2015-01-21 03:50:54