Printing the output of a decision tree without using Sklearn or PPrint packages

220 Views Asked by At

I am trying to print the output of a decision tree in the below format: enter image description here

I have my decision tree stored as a nested dictionary. I am trying to use the nested looping for the dictionary but having no result.

enter image description here

Please let me know any ideas on how to achieve this??

The output must look something like this

|--- XO <= 0.50
|   |--- XM <= 0.50
|   |   |--- XF <= 0.50
|   |   |   |--- class: 0
|   |   |--- XF >  0.50
|   |   |   |--- class: 0
|   |--- XM >  0.50
|   |   |--- XB <= 0.50
|   |   |   |--- XF <= 0.50
|   |   |   |   |--- XG <= 0.50
|   |   |   |   |   |--- class: 0
|   |   |   |   |--- XG >  0.50
|   |   |   |   |   |--- XD <= 0.50
|   |   |   |   |   |   |--- class: 1
|   |   |   |   |   |--- XD >  0.50
|   |   |   |   |   |   |--- class: 0
|   |   |   |--- XF >  0.50
|   |   |   |   |--- class: 1
|   |   |--- XB >  0.50
|   |   |   |--- XI <= 0.50
|   |   |   |   |--- class: 0
|   |   |   |--- XI >  0.5
1

There are 1 best solutions below

0
Heisen On

If you don't know the number of nested dictionaries you have to loop through, you can use recursion:

def dictionary_print(dict_):
    for key, values in dict_.items():
        if isinstance(values, dict): dictionary_print(values)
    else: print(key, ':', values)

This code is from this post.