gpt4 book ai didi

python - DecisionTreeClassifier - 树的手动修剪

转载 作者:行者123 更新时间:2023-12-01 08:31:30 25 4
gpt4 key购买 nike

我正在制作一个交互式建模工具。这个想法是用决策树生成变量。然而,这个变量需要具有经济意义(我希望能够删除理论上没有意义的分割)。因此,我用plotly绘制了一棵树,以便能够监听用户点击的位置。我在下面附上一张图片。

我的问题是我是否可以手动删除节点。我可以捕获点击,即您想要删除哪个节点;但是我在 DecisionTreeClassifier 中没有看到手动删除特定节点的选项。

Example Tree Fullsize image

非常感谢。

马林

最佳答案

根据 Maximilian 的建议,我访问了该链接并调整了代码以创建:

from sklearn.tree._tree import TREE_LEAF

def prune_index(inner_tree, index):
# turn node into a leaf by "unlinking" its children
inner_tree.children_left[index] = TREE_LEAF
inner_tree.children_right[index] = TREE_LEAF
# if there are shildren, visit them as well
if inner_tree.children_left[index] != TREE_LEAF:
prune_index(inner_tree, inner_tree.children_left[index])
prune_index(inner_tree, inner_tree.children_right[index])

就像魅力一样!谢谢!!

关于python - DecisionTreeClassifier - 树的手动修剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53896626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com