0

これはかなり具体的な質問ですが、ここで何が起こっているのか理解できなかったので、問題を提示しましょう。

決定木を成長させると、ノードを分割する分割関数があります。つまり、2 つの子をノードに接続します。何らかの理由で、以下のコードは、id(currentNode.children[0])==id(currentNode) のように、ノード自体を子として割り当てます。

def split(currentNode):
    if impure(currentNode.data):
        currentNode.attribute = getBestAttr(currentNode.attributes,currentNode.data);
        childrenAttributes = deepcopy(currentNode.attributes);
        childrenAttributes.remove(currentNode.attribute);
        childrenData = splitData(currentNode.data,currentNode.attribute);
        for i in range(2):            
            currentNode.children[i] = node(childrenData[i],childrenAttributes);
            split(currentNode.children[i]);

重要な部分はおそらく次のとおりです。

for i in range(2):            
    currentNode.children[i] = node(childrenData[i],childrenAttributes);
    split(currentNode.children[i]);

私の理解では、コンストラクター呼び出しは、新しく作成されたノード オブジェクトへの参照を返す必要があります。これは、NEW オブジェクトであるため、親ノードへの参照と決して同じではありません。

ノード オブジェクトは次のとおりです。

class node:    
    data = None;
    attributes = None;    
    attribute = None;    
    children = [None,None];

    def __init__(self,data,attributes):
        self.data = data;
        self.attributes = attributes;

私は Python で oop を初めて使用し、一般的に oop の経験があまりないため、この点で誤解があると思いますが、質問を指定する方法がわかりません。ありがとう。

4

2 に答える 2

1

ねえ、トバイアス、アンジャナです。

この問題は、ノード クラスの宣言 (または定義など) で、データ、属性、属性、および子をクラス レベルの属性として定義しているという事実と関係があるのでしょうか? つまり、新しい Node オブジェクトを作成しても、Node.children の値は変更されず、Node クラスをインスタンス化するオブジェクト (例: thisnode = Node()) の場合、thisnode.children は他のすべてのノード オブジェクトの場合と同じになります ( thisnode.children = Node.children)

ノードオブジェクトごとに異なるようにしたい場合は、initメソッドで設定する必要があります (self.children など)。

それが何か関係があるかどうかはわかりません...私に知らせてください。

于 2013-01-21T11:25:31.843 に答える
0

解決策: Python OOP は Java OOP とは異なります! クラス定義を次のように変更します。

class node:
def __init__(self,data,attributes):
    self.data = data;
    self.attributes = attributes;
    self.children = [None,None];
    self.attribute = None;
于 2013-01-21T17:00:01.277 に答える