I am just working on this project where I want to generate a multi-dimensional decision tree from a JSON dynamically. JSON has nested decision rules and each rule as node should be converted. Child nodes represent the decision points. I’ll give an example below:
{
"rule": "A > 5",
"true": {
"rule": "B < 3",
"true": {"rule": "C==1", "true": "Leaf 1", "false": "Leaf 2"},
"false": {"rule": "D!= 4", "true": "Leaf 3", "false": "Leaf 4"}
},
"false": {
"rule": "E>= 2",
"true": {"rule": "F <6", "true": "Leaf 5", "false": "Leaf 6"},
"false": {"rule": "G==0", "true": "Leaf 7", "false": "Leaf 8"}
}
}
The goal is to create a decision tree using Python. Each node as instance of TreeNode class. I want the specific conditions to solve this.
class TreeNode: def __init__(self, rule, truebranch, falsebranch): self.rule = rule self.truebranch= truebranch self.falsebranch =falsebranch def evaluate(self, conditions): pass
I expect a build_tree_from_json function to perform this fucntionaliyt. I tried with some parsing of rule strings and managing different datatypes for leaf nodes in the tree.
I need guidance on:
Proper parsing operators and values.
Evaluate method should manage it all correct.
Handle egde cases precisely without errors.