简体   繁体   English

DecisionTreeClassifier - 手动修剪树

[英]DecisionTreeClassifier - manual pruning of trees

I am making an interactive modelling tool.我正在制作一个交互式建模工具。 The idea is to produce variable with a decision tree.这个想法是用决策树产生变量。 However, this variable needs to make economic meaning (I want to be able to delete splits that make no sense theoretically).但是,这个变量需要具有经济意义(我希望能够删除理论上没有意义的拆分)。 Therefore, I plotted a tree with plotly to be able to listen to where user clicked.因此,我用 plotly 绘制了一棵树,以便能够收听用户单击的位置。 I am attaching a picture below.我在下面附上一张图片。

My question is whether I can manually delete a node.我的问题是我是否可以手动删除节点。 I can capture the click, ie which node you want removed;我可以捕获点击,即您要删除哪个节点; however I do not see within a DecisionTreeClassifier the option to manually delete a particular node.但是我在 DecisionTreeClassifier 中没有看到手动删除特定节点的选项。

示例树 Fullsize image全尺寸图像

Much obliged.多谢。

Marin马林

As per Maximilian's suggestion I visited the link and adjusted the code for just a tiny bit to create:根据马克西米利安的建议,我访问了链接并调整了代码以创建:

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])

Works like a charm!奇迹般有效! Thanks!!谢谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM