1

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. I am attaching a picture below.

My question is whether I can manually delete a node. I can capture the click, i.e. which node you want removed; however I do not see within a DecisionTreeClassifier the option to manually delete a particular node.

Example Tree Fullsize image

Much obliged.

Marin

AHeyne
  • 3,377
  • 2
  • 11
  • 16
Mark
  • 45
  • 5
  • Did you have a look at this question https://stackoverflow.com/questions/51397109/prune-unnecessary-leaves-in-sklearn-decisiontreeclassifier ? – Maximilian Peters Dec 23 '18 at 10:36
  • Thanks, this worked perfectly. I will put the adjusted code that does the trick in the answer! – Mark Dec 23 '18 at 17:47

1 Answers1

1

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!!

Mark
  • 45
  • 5