对象在重新分配后不会更改

2024-04-27 08:05:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试修剪决策树,使用一些程序。你知道吗

在对当前树进行各种操作之后,我从函数validate()(在prune_helper中,通过print(self.validate(validation_data)))得到不同的值—在操作树之前和之后,这是非常好的(这意味着对于给定的节点,树确实发生了一些事情)。你知道吗

def prune_helper(self, curr_node):
    all_err = []

    # The current tree.
    tree_1 = self
    all_err.append((tree_1._generalization_error(), tree_1))

    # Replace node with leaf 1.
    tree_2 = self._replace_leaf(curr_node, DEMOCRAT)
    all_err.append((tree_2._generalization_error(), tree_2))

    # Replace node with leaf 0.
    tree_3 = self._replace_leaf(curr_node, REPUBLICAN)
    all_err.append((tree_3._generalization_error(), tree_3))

    # Replace node with left subtree.
    test_4 = self._replace_subtree(curr_node, LEFT)
    all_err.append((test_4._generalization_error(), test_4))

    # Replace node with middle subtree.
    test_5 = self._replace_subtree(curr_node, MIDDLE)
    all_err.append((test_5._generalization_error(), test_5))

    # Replace node with right subtree.
    test_6 = self._replace_subtree(curr_node, RIGHT)
    all_err.append((test_6._generalization_error(), test_6))

    all_err.sort(key=lambda tup: tup[0])
    min_tree = all_err[0][1]

    # print(self.validate(validation_data)) <-- This

    self = copy.deepcopy(min_tree)

    # print(self.validate(validation_data)) <-- Mostly different than this

    curr_node.pruned = True

def prune(self, curr_node=None):

    if curr_node is None:
        curr_node = self._root

    # Node is leaf.
    if curr_node.leaf:
        self.prune_helper(curr_node=curr_node)
        return

    # Node is not a leaf, we may assume that he has all three children.
    if curr_node.left.pruned is False:
        self.prune(curr_node=curr_node.left)
    if curr_node.middle.pruned is False:
        self.prune(curr_node=curr_node.middle)
    if curr_node.right.pruned is False:
        self.prune(curr_node=curr_node.right)

    # We'll prune the current node, only if we checked all of its children.
    self.prune_helper(curr_node=curr_node)

但问题是,当我想在对树进行“完全”修剪之后为它计算一些值时,我得到了validate()返回的相同值,这意味着树可能根本没有改变,对树的影响只发生在prune_tree-

def prune_tree(tree):
    # print(tree.validate(validation_data)) <- This
    tree.prune()
    # print(tree.validate(validation_data)) <- Same as this

    tree.print_tree('after')

我想问题可能出在我试图改变self对象的方式上。有没有什么明显的迹象表明我在实现整个过程中犯了错误,从而导致了这样的结果?你知道吗


Tags: testselfnodetreeerrorallvalidateerr