1

I am trying to apply reduced-error post-pruning on a decision tree that is implemented with the a nested dictionary in Python. Here's a sample of the tree, when fitted on the data and with a constraint of being at most three layers deep (although it could be much deeper).

{'isLeaf': 'no',
 'attr': 8,
 'val': 0.936979167,
 'mode': 0,
 'left': {'isLeaf': 'no',
  'attr': 's',
  'val': 'Summer',
  'mode': 0,
  'left': {'isLeaf': 'no',
   'attr': 'a',
   'val': 4,
   'mode': 0,
   'left': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None},
   'right': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None}},
  'right': {'isLeaf': 'no',
   'attr': 3,
   'val': 'Sky',
   'mode': 0,
   'left': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None},
   'right': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None}}},
 'right': {'isLeaf': 'no',
  'attr': 4,
  'val': 23,
  'mode': 1,
  'left': {'isLeaf': 'no',
   'attr': 1,
   'val': 24.6,
   'mode': 0,
   'left': {'isLeaf': 'yes', 'mode': 1, 'left': None, 'right': None},
   'right': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None}},
  'right': {'isLeaf': 'no',
   'attr': 11,
   'val': 'day',
   'mode': 1,
   'left': {'isLeaf': 'yes', 'mode': 1, 'left': None, 'right': None},
   'right': {'isLeaf': 'yes', 'mode': 0, 'left': None, 'right': None}}}}

Essentially, I have the attributes 'isLeaf','attr','val' and 'mode' along with the left and right children contained in a further nested level. The leaf nodes do not split so lack the 'attr' and 'val' keys.As with reduced error pruning, my goal is to successively delete leaves from the bottom (deepest layer) and check the error at each time; if it is equal to/better than the entire tree, I will remove the leaves. I understand how to access/print the terminal nodes right above the leaves (that need to be converted to leaves in each iteration). This could be a simple recursion as:

def get_leaf(tree):
    node=tree
    if node['right']['isLeaf'] =='yes' or node['left']['isLeaf'] =='yes':
        print(node)
    else:
        if node['right']['isLeaf'] != 'yes':
            get_leaf(node['right'])
        if node['left']['isLeaf'] != 'yes':
            get_leaf(node['left'])

However, I'm lost on how to make the modification inplace (and change the entire tree after each iteration) to reflect the smaller tree by having deleted the leaves. This step would be necessary to fit the validation data and test if the pruning is necessary. Any inputs on this would be appreciated.

sineman
  • 89
  • 1
  • 7
  • 1
    Have you read through the docs yet? https://docs.python.org/3/tutorial/datastructures.html#dictionaries – Kacperito Jul 22 '20 at 20:45

0 Answers0